-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
210 lines (167 loc) · 4.79 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// @TODO: key commands, spoken messages, consider suggesting as a core Gutenberg component
/**
* External dependencies
*/
import classnames from 'classnames';
import {
map,
debounce
} from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import {
withInstanceId,
compose
} from '@wordpress/compose';
import {
Button,
Popover,
withFocusOutside
} from '@wordpress/components';
function filterOptions( options = [], maxResults = 10 ) {
const filtered = [];
for ( let i = 0; i < options.length; i++ ) {
const option = options[ i ];
// Merge label into keywords
let { keywords = [] } = option;
if ( 'string' === typeof option.label ) {
keywords = [ ...keywords, option.label ];
}
filtered.push( option );
// Abort early if max reached
if ( filtered.length === maxResults ) {
break;
}
}
return filtered;
}
export class Lookup extends Component {
static getInitialState() {
return {
selectedIndex: 0,
query: undefined,
filteredOptions: [],
};
}
constructor() {
super( ...arguments );
this.select = this.select.bind( this );
this.reset = this.reset.bind( this );
this.debouncedLoadOptions = debounce( this.loadOptions, 250 );
this.state = this.constructor.getInitialState();
this.onChange = this.onChange.bind( this );
}
componentWillUnmount() {
this.debouncedLoadOptions.cancel();
}
select( option ) {
const { completer } = this.props;
const getOptionCompletion = completer.getOptionCompletion || {};
getOptionCompletion( option );
this.reset();
}
reset() {
this.setState( this.constructor.getInitialState() );
}
handleFocusOutside() {
this.reset();
}
loadOptions( completer, query ) {
const { options } = completer;
const promise = this.activePromise = Promise.resolve(
typeof options === 'function' ? options( query ) : options
).then( ( optionsData ) => {
if ( promise !== this.activePromise ) {
// Another promise has become active since this one was asked to resolve, so do nothing,
// or else we might end triggering a race condition updating the state.
return;
}
const keyedOptions = optionsData.map( ( optionData, optionIndex ) => ( {
key: `${ optionIndex }`,
value: optionData,
label: completer.getOptionLabel( optionData ),
keywords: completer.getOptionKeywords ? completer.getOptionKeywords( optionData ) : []
} ) );
const filteredOptions = filterOptions( keyedOptions );
const selectedIndex = filteredOptions.length === this.state.filteredOptions.length ? this.state.selectedIndex : 0;
this.setState( {
[ 'options' ]: keyedOptions,
filteredOptions,
selectedIndex,
} );
} );
}
onChange( query ) {
const { completer } = this.props;
const { options } = this.state;
if ( ! query ) {
this.reset();
return;
}
if ( completer ) {
if ( completer.isDebounced ) {
this.debouncedLoadOptions( completer, query );
} else {
this.loadOptions( completer, query );
}
}
const filteredOptions = completer ? filterOptions( options ) : [];
if ( completer ) {
this.setState( { selectedIndex: 0, filteredOptions, query } );
}
}
render() {
const { onChange } = this;
const { children, instanceId, completer } = this.props;
const { selectedIndex, filteredOptions } = this.state;
const { key: selectedKey = '' } = filteredOptions[ selectedIndex ] || {};
const { className } = completer;
const isExpanded = filteredOptions.length > 0;
const listBoxId = isExpanded ? `components-autocomplete-listbox-${ instanceId }` : null;
const activeId = isExpanded ? `components-autocomplete-item-${ instanceId }-${ selectedKey }` : null;
return (
<div
className="components-autocomplete"
>
{ children( { isExpanded, listBoxId, activeId, onChange } ) }
{ isExpanded && (
<Popover
focusOnMount={ false }
onClose={ this.reset }
position="top center"
className="components-autocomplete__popover"
noArrow
>
<div
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
>
{ map( filteredOptions, ( option, index ) => (
<Button
key={ option.key }
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` }
role="option"
aria-selected={ index === selectedIndex }
disabled={ option.isDisabled }
className={ classnames( 'components-autocomplete__result', className, {
'is-selected': index === selectedIndex,
} ) }
onClick={ () => this.select( option ) }
>
{ option.label }
</Button>
) ) }
</div>
</Popover>
) }
</div>
);
}
}
export default compose( [
withInstanceId,
withFocusOutside, // this MUST be the innermost HOC as it calls handleFocusOutside
] )( Lookup );