-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathexample.js
More file actions
96 lines (80 loc) · 2.34 KB
/
example.js
File metadata and controls
96 lines (80 loc) · 2.34 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
var React = require('react')
var TokenInput = require('react-tokeninput')
var ComboboxOption = require('react-tokeninput').Option
var without = require('lodash-node/modern/arrays/without')
var uniq = require('lodash-node/modern/arrays/uniq')
var names = require('./names')
var App = React.createClass({displayName: 'App',
getInitialState: function() {
return {
selected: [],
options: names
};
},
handleChange: function(value) {
this.setState({
selected: value
})
},
handleRemove: function(value) {
var selectedOptions = uniq(without(this.state.selected,value))
this.handleChange(selectedOptions)
},
handleSelect: function(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: function(userInput) {
// this.setState({selectedStateId: null});
this.filterTags(userInput);
},
filterTags: function(userInput) {
if (userInput === '')
return this.setState({options: []});
var filter = new RegExp('^'+userInput, 'i');
this.setState({options: names.filter(function(state) {
return filter.test(state.name) || filter.test(state.id);
})});
},
renderComboboxOptions: function() {
return this.state.options.map(function(flavor) {
return (
ComboboxOption({
key: flavor.id,
value: flavor
}, flavor.name)
);
});
},
render: function() {
var selectedFlavors = this.state.selected.map(function(tag) {
return React.DOM.li({key: tag.id}, tag.name)
})
var options = this.state.options.length ?
this.renderComboboxOptions() : [];
return (
React.DOM.div(null,
React.DOM.h1(null, "React TokenInput Example"),
TokenInput({
onChange: this.handleChange,
onInput: this.handleInput,
onSelect: this.handleSelect,
onRemove: this.handleRemove,
selected: this.state.selected,
menuContent: options}),
React.DOM.h2(null, "Selected"),
React.DOM.ul(null,
selectedFlavors
)
)
);
}
})
React.renderComponent(App(null), document.getElementById('application'))