forked from reactjs/react-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathList.js
117 lines (99 loc) · 3.15 KB
/
List.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
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
114
115
116
117
var ListOption = require('./ListOption');
var React = require('react/addons');
var {PureRenderMixin} = React.addons;
var joinClasses = require('react/lib/joinClasses');
/**
* <List> is a component that renders the list that Combobox displays.
*/
var List = React.createClass({
mixins: [PureRenderMixin],
propTypes: {
/**
* Function that given an `index` of an option, returns an ID that should
* be unique across the document, but deterministic--multiple calls of this
* function with the same `index` should return the same ID.
*
* Useful for assigning `aria-activedescendant` in a parent component.
*/
getDescendantIdForOption: React.PropTypes.func.isRequired,
/**
* Function that takes an `option` value, and returns a string label.
*/
getLabelForOption: React.PropTypes.func.isRequired,
/**
* Event handler called if the user does an action to change `optionIndex`.
* The function called receives the value of `optionIndex` to change to.
*/
onChange: React.PropTypes.func.isRequired,
/**
* Event handler called if the user does an action to complete a given
* option into the parent <Combobox> as the value. The function called
* receives the value of `optionIndex` to complete.
*/
onComplete: React.PropTypes.func.isRequired,
/**
* The React component to use render for each option of the list popup.
* The component must support rendering passed properties and `className`,
* and will receive the `option` and `getLabelForOption` to help with
* rendering.
*/
optionComponent: React.PropTypes.func,
/**
* The currently focused index of the list view.
*/
optionIndex: React.PropTypes.number,
/**
* The different option values that the user is selecting between.
*/
options: React.PropTypes.array
},
getDefaultProps: function() {
return {
optionComponent: ListOption,
optionIndex: null,
options: []
};
},
handleOptionMouseEnter: function(idx, event) {
this.props.onChange(idx);
},
handleOptionClick: function(idx, event) {
this.props.onComplete(idx);
},
render: function() {
var OptionComponent = this.props.optionComponent;
var {
className,
optionIndex,
options,
getLabelForOption,
getDescendantIdForOption,
...otherProps
} = this.props;
return (
<ul
{...otherProps}
className={joinClasses('List', className)}
role="listbox">
{options.map((option, idx) => {
return (
<OptionComponent
className={joinClasses(
'List-option',
(idx === optionIndex) && 'List-option--isFocused'
)}
id={getDescendantIdForOption(idx)}
getLabelForOption={getLabelForOption}
key={idx}
onClick={this.handleOptionClick.bind(this, idx)}
onMouseEnter={this.handleOptionMouseEnter.bind(this, idx)}
option={option}
role="listitem"
/>
);
})}
</ul>
);
}
});
module.exports = List;