-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.js
More file actions
122 lines (105 loc) · 2.87 KB
/
main.js
File metadata and controls
122 lines (105 loc) · 2.87 KB
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
var React = require('react')
var ReactDOM = require('react-dom')
var TokenInput = require('../index')
var ComboboxOption = require('../index').Option
var without = require('lodash-node/modern/arrays/without')
var uniq = require('lodash-node/modern/arrays/uniq')
var names = require('./names')
class App extends React.Component {
state = {
input: '',
loading: false,
selected: [],
options: names
};
handleChange = (value) => {
this.setState({
selected: value
})
};
handleRemove = (value) => {
var selectedOptions = uniq(without(this.state.selected,value))
this.handleChange(selectedOptions)
};
handleSelect = (value, combobox) => {
if(typeof value === 'string') {
value = {id: value, name: value};
}
var selected = uniq(this.state.selected.concat([value]))
this.setState({
selected: selected,
selectedToken: null
})
this.handleChange(selected)
};
handleInput = (userInput) => {
this.setState({
input: userInput,
loading: true,
options: []
})
setTimeout(function () {
this.filterTags(this.state.input)
this.setState({
loading: false
})
}.bind(this), 500)
};
filterTags = (userInput) => {
if (userInput === '')
return this.setState({options: []});
var filter = new RegExp('^'+userInput, 'i');
var filteredNames = names.filter(function(state) {
return filter.test(state.name); // || filter.test(state.id);
}).filter(function(state) {
return this.state.selected
.map(function(value) { return value.name })
.indexOf(state.name) === -1
}.bind(this))
this.setState({
options: filteredNames
});
};
renderComboboxOptions = () => {
return this.state.options.map(function(name) {
return (
<ComboboxOption
key={name.id}
value={name}
isFocusable={name.name.length > 1}
>{name.name}</ComboboxOption>
);
});
};
render() {
var selectedNames = this.state.selected.map(function(tag) {
return <li key={tag.id}>{tag.name}</li>
})
var options = this.state.options.length ?
this.renderComboboxOptions() : [];
const loadingComponent = (
<img src='spinner.gif' />
)
return (
<div>
<h1>React TokenInput Example</h1>
<TokenInput
isLoading={this.state.loading}
loadingComponent={loadingComponent}
menuContent={options}
onChange={this.handleChange}
onInput={this.handleInput}
onSelect={this.handleSelect}
onRemove={this.handleRemove}
selected={this.state.selected}
placeholder='Enter tokens here'
/>
<h2>Selected</h2>
<ul>
{selectedNames}
</ul>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('application'))