1- import { forwardRef , useMemo , useRef } from 'react' ;
1+ import { forwardRef , useRef } from 'react' ;
22import { useFocusableRef } from '@react-spectrum/utils' ;
33import { useSwitch , useHover , AriaSwitchProps } from 'react-aria' ;
44import { useToggleState } from 'react-stately' ;
88 BaseProps ,
99 BLOCK_STYLES ,
1010 BlockStyleProps ,
11- Element ,
1211 extractStyles ,
13- filterBaseProps ,
1412 OUTER_STYLES ,
1513 OuterStyleProps ,
1614 Styles ,
@@ -25,13 +23,7 @@ import {
2523 castNullableIsSelected ,
2624 WithNullableSelected ,
2725} from '../../../utils/react/nullableValue' ;
28- import {
29- useFieldProps ,
30- useFormProps ,
31- wrapWithField ,
32- INLINE_LABEL_STYLES ,
33- LABEL_STYLES ,
34- } from '../../form' ;
26+ import { useFieldProps , useFormProps , wrapWithField } from '../../form' ;
3527import { LoadingIcon } from '../../../icons' ;
3628
3729const SwitchWrapperElement = tasty ( {
@@ -49,23 +41,6 @@ const SwitchWrapperElement = tasty({
4941 } ,
5042} ) ;
5143
52- const SwitchLabelElement = tasty ( {
53- as : 'label' ,
54- qa : 'SwitchLabel' ,
55- styles : {
56- position : 'relative' ,
57- display : 'flex' ,
58- placeItems : 'center' ,
59- gap : '1x' ,
60- flow : 'row' ,
61- preset : 'input' ,
62- width : 'min-content' ,
63- cursor : 'pointer' ,
64- verticalAlign : 'baseline' ,
65- color : '#dark-02' ,
66- } ,
67- } ) ;
68-
6944const SwitchElement = tasty ( {
7045 qa : 'Switch' ,
7146 styles : {
@@ -152,29 +127,19 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
152127 isDisabled = false ,
153128 children,
154129 label,
155- labelProps,
156130 labelStyles,
157131 insideForm,
158132 isLoading,
159133 labelPosition,
160134 inputStyles,
161135 validationState,
162136 size = 'large' ,
163- ...otherProps
164137 } = props ;
165138
166139 let styles = extractStyles ( props , OUTER_STYLES ) ;
167140
168141 inputStyles = extractStyles ( props , BLOCK_STYLES , inputStyles ) ;
169142
170- labelStyles = useMemo (
171- ( ) => ( {
172- ...( insideForm ? LABEL_STYLES : INLINE_LABEL_STYLES ) ,
173- ...labelStyles ,
174- } ) ,
175- [ insideForm , labelStyles ] ,
176- ) ;
177-
178143 let { isFocused, focusProps } = useFocus ( { isDisabled } , true ) ;
179144 let { hoverProps, isHovered } = useHover ( { isDisabled } ) ;
180145
@@ -205,7 +170,7 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
205170 } ;
206171
207172 const switchField = (
208- < SwitchWrapperElement mods = { mods } data-size = { size } >
173+ < SwitchWrapperElement mods = { mods } data-size = { size } { ... hoverProps } >
209174 < HiddenInput
210175 data-qa = "HiddenInput"
211176 { ...mergeProps ( inputProps , focusProps ) }
@@ -220,47 +185,22 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
220185 < div data-element = "Thumb" aria-hidden = "true" />
221186 </ SwitchElement >
222187 { children ? < Text nowrap > { children } </ Text > : null }
188+ { isLoading ? (
189+ < >
190+ { label ? < > </ > : null }
191+ < LoadingIcon />
192+ </ >
193+ ) : null }
223194 </ SwitchWrapperElement >
224195 ) ;
225196
226- if ( insideForm ) {
227- return wrapWithField ( switchField , domRef , {
228- ...props ,
229- children : null ,
230- labelStyles,
231- inputStyles,
232- styles,
233- } ) ;
234- }
235-
236- return (
237- < SwitchLabelElement
238- styles = { styles }
239- mods = { mods }
240- { ...hoverProps }
241- { ...filterBaseProps ( otherProps ) }
242- ref = { domRef }
243- >
244- { switchField }
245- { label ? (
246- < Element
247- styles = { labelStyles }
248- mods = { {
249- disabled : isDisabled ,
250- } }
251- { ...filterBaseProps ( labelProps ) }
252- >
253- { label }
254- { isLoading ? (
255- < >
256- { label ? < > </ > : null }
257- < LoadingIcon />
258- </ >
259- ) : null }
260- </ Element >
261- ) : null }
262- </ SwitchLabelElement >
263- ) ;
197+ return wrapWithField ( switchField , domRef , {
198+ ...props ,
199+ children : null ,
200+ labelStyles,
201+ inputStyles,
202+ styles,
203+ } ) ;
264204}
265205
266206/**
0 commit comments