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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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import clsx from 'clsx';
import { filter, find, get, noop } from 'lodash';
import PropTypes from 'prop-types';
import { createRef, Children, cloneElement, Component, forwardRef } from 'react';
import Count from '../count';
import Gridicon from '../gridicon';
import DropdownItem from './item';
import DropdownLabel from './label';
import DropdownSeparator from './separator';
import TranslatableString from './translatable/proptype';
import './style.scss';

class SelectDropdown extends Component {
	static Item = DropdownItem;
	static Separator = DropdownSeparator;
	static Label = DropdownLabel;

	static propTypes = {
		id: PropTypes.string,
		selectedText: TranslatableString,
		selectedIcon: PropTypes.element,
		selectedCount: PropTypes.number,
		selectedSecondaryIcon: PropTypes.element,
		positionSelectedSecondaryIconOnRight: PropTypes.bool,
		initialSelected: PropTypes.string,
		className: PropTypes.string,
		style: PropTypes.object,
		onSelect: PropTypes.func,
		onToggle: PropTypes.func,
		focusSibling: PropTypes.func,
		tabIndex: PropTypes.number,
		disabled: PropTypes.bool,
		options: PropTypes.arrayOf(
			PropTypes.shape( {
				value: PropTypes.string.isRequired,
				label: PropTypes.oneOfType( [ TranslatableString, PropTypes.node ] ).isRequired,
				path: PropTypes.string,
				icon: PropTypes.element,
				secondaryIcon: PropTypes.element,
				count: PropTypes.number,
			} )
		),
		isLoading: PropTypes.bool,
		ariaLabel: PropTypes.string,
		showSelectedOption: PropTypes.bool,
		children: PropTypes.node,
	};

	static defaultProps = {
		options: [],
		onSelect: noop,
		onToggle: noop,
		style: {},
		showSelectedOption: true,
	};

	instanceId = crypto.randomUUID();

	state = {
		isOpen: false,
		selected: this.getInitialSelectedItem(),
	};

	itemRefs = [];

	setItemRef = ( index ) => ( itemEl ) => {
		this.itemRefs[ index ] = itemEl;
	};

	constructor( props ) {
		super( props );
		this.dropdownContainerRef = props.innerRef ?? createRef();
	}

	componentWillUnmount() {
		window.removeEventListener( 'click', this.handleOutsideClick );
	}

	componentDidUpdate( prevProps, prevState ) {
		if ( this.state.isOpen ) {
			window.addEventListener( 'click', this.handleOutsideClick );
		} else {
			window.removeEventListener( 'click', this.handleOutsideClick );
		}

		if ( this.state.isOpen !== prevState.isOpen ) {
			this.props.onToggle( {
				target: this,
				open: this.state.isOpen,
			} );
		}
	}

	getInitialSelectedItem() {
		// This method is only useful for the case when the component is uncontrolled, i.e., the
		// selected state is in local state as opposed to being maintained by parent container.
		// The `SelectDropdown` is uncontrolled iff the items are specified as `options` prop.
		// (And is controlled when the items are specified as `children`.)
		if ( ! this.props.options.length ) {
			return undefined;
		}

		// Use the `initialSelected` prop if specified
		if ( this.props.initialSelected ) {
			return this.props.initialSelected;
		}

		// Otherwise find the first option that is an item, i.e., not label or separator
		return get(
			find( this.props.options, ( item ) => item && ! item.isLabel ),
			'value'
		);
	}

	getSelectedText() {
		const { options, selectedText } = this.props;
		const { selected } = this.state;

		if ( selectedText ) {
			return selectedText;
		}

		// return currently selected text
		return get( find( options, { value: selected } ), 'label' );
	}

	getSelectedIcon() {
		const { options, selectedIcon } = this.props;
		const { selected } = this.state;

		if ( selectedIcon ) {
			return selectedIcon;
		}

		// return currently selected icon
		return get( find( options, { value: selected } ), 'icon' );
	}

	getSelectedSecondaryIcon() {
		const { options, selectedSecondaryIcon } = this.props;
		const { selected } = this.state;

		if ( selectedSecondaryIcon ) {
			return selectedSecondaryIcon;
		}

		return get( find( options, { value: selected } ), 'secondaryIcon' );
	}

	dropdownOptions() {
		let refIndex = 0;

		if ( this.props.children ) {
			// add refs and focus-on-click handlers to children
			return Children.map( this.props.children, ( child ) => {
				if (
					! child ||
					! [ DropdownItem, DropdownSeparator, DropdownLabel ].includes( child.type )
				) {
					return null;
				}

				return cloneElement( child, {
					ref: child.type === DropdownItem ? this.setItemRef( refIndex++ ) : null,
					onClick: ( event ) => {
						this.dropdownContainerRef.current.focus();
						if ( typeof child.props.onClick === 'function' ) {
							child.props.onClick( event );
						}
					},
				} );
			} );
		}

		return this.props.options
			.filter( ( item ) => {
				if ( this.props.showSelectedOption ) {
					return true;
				}
				return item.value !== this.state.selected;
			} )
			.map( ( item, index ) => {
				if ( ! item ) {
					return <DropdownSeparator key={ 'dropdown-separator-' + index } />;
				}

				if ( item.isLabel ) {
					return <DropdownLabel key={ 'dropdown-label-' + index }>{ item.label }</DropdownLabel>;
				}

				return (
					<DropdownItem
						key={ 'dropdown-item-' + item.value }
						ref={ this.setItemRef( refIndex++ ) }
						selected={ this.state.selected === item.value }
						onClick={ this.onSelectItem( item ) }
						path={ item.path }
						icon={ item.icon }
						secondaryIcon={ item.secondaryIcon }
						count={ item.count }
					>
						{ item.label }
					</DropdownItem>
				);
			} );
	}

