forked from ElemeFE/element-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestions.jsx
113 lines (97 loc) · 2.8 KB
/
Suggestions.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* @flow */
import React from 'react';
import ReactDOM from 'react-dom';
import Popper from '../../libs/utils/popper';
import { Component, PropTypes, Transition, View } from '../../libs';
import { Scrollbar } from '../scrollbar';
type Props = {
suggestions: Array<any>,
}
type State = {
showPopper: boolean,
dropdownWidth: string,
}
export default class Suggestions extends Component {
props: Props;
state: State;
constructor(props: Props) {
super(props);
this.state = {
showPopper: false,
dropdownWidth: ''
};
}
onVisibleChange(visible: boolean, inputWidth: string): void {
this.setState({
dropdownWidth: inputWidth,
showPopper: visible
});
}
parent(): Component {
return this.context.component;
}
onSelect(item: Object): void {
this.parent().select(item);
}
onEnter(): void {
const reference = ReactDOM.findDOMNode(this.parent().inputNode);
this.popperJS = new Popper(reference, this.refs.popper, {
gpuAcceleration: false,
forceAbsolute: true
});
}
onAfterLeave(): void {
this.popperJS.destroy();
}
render(): React.Element<any> {
const { customItem } = this.parent().props;
const { loading, highlightedIndex } = this.parent().state;
const { suggestions } = this.props;
const { showPopper, dropdownWidth } = this.state;
return (
<Transition name="el-zoom-in-top" onEnter={this.onEnter.bind(this)} onAfterLeave={this.onAfterLeave.bind(this)}>
<View show={showPopper}>
<div
ref="popper"
className={this.classNames('el-autocomplete-suggestion', 'el-popper', {
'is-loading': loading
})}
style={{
width: dropdownWidth,
zIndex: 1
}}
>
<Scrollbar
viewComponent="ul"
wrapClass="el-autocomplete-suggestion__wrap"
viewClass="el-autocomplete-suggestion__list"
>
{
loading ? (
<li><i className="el-icon-loading"></i></li>
) : suggestions.map((item, index) => {
return (
<li
key={index}
className={this.classNames({'highlighted': highlightedIndex === index})}
onClick={this.onSelect.bind(this, item)}>
{
!customItem ? item.value : React.createElement(customItem, {
index,
item
})
}
</li>
)
})
}
</Scrollbar>
</div>
</View>
</Transition>
)
}
}
Suggestions.contextTypes = {
component: PropTypes.any
};