Skip to content

Commit c5c6339

Browse files
authored
fix(Checkbox): wrap in a field (#603)
1 parent de8533f commit c5c6339

File tree

7 files changed

+91
-117
lines changed

7 files changed

+91
-117
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: 25 additions & 22 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 = {
@@ -54,11 +57,11 @@ Intermediate.args = {
5457
export const Disabled = Template.bind({});
5558
Disabled.args = {
5659
isDisabled: true,
57-
label: 'Checkbox',
60+
children: 'Checkbox',
5861
};
5962

6063
export const Invalid = Template.bind({});
6164
Invalid.args = {
6265
validationState: 'invalid',
63-
label: 'Checkbox',
66+
children: 'Checkbox',
6467
};

src/components/fields/Checkbox/Checkbox.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const CheckboxElement = tasty({
9090
styles: {
9191
display: 'grid',
9292
placeItems: 'center',
93-
radius: '.25x',
93+
radius: '.5r',
9494
fill: {
9595
'': '#white',
9696
'checked | indeterminate': '#purple-text',
@@ -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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,37 @@ 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 Invalid = Template.bind({});
61+
Invalid.args = {
62+
children: 'Switch',
63+
validationState: 'invalid',
64+
};
65+
66+
export const Loading = Template.bind({});
67+
Loading.args = {
68+
children: 'Switch',
69+
isLoading: true,
70+
};

src/components/fields/Switch/Switch.tsx

Lines changed: 20 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: {
@@ -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 ? <>&nbsp;</> : 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 ? <>&nbsp;</> : 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
/**

src/components/form/Label.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const INTL_MESSAGES = {
5151
export const INLINE_LABEL_STYLES: Styles = {
5252
preset: 't3',
5353
color: {
54-
'': '#dark.85',
54+
'': '#dark-02',
5555
invalid: '#danger-text',
5656
},
5757
whiteSpace: 'nowrap',

0 commit comments

Comments
 (0)