-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (128 loc) · 3.78 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
var keys = require('keys')
, Textarea = require('textarea-grow')
var InputHead = module.exports = React.createClass({
getDefaultProps: function () {
return {
actions: {},
keymap: {},
value: '',
multiline: true,
onChange: function () {},
setFocus: false,
onFocus: function () {},
setSelection: false,
className: ''
}
},
keyMap: function () {
var keymap = {}
, key
if (this.props.onNext) {
keymap['tab'] = this.props.onNext
}
if (this.props.onPrev) {
keymap['shift tab'] = this.props.onPrev
}
if (this.props.keymap) {
for (var name in this.props.actions) {
key = this.props.keymap[name]
if (!key) continue
keymap[keys.normalize(key).value] = this.props.actions[name].bind(null, true)
}
if (this.props.keymap.newNode) {
keymap[keys.normalize(this.props.keymap.newNode).value] = this.onReturn.bind(this, false)
}
if (this.props.keymap.newAfter) {
keymap[keys.normalize(this.props.keymap.newAfter).value] = this.onReturn.bind(this, true)
}
}
if (this.props.actions) {
keymap['backspace'] = this.onBackspace
keymap['left'] = this.onLeft
keymap['right'] = this.onRight
}
if (this.props.preserveReturn) {
delete keymap['return']
}
return keys(keymap)
},
onBackspace: function (e) {
if (e.target.selectionEnd > 0) return true
this.props.actions.remove(this.props.value)
},
onLeft: function (e) {
if (e.target.selectionEnd > 0) return true
this.props.actions.goLeft()
},
onRight: function (e) {
if (e.target.selectionStart < e.target.value.length) return true
this.props.actions.goRight()
},
onReturn: function (after) {
var inp = this.refs.input.getDOMNode()
, pos = inp.selectionStart
, bef = this.props.value.slice(0, pos)
, aft = this.props.value.slice(pos)
if (bef !== this.props.value) this.props.onChange(bef)
this.props.actions.createAfter({text: aft}, after)
},
inputChange: function (e) {
this.props.onChange(e.target.value)
},
focus: function (start) {
this.focusMe(start)
},
onFocus: function () {
// if (!this.props.setFocus && this.props.onFocus) this.props.onFocus()
},
// component api
focusMe: function (start) {
if (this.props.multiline) {
return this.refs.input.focus(start)
}
var inp = this.refs.input.getDOMNode()
, focusAtStart = start || this.props.setFocus === 'start'
, pos = 0
if (inp === document.activeElement) return
if (this.props.value && !focusAtStart) pos = this.props.value.length
inp.focus()
inp.selectionStart = inp.selectionEnd = pos
},
selectMe: function () {
var inp = this.refs.input.getDOMNode()
inp.selectionStart = inp.selectionEnd = this.props.setSelection
},
componentDidMount: function () {
if (this.props.setFocus) {
this.focusMe()
}
if (this.props.setSelection !== false) {
this.selectMe()
}
},
componentDidUpdate: function () {
if (this.props.setFocus) {
this.focusMe()
}
if (this.props.setSelection !== false) {
this.selectMe()
}
},
shouldComponentUpdate: function (props, state) {
return props.value !== this.props.value || props.multiline !== this.props.multiline
},
render: function () {
var n = this.props.multiline ? Textarea : React.DOM.input
return n({
ref: 'input',
className: 'input-head ' + this.props.className + ' ' + (this.props.setFocus ? 'focus' : ''),
onChange: this.inputChange,
onFocus: this.onFocus,
placeholder: 'Type here',
value: this.props.value,
goUp: this.props.actions.goUp,
goDown: this.props.actions.goDown,
onKeyDown: this.keyMap()
})
}
})