Skip to content

Commit f843aaf

Browse files
committed
fix(Checkbox): wrap in a field
1 parent de8533f commit f843aaf

File tree

6 files changed

+77
-113
lines changed

6 files changed

+77
-113
lines changed

.changeset/pretty-goats-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Always wrap Switch in a Field.

.changeset/thick-horses-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Always wrap Checkbox in a Field except checkbox group case.

src/components/fields/Checkbox/Checkbox.stories.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,33 @@ export default {
2121
},
2222
};
2323

24-
const Template = (props) => (
25-
<Flow gap="2x">
26-
<Checkbox
27-
aria-label="Checkbox"
28-
{...props}
29-
defaultSelected={true}
30-
onChange={(query) => console.log('onChange event', query)}
31-
/>
32-
<Checkbox
33-
aria-label="Checkbox"
34-
{...props}
35-
defaultSelected={false}
36-
onChange={(query) => console.log('onChange event', query)}
37-
/>
38-
</Flow>
39-
);
24+
const Template = (props) => {
25+
return (
26+
<Flow gap="2x">
27+
<Checkbox
28+
aria-label="Checkbox"
29+
{...props}
30+
defaultSelected={true}
31+
onChange={(query) => console.log('onChange event', query)}
32+
/>
33+
<Checkbox
34+
aria-label="Checkbox"
35+
{...props}
36+
defaultSelected={false}
37+
onChange={(query) => console.log('onChange event', query)}
38+
/>
39+
</Flow>
40+
);
41+
};
4042

4143
export const Default = Template.bind({});
4244
Default.args = { children: 'Checkbox' };
4345

44-
export const WithoutValue = Template.bind({});
45-
WithoutValue.args = {
46-
label: '',
47-
};
46+
export const WithLabel = Template.bind({});
47+
WithLabel.args = { label: 'Checkbox' };
48+
49+
export const WithoutLabel = Template.bind({});
50+
WithoutLabel.args = {};
4851

4952
export const Intermediate = Template.bind({});
5053
Intermediate.args = {

src/components/fields/Checkbox/Checkbox.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ function Checkbox(
165165

166166
labelStyles = useMemo(
167167
() => ({
168-
...(insideForm ? LABEL_STYLES : INLINE_LABEL_STYLES),
168+
...(!groupState ? LABEL_STYLES : INLINE_LABEL_STYLES),
169169
...labelStyles,
170170
}),
171-
[insideForm, labelStyles],
171+
[groupState, labelStyles],
172172
);
173173

174174
let { isFocused, focusProps } = useFocus({ isDisabled }, true);
@@ -234,12 +234,8 @@ function Checkbox(
234234
'inside-form': insideForm,
235235
};
236236

237-
const checkboxField = (
238-
<CheckboxWrapperElement
239-
isHidden={isHidden}
240-
mods={mods}
241-
styles={{ position: 'relative' }}
242-
>
237+
const checkbox = (
238+
<>
243239
<HiddenInput
244240
data-qa="HiddenInput"
245241
{...mergeProps(inputProps, focusProps)}
@@ -248,11 +244,17 @@ function Checkbox(
248244
<CheckboxElement qa={qa || 'Checkbox'} mods={mods} styles={inputStyles}>
249245
{markIcon}
250246
</CheckboxElement>
247+
</>
248+
);
249+
250+
const checkboxField = (
251+
<CheckboxWrapperElement isHidden={isHidden} mods={mods}>
252+
{checkbox}
251253
{children && <Text nowrap>{children}</Text>}
252254
</CheckboxWrapperElement>
253255
);
254256

255-
if (insideForm && !groupState) {
257+
if (!groupState) {
256258
return wrapWithField(checkboxField, domRef, {
257259
...props,
258260
children: null,
@@ -264,15 +266,14 @@ function Checkbox(
264266

265267
return (
266268
<CheckboxWrapperElement
267-
as="label"
268269
styles={styles}
269270
isHidden={isHidden}
270271
{...hoverProps}
271272
{...filterBaseProps(otherProps)}
272273
ref={domRef}
273274
>
274-
{checkboxField}
275-
{label ? (
275+
{checkbox}
276+
{label ?? children ? (
276277
<Element
277278
styles={labelStyles}
278279
mods={{
@@ -282,7 +283,7 @@ function Checkbox(
282283
}}
283284
{...filterBaseProps(labelProps)}
284285
>
285-
{label}
286+
{label ?? children}
286287
</Element>
287288
) : null}
288289
</CheckboxWrapperElement>

src/components/fields/Switch/Switch.stories.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,31 @@ const Template = (args) => (
3434

3535
export const Default = Template.bind({});
3636
Default.args = {
37+
children: 'Switch',
38+
};
39+
40+
export const WithLabel = Template.bind({});
41+
WithLabel.args = {
3742
label: 'Switch',
3843
};
3944

4045
export const Small = Template.bind({});
4146
Small.args = {
42-
label: 'Switch',
47+
children: 'Switch',
4348
size: 'small',
4449
};
4550

4651
export const WithoutLabel = Template.bind({});
47-
WithoutLabel.args = {
48-
label: '',
49-
};
52+
WithoutLabel.args = {};
5053

5154
export const Disabled = Template.bind({});
5255
Disabled.args = {
56+
children: 'Switch',
5357
isDisabled: true,
5458
};
59+
60+
export const Loading = Template.bind({});
61+
Loading.args = {
62+
children: 'Switch',
63+
isLoading: true,
64+
};

src/components/fields/Switch/Switch.tsx

Lines changed: 16 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forwardRef, useMemo, useRef } from 'react';
1+
import { forwardRef, useRef } from 'react';
22
import { useFocusableRef } from '@react-spectrum/utils';
33
import { useSwitch, useHover, AriaSwitchProps } from 'react-aria';
44
import { useToggleState } from 'react-stately';
@@ -8,9 +8,7 @@ import {
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';
3527
import { LoadingIcon } from '../../../icons';
3628

3729
const 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-
6944
const 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 ? <>&nbsp;</> : 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 ? <>&nbsp;</> : 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

Comments
 (0)