-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathtoken.js
66 lines (59 loc) · 1.49 KB
/
token.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
var React = require('react');
var classNames = require('classnames');
var createReactClass = require('create-react-class');
var PropTypes = require('prop-types');
/**
* Encapsulates the rendering of an option that has been "selected" in a
* TypeaheadTokenizer
*/
var Token = createReactClass({
propTypes: {
className: PropTypes.string,
name: PropTypes.string,
children: PropTypes.string,
object: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
onRemove: PropTypes.func,
value: PropTypes.string
},
render: function() {
var className = classNames([
"typeahead-token",
this.props.className
]);
return (
<div className={className}>
{this._renderHiddenInput()}
{this.props.children}
{this._renderCloseButton()}
</div>
);
},
_renderHiddenInput: function() {
// If no name was set, don't create a hidden input
if (!this.props.name) {
return null;
}
return (
<input
type="hidden"
name={ this.props.name + '[]' }
value={ this.props.value || this.props.object }
/>
);
},
_renderCloseButton: function() {
if (!this.props.onRemove) {
return "";
}
return (
<a className={this.props.className || "typeahead-token-close"} href="#" onClick={function(event) {
this.props.onRemove(this.props.object);
event.preventDefault();
}.bind(this)}>×</a>
);
}
});
module.exports = Token;