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 : {
@@ -94,6 +69,10 @@ const SwitchElement = tasty({
9469 } ,
9570 transition : 'theme' ,
9671 cursor : 'pointer' ,
72+ shadow : {
73+ '' : '0 0 0 0 #clear' ,
74+ invalid : '0 0 0 1bw #white, 0 0 0 1ow #danger' ,
75+ } ,
9776
9877 Thumb : {
9978 position : 'absolute' ,
@@ -152,29 +131,19 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
152131 isDisabled = false ,
153132 children,
154133 label,
155- labelProps,
156134 labelStyles,
157135 insideForm,
158136 isLoading,
159137 labelPosition,
160138 inputStyles,
161139 validationState,
162140 size = 'large' ,
163- ...otherProps
164141 } = props ;
165142
166143 let styles = extractStyles ( props , OUTER_STYLES ) ;
167144
168145 inputStyles = extractStyles ( props , BLOCK_STYLES , inputStyles ) ;
169146
170- labelStyles = useMemo (
171- ( ) => ( {
172- ...( insideForm ? LABEL_STYLES : INLINE_LABEL_STYLES ) ,
173- ...labelStyles ,
174- } ) ,
175- [ insideForm , labelStyles ] ,
176- ) ;
177-
178147 let { isFocused, focusProps } = useFocus ( { isDisabled } , true ) ;
179148 let { hoverProps, isHovered } = useHover ( { isDisabled } ) ;
180149
@@ -205,7 +174,7 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
205174 } ;
206175
207176 const switchField = (
208- < SwitchWrapperElement mods = { mods } data-size = { size } >
177+ < SwitchWrapperElement mods = { mods } data-size = { size } { ... hoverProps } >
209178 < HiddenInput
210179 data-qa = "HiddenInput"
211180 { ...mergeProps ( inputProps , focusProps ) }
@@ -220,47 +189,22 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
220189 < div data-element = "Thumb" aria-hidden = "true" />
221190 </ SwitchElement >
222191 { children ? < Text nowrap > { children } </ Text > : null }
192+ { isLoading ? (
193+ < >
194+ { label ? < > </ > : null }
195+ < LoadingIcon />
196+ </ >
197+ ) : null }
223198 </ SwitchWrapperElement >
224199 ) ;
225200
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- ) ;
201+ return wrapWithField ( switchField , domRef , {
202+ ...props ,
203+ children : null ,
204+ labelStyles,
205+ inputStyles,
206+ styles,
207+ } ) ;
264208}
265209
266210/**
0 commit comments