-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (94 loc) · 2.61 KB
/
index.js
File metadata and controls
104 lines (94 loc) · 2.61 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
import React, { Component } from 'react';
import cx from 'classnames';
import Select from '../Select';
import MultiSelectTrigger from './SelectTrigger';
export default class PowerSelectMultiple extends Component {
state = {};
componentWillMount() {
this.filterOptions(this.props.options, this.props.selected);
}
componentWillReceiveProps(nextProps) {
this.filterOptions(nextProps.options, nextProps.selected);
}
handleOnChange = ({ option, select }) => {
let { selected, onChange } = this.props;
if (option) {
let options = selected.slice();
options.push(option);
onChange({
options,
select,
});
}
select.actions.focus();
if (select.searchTerm) {
select.actions.search('');
}
};
filterOptions(options, selected, callback) {
let filteredOptions = options.filter(option => selected.indexOf(option) === -1);
this.setState({ filteredOptions }, callback);
}
handleKeyDown = (event, { select }) => {
if (event.which === 8) {
let { selected, onChange } = this.props;
let value = event.target.value;
if (value === '' && selected.length) {
let options = selected.slice(0, selected.length - 1);
onChange({
options,
select,
});
select.actions.open();
select.actions.focus();
}
}
if (this.props.onKeyDown) {
this.props.onKeyDown(event, { select });
}
};
removeOption = ({ option, select }) => {
let { selected, onChange } = this.props;
let options = selected.filter(opt => opt !== option);
onChange({
options,
select,
});
select.actions.focus();
};
handleClearClick = (event, { select }) => {
event.stopPropagation();
this.props.onChange({
options: [],
select,
});
if (select.searchTerm) {
select.actions.search('');
}
select.actions.close();
select.actions.focus();
};
render() {
let { className, options, onChange, ...rest } = this.props;
return (
<Select
className={cx('PowerSelectMultiple', className)}
ref={select => (this.select = select)}
triggerComponent={props => (
<MultiSelectTrigger
{...props}
showOptionClose={true}
onOptionCloseClick={this.removeOption}
onClearClick={this.handleClearClick}
/>
)}
{...rest}
options={this.state.filteredOptions}
onChange={this.handleOnChange}
closeOnSelect={false}
onKeyDown={this.handleKeyDown}
/>
);
}
}
PowerSelectMultiple.displayName = 'PowerSelectMultiple';