This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
index.js
343 lines (318 loc) · 8.42 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* External dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import {
Button,
MenuGroup,
Spinner,
TextControl,
withSpokenMessages,
} from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import { compose, withInstanceId, withState } from '@wordpress/compose';
import { escapeRegExp, findIndex } from 'lodash';
import Gridicon from 'gridicons';
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import { buildTermsTree } from './hierarchy';
import SearchListItem from './item';
import Tag from '../tag';
const defaultMessages = {
clear: __( 'Clear all selected items', 'woocommerce-admin' ),
list: __( 'Results', 'woocommerce-admin' ),
noItems: __( 'No items found.', 'woocommerce-admin' ),
noResults: __( 'No results for %s', 'woocommerce-admin' ),
search: __( 'Search for items', 'woocommerce-admin' ),
selected: ( n ) =>
sprintf(
_n(
'%d item selected',
'%d items selected',
n,
'woocommerce-admin'
),
n
),
updated: __( 'Search results updated.', 'woocommerce-admin' ),
};
/**
* Component to display a searchable, selectable list of items.
*/
export class SearchListControl extends Component {
constructor() {
super( ...arguments );
this.onSelect = this.onSelect.bind( this );
this.onRemove = this.onRemove.bind( this );
this.onClear = this.onClear.bind( this );
this.isSelected = this.isSelected.bind( this );
this.defaultRenderItem = this.defaultRenderItem.bind( this );
this.renderList = this.renderList.bind( this );
}
componentDidUpdate( prevProps ) {
const { onSearch, search } = this.props;
if ( search !== prevProps.search && typeof onSearch === 'function' ) {
onSearch( search );
}
}
onRemove( id ) {
const { isSingle, onChange, selected } = this.props;
return () => {
if ( isSingle ) {
onChange( [] );
}
const i = findIndex( selected, { id } );
onChange( [
...selected.slice( 0, i ),
...selected.slice( i + 1 ),
] );
};
}
onSelect( item ) {
const { isSingle, onChange, selected } = this.props;
return () => {
if ( this.isSelected( item ) ) {
this.onRemove( item.id )();
return;
}
if ( isSingle ) {
onChange( [ item ] );
} else {
onChange( [ ...selected, item ] );
}
};
}
onClear() {
this.props.onChange( [] );
}
isSelected( item ) {
return findIndex( this.props.selected, { id: item.id } ) !== -1;
}
getFilteredList( list, search ) {
const { isHierarchical } = this.props;
if ( ! search ) {
return isHierarchical ? buildTermsTree( list ) : list;
}
const messages = { ...defaultMessages, ...this.props.messages };
const re = new RegExp( escapeRegExp( search ), 'i' );
this.props.debouncedSpeak( messages.updated );
const filteredList = list
.map( ( item ) => ( re.test( item.name ) ? item : false ) )
.filter( Boolean );
return isHierarchical
? buildTermsTree( filteredList, list )
: filteredList;
}
defaultRenderItem( args ) {
return <SearchListItem { ...args } />;
}
renderList( list, depth = 0 ) {
const { isSingle, search } = this.props;
const renderItem = this.props.renderItem || this.defaultRenderItem;
if ( ! list ) {
return null;
}
return list.map( ( item ) => (
<Fragment key={ item.id }>
{ renderItem( {
item,
isSelected: this.isSelected( item ),
onSelect: this.onSelect,
isSingle,
search,
depth,
} ) }
{ this.renderList( item.children, depth + 1 ) }
</Fragment>
) );
}
renderListSection() {
const { isLoading, search } = this.props;
const messages = { ...defaultMessages, ...this.props.messages };
if ( isLoading ) {
return (
<div className="woocommerce-search-list__list is-loading">
<Spinner />
</div>
);
}
const list = this.getFilteredList( this.props.list, search );
if ( ! list.length ) {
return (
<div className="woocommerce-search-list__list is-not-found">
<span className="woocommerce-search-list__not-found-icon">
<Gridicon
icon="notice-outline"
role="img"
aria-hidden="true"
focusable="false"
/>
</span>
<span className="woocommerce-search-list__not-found-text">
{ search
? // eslint-disable-next-line @wordpress/valid-sprintf
sprintf( messages.noResults, search )
: messages.noItems }
</span>
</div>
);
}
return (
<MenuGroup
label={ messages.list }
className="woocommerce-search-list__list"
>
{ this.renderList( list ) }
</MenuGroup>
);
}
renderSelectedSection() {
const { isLoading, isSingle, selected } = this.props;
const messages = { ...defaultMessages, ...this.props.messages };
if ( isLoading || isSingle || ! selected ) {
return null;
}
const selectedCount = selected.length;
return (
<div className="woocommerce-search-list__selected">
<div className="woocommerce-search-list__selected-header">
<strong>{ messages.selected( selectedCount ) }</strong>
{ selectedCount > 0 ? (
<Button
isLink
isDestructive
onClick={ this.onClear }
aria-label={ messages.clear }
>
{ __( 'Clear all', 'woocommerce-admin' ) }
</Button>
) : null }
</div>
{ selected.map( ( item, i ) => (
<Tag
key={ i }
label={ item.name }
id={ item.id }
remove={ this.onRemove }
/>
) ) }
</div>
);
}
render() {
const { className = '', search, setState } = this.props;
const messages = { ...defaultMessages, ...this.props.messages };
return (
<div className={ `woocommerce-search-list ${ className }` }>
{ this.renderSelectedSection() }
<div className="woocommerce-search-list__search">
<TextControl
label={ messages.search }
type="search"
value={ search }
onChange={ ( value ) => setState( { search: value } ) }
/>
</div>
{ this.renderListSection() }
</div>
);
}
}
SearchListControl.propTypes = {
/**
* Additional CSS classes.
*/
className: PropTypes.string,
/**
* Whether the list of items is hierarchical or not. If true, each list item is expected to
* have a parent property.
*/
isHierarchical: PropTypes.bool,
/**
* Whether the list of items is still loading.
*/
isLoading: PropTypes.bool,
/**
* Restrict selections to one item.
*/
isSingle: PropTypes.bool,
/**
* A complete list of item objects, each with id, name properties. This is displayed as a
* clickable/keyboard-able list, and possibly filtered by the search term (searches name).
*/
list: PropTypes.arrayOf(
PropTypes.shape( {
id: PropTypes.number,
name: PropTypes.string,
} )
),
/**
* Messages displayed or read to the user. Configure these to reflect your object type.
* See `defaultMessages` above for examples.
*/
messages: PropTypes.shape( {
/**
* A more detailed label for the "Clear all" button, read to screen reader users.
*/
clear: PropTypes.string,
/**
* Label for the list of selectable items, only read to screen reader users.
*/
list: PropTypes.string,
/**
* Message to display when the list is empty (implies nothing loaded from the server
* or parent component).
*/
noItems: PropTypes.string,
/**
* Message to display when no matching results are found. %s is the search term.
*/
noResults: PropTypes.string,
/**
* Label for the search input
*/
search: PropTypes.string,
/**
* Label for the selected items. This is actually a function, so that we can pass
* through the count of currently selected items.
*/
selected: PropTypes.func,
/**
* Label indicating that search results have changed, read to screen reader users.
*/
updated: PropTypes.string,
} ),
/**
* Callback fired when selected items change, whether added, cleared, or removed.
* Passed an array of item objects (as passed in via props.list).
*/
onChange: PropTypes.func.isRequired,
/**
* Callback fired when the search field is used.
*/
onSearch: PropTypes.func,
/**
* Callback to render each item in the selection list, allows any custom object-type rendering.
*/
renderItem: PropTypes.func,
/**
* The list of currently selected items.
*/
selected: PropTypes.array.isRequired,
// from withState
search: PropTypes.string,
setState: PropTypes.func,
// from withSpokenMessages
debouncedSpeak: PropTypes.func,
// from withInstanceId
instanceId: PropTypes.number,
};
export default compose( [
withState( {
search: '',
} ),
withSpokenMessages,
withInstanceId,
] )( SearchListControl );