	render() {
		const dropdownClassName = clsx( 'select-dropdown', this.props.className, {
			'is-compact': this.props.compact,
			'is-open': this.state.isOpen && ! this.props.disabled,
			'is-disabled': this.props.disabled,
			'has-count': 'number' === typeof this.props.selectedCount,
			'is-loading': this.props?.isLoading,
		} );

		const selectedText = this.getSelectedText();
		const selectedIcon = this.getSelectedIcon();
		const selectedSecondaryIcon = this.getSelectedSecondaryIcon();
		const { positionSelectedSecondaryIconOnRight } = this.props;

		return (
			<div id={ this.props.id } style={ this.props.style } className={ dropdownClassName }>
				<div
					ref={ this.dropdownContainerRef }
					className="select-dropdown__container"
					onKeyDown={ this.navigateItem }
					tabIndex={ this.props.tabIndex || 0 }
					role="button"
					aria-haspopup="true"
					aria-owns={ 'select-submenu-' + this.instanceId }
					aria-controls={ 'select-submenu-' + this.instanceId }
					aria-expanded={ this.state.isOpen }
					aria-disabled={ this.props.disabled }
					data-tip-target={ this.props.tipTarget }
					onClick={ this.toggleDropdown }
				>
					<div id={ 'select-dropdown-' + this.instanceId } className="select-dropdown__header">
						<span className="select-dropdown__header-text" aria-label={ this.props.ariaLabel }>
							{ ! positionSelectedSecondaryIconOnRight && selectedSecondaryIcon }
							{ selectedIcon }
							{ selectedText }
						</span>
						{ 'number' === typeof this.props.selectedCount && (
							<Count count={ this.props.selectedCount } />
						) }
						{ positionSelectedSecondaryIconOnRight && selectedSecondaryIcon }
						<Gridicon icon="chevron-down" size={ 18 } />
					</div>

					<ul
						id={ 'select-submenu-' + this.instanceId }
						className="select-dropdown__options"
						role="listbox"
						aria-labelledby={ 'select-dropdown-' + this.instanceId }
						aria-expanded={ this.state.isOpen }
					>
						{ this.dropdownOptions() }
					</ul>
				</div>
			</div>
		);
	}

	toggleDropdown = () => {
		if ( this.props && this.props.disabled ) {
			return;
		}

		this.setState( {
			isOpen: ! this.state.isOpen,
		} );
	};

	openDropdown() {
		if ( this.props && this.props.disabled ) {
			return;
		}
		this.setState( {
			isOpen: true,
		} );
	}

	closeDropdown() {
		if ( this.state.isOpen ) {
			delete this.focused;
			this.setState( {
				isOpen: false,
			} );
		}
	}

	onSelectItem( option ) {
		return this.selectItem.bind( this, option );
	}

	selectItem( option ) {
		if ( ! option ) {
			return;
		}

		if ( this.props.onSelect ) {
			this.props.onSelect( option );
		}

		this.setState( {
			selected: option.value,
		} );

		this.dropdownContainerRef.current.focus();
	}

	navigateItem = ( event ) => {
		switch ( event.code ) {
			case 'Tab':
				this.navigateItemByTabKey( event );
				break;
			case 'Space':
			case 'Enter':
				event.preventDefault();
				this.activateItem();
				break;
			case 'ArrowUp':
				event.preventDefault();
				this.focusSibling( 'previous' );
				this.openDropdown();
				break;
			case 'ArrowDown':
				event.preventDefault();
				this.focusSibling( 'next' );
				this.openDropdown();
				break;
			case 'Escape':
				event.preventDefault();
				this.closeDropdown();
				this.dropdownContainerRef.current.focus();
				break;
		}
	};

	navigateItemByTabKey( event ) {
		if ( ! this.state.isOpen ) {
			return;
		}

		event.preventDefault();

		const direction = event.shiftKey ? 'previous' : 'next';
		this.focusSibling( direction );
	}

	activateItem() {
		if ( ! this.state.isOpen ) {
			return this.openDropdown();
		}
		document.activeElement.click();
	}

	focusSibling( direction ) {
		// the initial up-arrow/down-arrow should only open the menu
		if ( ! this.state.isOpen ) {
			return;
		}

		let items;
		let focusedIndex;

		if ( this.props.options.length ) {
			items = filter( this.props.options, ( item ) => {
				if ( ! item || item.isLabel ) {
					return false;
				}

				if ( ! this.props.showSelectedOption && item.value === this.state.selected ) {
					return false;
				}

				return true;
			} );

			focusedIndex =
				typeof this.focused === 'number'
					? this.focused
					: items.findIndex( ( item ) => item.value === this.state.selected );
		} else {
			items = filter( this.props.children, ( item ) => item.type === DropdownItem );

			focusedIndex =
				typeof this.focused === 'number'
					? this.focused
					: items.findIndex( ( item ) => item.props.selected );
		}

		const increment = direction === 'previous' ? -1 : 1;
		const newIndex = focusedIndex + increment;

		if ( newIndex >= items.length || newIndex < 0 ) {
			return;
		}

		this.itemRefs[ newIndex ].focusLink();
		this.focused = newIndex;
	}

	handleOutsideClick = ( event ) => {
		if ( ! this.dropdownContainerRef.current.contains( event.target ) ) {
			this.closeDropdown();
		}
	};
}

export default SelectDropdown;

export const SelectDropdownForwardingRef = forwardRef( ( props, ref ) => (
	<SelectDropdown { ...props } innerRef={ ref } />
) );