/
Value.js
73 lines (63 loc) · 1.76 KB
/
Value.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
import React from 'react';
import classNames from 'classnames';
const Value = React.createClass({
displayName: 'Value',
propTypes: {
disabled: React.PropTypes.bool, // disabled prop passed to ReactSelect
onClick: React.PropTypes.func, // method to handle click on value label
onRemove: React.PropTypes.func, // method to handle removal of the value
value: React.PropTypes.object.isRequired, // the option object for this value
},
handleMouseDown (event) {
if (event.type === 'mousedown' && event.button !== 0) {
return;
}
if (this.props.onClick) {
event.stopPropagation();
this.props.onClick(this.props.value, event);
return;
}
if (this.props.value.href) {
event.stopPropagation();
}
},
onRemove (event) {
event.preventDefault();
event.stopPropagation();
this.props.onRemove(this.props.value);
},
renderRemoveIcon () {
if (this.props.disabled || !this.props.onRemove) return;
return (
<span className="Select-value-icon"
onMouseDown={this.onRemove}
onTouchEnd={this.onRemove}>
×
</span>
);
},
renderLabel () {
let className = 'Select-value-label';
return this.props.onClick || this.props.value.href ? (
<a className={className} href={this.props.value.href} target={this.props.value.target} onMouseDown={this.handleMouseDown} onTouchEnd={this.handleMouseDown}>
{this.props.children}
</a>
) : (
<span className={className}>
{this.props.children}
</span>
);
},
render () {
return (
<div className={classNames('Select-value', this.props.value.className)}
style={this.props.value.style}
title={this.props.value.title}
>
{this.renderRemoveIcon()}
{this.renderLabel()}
</div>
);
}
});
module.exports = Value;