-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.tsx
174 lines (158 loc) · 4.33 KB
/
index.tsx
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
/**
* External dependencies
*/
import classNames from 'classnames';
import type { ChangeEvent, FocusEvent, ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useState, forwardRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import InputBase from '../input-control/input-base';
import { Select } from './styles/select-control-styles';
import type { WordPressComponentProps } from '../ui/context';
import type { SelectControlProps } from './types';
import SelectControlChevronDown from './chevron-down';
const noop = () => {};
function useUniqueId( idProp?: string ) {
const instanceId = useInstanceId( SelectControl );
const id = `inspector-select-control-${ instanceId }`;
return idProp || id;
}
function UnforwardedSelectControl(
{
className,
disabled = false,
help,
hideLabelFromVision,
id: idProp,
label,
multiple = false,
onBlur = noop,
onChange = noop,
onFocus = noop,
options = [],
size = 'default',
value: valueProp,
labelPosition = 'top',
children,
prefix,
suffix,
__next36pxDefaultSize = false,
__nextHasNoMarginBottom = false,
...props
}: WordPressComponentProps< SelectControlProps, 'select', false >,
ref: ForwardedRef< HTMLSelectElement >
) {
const [ isFocused, setIsFocused ] = useState( false );
const id = useUniqueId( idProp );
const helpId = help ? `${ id }__help` : undefined;
// Disable reason: A select with an onchange throws a warning.
if ( ! options?.length && ! children ) return null;
const handleOnBlur = ( event: FocusEvent< HTMLSelectElement > ) => {
onBlur( event );
setIsFocused( false );
};
const handleOnFocus = ( event: FocusEvent< HTMLSelectElement > ) => {
onFocus( event );
setIsFocused( true );
};
const handleOnChange = ( event: ChangeEvent< HTMLSelectElement > ) => {
if ( multiple ) {
const selectedOptions = Array.from( event.target.options ).filter(
( { selected } ) => selected
);
const newValues = selectedOptions.map( ( { value } ) => value );
onChange( newValues );
return;
}
onChange( event.target.value, { event } );
};
const classes = classNames( 'components-select-control', className );
/* eslint-disable jsx-a11y/no-onchange */
return (
<BaseControl
help={ help }
id={ id }
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
>
<InputBase
className={ classes }
disabled={ disabled }
hideLabelFromVision={ hideLabelFromVision }
id={ id }
isFocused={ isFocused }
label={ label }
size={ size }
suffix={ suffix || <SelectControlChevronDown /> }
prefix={ prefix }
labelPosition={ labelPosition }
__next36pxDefaultSize={ __next36pxDefaultSize }
>
<Select
{ ...props }
__next36pxDefaultSize={ __next36pxDefaultSize }
aria-describedby={ helpId }
className="components-select-control__input"
disabled={ disabled }
id={ id }
multiple={ multiple }
onBlur={ handleOnBlur }
onChange={ handleOnChange }
onFocus={ handleOnFocus }
ref={ ref }
selectSize={ size }
value={ valueProp }
>
{ children ||
options.map( ( option, index ) => {
const key =
option.id ||
`${ option.label }-${ option.value }-${ index }`;
return (
<option
key={ key }
value={ option.value }
disabled={ option.disabled }
>
{ option.label }
</option>
);
} ) }
</Select>
</InputBase>
</BaseControl>
);
/* eslint-enable jsx-a11y/no-onchange */
}
/**
* `SelectControl` allows users to select from a single or multiple option menu.
* It functions as a wrapper around the browser's native `<select>` element.
*
* @example
* import { SelectControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MySelectControl = () => {
* const [ size, setSize ] = useState( '50%' );
*
* return (
* <SelectControl
* label="Size"
* value={ size }
* options={ [
* { label: 'Big', value: '100%' },
* { label: 'Medium', value: '50%' },
* { label: 'Small', value: '25%' },
* ] }
* onChange={ setSize }
* />
* );
* };
*/
export const SelectControl = forwardRef( UnforwardedSelectControl );
export default SelectControl;