/
AutoComplete.jsx
96 lines (75 loc) · 2.62 KB
/
AutoComplete.jsx
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
import React from 'react';
import _ from 'lodash';
import hierarchicalMappingChannel from '../../../store';
import Highlighter from 'react-highlight-words';
import {SelectBox, Nothing} from 'ecc-gui-elements';
const Highlight = (props) => {
const {textToHighlight, searchWord} = props;
if (!_.isString(textToHighlight) || _.isEmpty(textToHighlight)) {
return <Nothing/>;
}
if (!_.isString(searchWord) || _.isEmpty(searchWord)) {
return <span>{textToHighlight}</span>;
}
return <Highlighter textToHighlight={textToHighlight} searchWords={[searchWord]}/>;
};
const AutoComplete = React.createClass({
getInitialState(){
return {}
},
optionRender(option) {
const {label, value, description, $userCreated} = option;
if ($userCreated) {
return (
<strong className="Select-option__label">
{label}
</strong>
);
}
return (
[
<strong className="Select-option__label"><Highlight textToHighlight={label} searchWord={this._inputValue}/></strong>,
<code className="Select-option__value"><Highlight textToHighlight={value} searchWord={this._inputValue}/></code>,
<span className="Select-option__description"><Highlight textToHighlight={description} searchWord={this._inputValue}/></span>
]
);
},
newOptionCreator({label, labelKey, valueKey}) {
return {
[labelKey]: label,
[valueKey]: label,
$userCreated: true,
}
},
render() {
const {entity, ruleId, ...otherProps} = this.props;
const loadOptionsRaw = (input, callback) => {
hierarchicalMappingChannel.request({
topic: 'autocomplete',
data: {
entity,
input,
ruleId,
}
}).subscribe(({options}) => {
callback(null, {
options,
complete: false,
})
})
};
return (
<SelectBox {...otherProps}
onInputChange={(inputValue) => {
this._inputValue = inputValue;
return inputValue;
}}
async={true}
optionRenderer={this.optionRender}
newOptionCreator={this.newOptionCreator}
loadOptions={loadOptionsRaw}
/>
);
}
});
export default AutoComplete;