-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautocomplete.jsx
59 lines (57 loc) · 1.61 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
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
export default class Autocomplete extends React.Component {
constructor(props) {
super(props);
this.onSelect = this.onSelect.bind(this);
}
onSelect(item) {
if (this.props.onSelect) this.props.onSelect(item);
}
render() {
return (
<ul className={classNames("rc-autocomplete", this.props.className)}>
{
this.props.label ? (
<li className="rc-label-autocomplete">
<span>
{this.props.label}
</span>
</li>
) : null
}
{
!!this.props.autocomplete && this.props.autocomplete.map((item, index) => {
let props = {};
if (item.href) {
props["href"] = item.href;
props["target"] = "_blank";
}
return(
<li key={`item-${index}`} className={classNames("tag", "item", item.className)} style={item.style} onClick={this.onSelect.bind(this, item)}>
{
this.props.renderItem ? this.props.renderItem(item) : (
<a className="name" {...props}>
{item.name ? item.name : item}
</a>
)
}
</li>
);
})
}
</ul>
);
}
}
Autocomplete.propTypes = {
autocomplete: PropTypes.array,
label: PropTypes.string,
onSelect: PropTypes.func,
renderItem: PropTypes.func,
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
]),
};