-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
157 lines (146 loc) · 3.72 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
/**
* External dependencies
*/
import clsx from 'clsx';
import type { ChangeEvent } from 'react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import type { WordPressComponentProps } from '../context';
import type { RadioControlProps } from './types';
import { VStack } from '../v-stack';
import { StyledHelp } from '../base-control/styles/base-control-styles';
import { VisuallyHidden } from '../visually-hidden';
function generateOptionDescriptionId( radioGroupId: string, index: number ) {
return `${ radioGroupId }-${ index }-option-description`;
}
function generateOptionId( radioGroupId: string, index: number ) {
return `${ radioGroupId }-${ index }`;
}
function generateHelpId( radioGroupId: string ) {
return `${ radioGroupId }__help`;
}
/**
* Render a user interface to select the user type using radio inputs.
*
* ```jsx
* import { RadioControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyRadioControl = () => {
* const [ option, setOption ] = useState( 'a' );
*
* return (
* <RadioControl
* label="User type"
* help="The type of the current user"
* selected={ option }
* options={ [
* { label: 'Author', value: 'a' },
* { label: 'Editor', value: 'e' },
* ] }
* onChange={ ( value ) => setOption( value ) }
* />
* );
* };
* ```
*/
export function RadioControl(
props: WordPressComponentProps< RadioControlProps, 'input', false >
) {
const {
label,
className,
selected,
help,
onChange,
hideLabelFromVision,
options = [],
id: preferredId,
...additionalProps
} = props;
const id = useInstanceId(
RadioControl,
'inspector-radio-control',
preferredId
);
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.value );
if ( ! options?.length ) {
return null;
}
return (
<fieldset
id={ id }
className={ clsx( className, 'components-radio-control' ) }
aria-describedby={ !! help ? generateHelpId( id ) : undefined }
>
{ hideLabelFromVision ? (
<VisuallyHidden as="legend">{ label }</VisuallyHidden>
) : (
<BaseControl.VisualLabel as="legend">
{ label }
</BaseControl.VisualLabel>
) }
<VStack
spacing={ 3 }
className={ clsx( 'components-radio-control__group-wrapper', {
'has-help': !! help,
} ) }
>
{ options.map( ( option, index ) => (
<div
key={ generateOptionId( id, index ) }
className="components-radio-control__option"
>
<input
id={ generateOptionId( id, index ) }
className="components-radio-control__input"
type="radio"
name={ id }
value={ option.value }
onChange={ onChangeValue }
checked={ option.value === selected }
aria-describedby={
!! option.description
? generateOptionDescriptionId( id, index )
: undefined
}
{ ...additionalProps }
/>
<label
className="components-radio-control__label"
htmlFor={ generateOptionId( id, index ) }
>
{ option.label }
</label>
{ !! option.description ? (
<StyledHelp
__nextHasNoMarginBottom
id={ generateOptionDescriptionId( id, index ) }
className="components-radio-control__option-description"
>
{ option.description }
</StyledHelp>
) : null }
</div>
) ) }
</VStack>
{ !! help && (
<StyledHelp
__nextHasNoMarginBottom
id={ generateHelpId( id ) }
className="components-base-control__help"
>
{ help }
</StyledHelp>
) }
</fieldset>
);
}
export default RadioControl;