-
Notifications
You must be signed in to change notification settings - Fork 4k
/
component.tsx
168 lines (159 loc) · 4.45 KB
/
component.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
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';
// eslint-disable-next-line no-restricted-imports
import { RadioGroup, useRadioState } from 'reakit';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useRef, useMemo } from '@wordpress/element';
import {
useMergeRefs,
useInstanceId,
usePrevious,
useResizeObserver,
} from '@wordpress/compose';
/**
* Internal dependencies
*/
import {
contextConnect,
useContextSystem,
WordPressComponentProps,
} from '../../ui/context';
import { useUpdateEffect, useCx } from '../../utils/hooks';
import { View } from '../../view';
import BaseControl from '../../base-control';
import type { ToggleGroupControlProps } from '../types';
import ToggleGroupControlBackdrop from './toggle-group-control-backdrop';
import ToggleGroupControlContext from '../context';
import { VisualLabelWrapper } from './styles';
import * as styles from './styles';
const noop = () => {};
function UnconnectedToggleGroupControl(
props: WordPressComponentProps< ToggleGroupControlProps, 'input', false >,
forwardedRef: ForwardedRef< any >
) {
const {
__nextHasNoMarginBottom = false,
className,
isAdaptiveWidth = false,
isBlock = false,
__experimentalIsBorderless = false,
label,
hideLabelFromVision = false,
help,
onChange = noop,
size = 'default',
value,
children,
...otherProps
} = useContextSystem( props, 'ToggleGroupControl' );
const cx = useCx();
const containerRef = useRef();
const [ resizeListener, sizes ] = useResizeObserver();
const baseId = useInstanceId(
ToggleGroupControl,
'toggle-group-control'
).toString();
const radio = useRadioState( {
baseId,
state: value,
} );
const previousValue = usePrevious( value );
// Propagate radio.state change.
useUpdateEffect( () => {
// Avoid calling onChange if radio state changed
// from incoming value.
if ( previousValue !== radio.state ) {
onChange( radio.state );
}
}, [ radio.state ] );
// Sync incoming value with radio.state.
useUpdateEffect( () => {
if ( value !== radio.state ) {
radio.setState( value );
}
}, [ value ] );
const classes = useMemo(
() =>
cx(
styles.ToggleGroupControl( { size } ),
! __experimentalIsBorderless && styles.border,
isBlock && styles.block,
className
),
[ className, cx, isBlock, __experimentalIsBorderless, size ]
);
return (
<BaseControl
help={ help }
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
>
<ToggleGroupControlContext.Provider
value={ { ...radio, isBlock: ! isAdaptiveWidth, size } }
>
{ ! hideLabelFromVision && (
<VisualLabelWrapper>
<BaseControl.VisualLabel>
{ label }
</BaseControl.VisualLabel>
</VisualLabelWrapper>
) }
<RadioGroup
{ ...radio }
aria-label={ label }
as={ View }
className={ classes }
{ ...otherProps }
ref={ useMergeRefs( [ containerRef, forwardedRef ] ) }
>
{ resizeListener }
<ToggleGroupControlBackdrop
{ ...radio }
containerRef={ containerRef }
containerWidth={ sizes.width }
isAdaptiveWidth={ isAdaptiveWidth }
/>
{ children }
</RadioGroup>
</ToggleGroupControlContext.Provider>
</BaseControl>
);
}
/**
* `ToggleGroupControl` is a form component that lets users choose options
* represented in horizontal segments. To render options for this control use
* `ToggleGroupControlOption` component.
*
* This component is intended for selecting a single persistent value from a set of options,
* similar to a how a radio button group would work. If you simply want a toggle to switch between views,
* use a `TabPanel` instead.
*
* Only use this control when you know for sure the labels of items inside won't
* wrap. For items with longer labels, you can consider a `SelectControl` or a
* `CustomSelectControl` component instead.
*
* ```jsx
* import {
* __experimentalToggleGroupControl as ToggleGroupControl,
* __experimentalToggleGroupControlOption as ToggleGroupControlOption,
* } from '@wordpress/components';
*
* function Example() {
* return (
* <ToggleGroupControl label="my label" value="vertical" isBlock>
* <ToggleGroupControlOption value="horizontal" label="Horizontal" />
* <ToggleGroupControlOption value="vertical" label="Vertical" />
* </ToggleGroupControl>
* );
* }
* ```
*/
export const ToggleGroupControl = contextConnect(
UnconnectedToggleGroupControl,
'ToggleGroupControl'
);
export default ToggleGroupControl;