-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (149 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/** @jsx React.DOM */
var _ = require('lodash')
, keys = require('keys')
, states = require('./states')
var Tags = React.createClass({displayName: 'Tags',
getDefaultProps: function () {
return {
onChange: function () {
console.warn('Trying to change tags, but no onChange handler set')
},
plusButton: true,
plusClass: '',
className: '',
value: []
}
},
getInitialState: function () {
var editing = this.props.editing
, input = ''
editing = 'undefined' === typeof editing ? false : editing
return {
editing: editing,
focused: this.props.focused || false,
input: input
}
},
// all about the focus
componentDidMount: function () {
if (this.state.focused) {
this.refs.input.getDOMNode().focus()
}
},
componentWillReceiveProps: function (props) {
if (props.focused) {
this.setState({focused: true})
}
},
componentDidUpdate: function () {
if (this.state.focused) {
this.refs.input.getDOMNode().focus()
}
},
// events
inputChange: function (e) {
this.setState({input: e.target.value})
},
stateChange: function (name) {
var tags = this.props.value.slice()
var nstate = states[name](tags, this.state, this.props)
if (!nstate) return
if (nstate.value) {
tags = nstate.value
this.props.onChange(tags)
delete nstate.value
}
this.setState(nstate)
},
keyDown: keys({
'backspace': function (e) {
if (this.state.input.length !== 0) return true
this.stateChange('backspace')
},
'escape': function (e) {
this.stateChange('blur')
},
'return': function (e) {
this.stateChange('return')
},
'tab': function (e) {
this.stateChange('tab')
},
'shift tab': function (e) {
this.stateChange('shift tab')
}
}),
blur: function () {
this.stateChange('blur')
},
focus: function () {
var editing = this.props.value.length ? 0 : false
this.setState({
input: editing === false ? '' : this.props.value[editing],
editing: editing,
focused: true,
})
},
focusEnd: function () {
this.setState({
input: '',
focused: true,
editing: false
})
},
edit: function (i) {
if (i >= this.props.value.length) return this.focusEnd()
this.setState({
input: this.props.value[i],
focused: true,
editing: i
})
},
remove: function (i) {
var tags = this.props.value
tags.splice(i, 1)
this.props.onChange(tags)
this.setState({
input: '',
focused: false,
editing: false
})
},
// and the render!
render: function () {
var ln = this.state.input ? this.state.input.length : 0
, children = this.props.value.map(function (tag, i) {
return React.DOM.div(
{className:"tag"},
React.DOM.span({className:"text", onClick:this.edit.bind(this, i)}, tag),
React.DOM.div({className:"remove-tag small-btn", onClick:this.remove.bind(this, i)}, "×")
)
}.bind(this))
if (this.state.focused) {
var input = (
React.DOM.input( {ref:"input",
onBlur:this.blur,
style:{
width: (ln > 3 ? ln : 3)*7 + 20
},
onKeyDown:this.keyDown,
onChange:this.inputChange,
value:this.state.input})
)
if (this.state.editing !== false) {
children.splice(this.state.editing, 1, input)
} else {
children.push(input)
}
}
return (
React.DOM.div({className:'tags ' + this.props.className},
children,
React.DOM.span({className:"plus small-btn " + this.props.plusClass,
style:{display: (this.state.focused && false === this.state.editing) ? 'none' : 'inline-block'},
onClick:this.focusEnd}, this.props.plusButton ? "+" : '')
)
)
}
})
module.exports = Tags