Skip to content

Commit 76d9fd7

Browse files
authored
fix(Label): for attribute and styles when Switch is inside (#413)
1 parent 20ef7c1 commit 76d9fd7

File tree

8 files changed

+85
-144
lines changed

8 files changed

+85
-144
lines changed

.changeset/ninety-buttons-explain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': patch
3+
---
4+
5+
Fix Field label sizing.

src/components/forms/Checkbox/Checkbox.tsx

+27-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useFocusableRef } from '@react-spectrum/utils';
2-
import { forwardRef, useContext, useRef } from 'react';
2+
import { forwardRef, useContext, useMemo, useRef } from 'react';
33
import { useCheckbox, useCheckboxGroupItem } from '@react-aria/checkbox';
44
import { useHover } from '@react-aria/interactions';
55
import { useToggleState } from '@react-stately/toggle';
@@ -20,13 +20,13 @@ import { mergeProps } from '../../../utils/react';
2020
import { INLINE_LABEL_STYLES, LABEL_STYLES } from '../Label';
2121
import { HiddenInput } from '../../HiddenInput';
2222
import { useFieldProps, useFormProps } from '../Form';
23-
import { FieldWrapper } from '../FieldWrapper';
2423
import { FieldBaseProps } from '../../../shared';
2524
import {
2625
castNullableIsSelected,
2726
WithNullableSelected,
2827
} from '../../../utils/react/nullableValue';
2928
import { Text } from '../../content/Text';
29+
import { wrapWithField } from '../wrapper';
3030

3131
import { CheckboxGroup } from './CheckboxGroup';
3232
import { CheckboxGroupContext } from './context';
@@ -37,7 +37,9 @@ import type { AriaCheckboxProps } from '@react-types/checkbox';
3737
export interface CubeCheckboxProps
3838
extends BaseProps,
3939
AriaCheckboxProps,
40-
FieldBaseProps {}
40+
FieldBaseProps {
41+
inputStyles?: Styles;
42+
}
4143

4244
function CheckOutlined() {
4345
return (
@@ -51,7 +53,7 @@ function CheckOutlined() {
5153
}
5254
function IndeterminateOutline() {
5355
return (
54-
<svg width="9" height="3" fill="none" xmlns="http://www.w3.org/2000/svg">
56+
<svg width="8" height="2" fill="none" xmlns="http://www.w3.org/2000/svg">
5557
<path d="M0 .044v2.001l.026.025h8.063V.044H0z" fill="#fff" />
5658
</svg>
5759
);
@@ -69,6 +71,7 @@ const CheckboxWrapperElement = tasty({
6971
flow: 'row',
7072
preset: 'default',
7173
cursor: 'pointer',
74+
width: 'max max-content',
7275
},
7376
});
7477

@@ -135,32 +138,28 @@ function Checkbox(
135138
isDisabled = false,
136139
insideForm,
137140
isRequired,
138-
autoFocus,
139141
children,
140142
label,
141-
extra,
142143
validationState,
143144
labelProps,
144145
labelStyles,
145146
labelPosition,
146-
necessityLabel,
147-
necessityIndicator,
148-
message,
149-
description,
150-
requiredMark = true,
151-
tooltip,
147+
inputStyles,
152148
isHidden,
153-
labelSuffix,
154149
...otherProps
155150
} = props;
156151

157152
let styles: Styles = extractStyles(props, OUTER_STYLES);
158-
let inputStyles = extractStyles(props, BLOCK_STYLES);
159153

160-
labelStyles = {
161-
...(insideForm && !groupState ? LABEL_STYLES : INLINE_LABEL_STYLES),
162-
...labelStyles,
163-
};
154+
inputStyles = extractStyles(props, BLOCK_STYLES, inputStyles);
155+
156+
labelStyles = useMemo(
157+
() => ({
158+
...(insideForm ? LABEL_STYLES : INLINE_LABEL_STYLES),
159+
...labelStyles,
160+
}),
161+
[insideForm, labelStyles],
162+
);
164163

165164
let { isFocused, focusProps } = useFocus({ isDisabled }, true);
166165
let { hoverProps, isHovered } = useHover({ isDisabled });
@@ -232,44 +231,25 @@ function Checkbox(
232231
styles={{ position: 'relative' }}
233232
>
234233
<HiddenInput
235-
data-qa={qa || 'Checkbox'}
234+
data-qa="HiddenInput"
236235
{...mergeProps(inputProps, focusProps)}
237236
ref={inputRef}
238237
/>
239-
<CheckboxElement qa="Checkbox" mods={mods} styles={inputStyles}>
238+
<CheckboxElement qa={qa || 'Checkbox'} mods={mods} styles={inputStyles}>
240239
{markIcon}
241240
</CheckboxElement>
242-
{children && <Text>{children}</Text>}
241+
{children && <Text nowrap>{children}</Text>}
243242
</CheckboxWrapperElement>
244243
);
245244

246245
if (insideForm && !groupState) {
247-
return (
248-
<FieldWrapper
249-
{...{
250-
as: 'label',
251-
labelPosition,
252-
label,
253-
extra,
254-
styles,
255-
isRequired,
256-
labelStyles,
257-
isHidden,
258-
labelProps,
259-
isDisabled,
260-
validationState,
261-
necessityLabel,
262-
necessityIndicator,
263-
message,
264-
description,
265-
requiredMark,
266-
tooltip,
267-
labelSuffix,
268-
Component: checkboxField,
269-
ref: domRef,
270-
}}
271-
/>
272-
);
246+
return wrapWithField(checkboxField, domRef, {
247+
...props,
248+
children: null,
249+
labelStyles,
250+
inputStyles,
251+
styles,
252+
});
273253
}
274254

275255
return (

src/components/forms/Checkbox/CheckboxGroup.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
Styles,
1313
tasty,
1414
} from '../../../tasty';
15-
import { extractFieldWrapperProps, FieldWrapper } from '../FieldWrapper';
1615
import { FieldBaseProps } from '../../../shared';
1716
import {
1817
castNullableArrayValue,
1918
WithNullableValue,
2019
} from '../../../utils/react/nullableValue';
2120
import { useFieldProps, FormContext, useFormProps } from '../Form';
21+
import { mergeProps } from '../../../utils/react';
22+
import { wrapWithField } from '../wrapper';
2223

2324
import { CheckboxGroupContext } from './context';
2425

@@ -76,14 +77,14 @@ function CheckboxGroup(props: WithNullableValue<CubeCheckboxGroupProps>, ref) {
7677
description,
7778
labelStyles,
7879
tooltip,
80+
labelProps: baseLabelProps,
7981
labelSuffix,
8082
inputStyles,
8183
...otherProps
8284
} = props;
8385
let domRef = useDOMRef(ref);
8486

8587
let styles = extractStyles(otherProps, CONTAINER_STYLES);
86-
let { fieldWrapperProps } = extractFieldWrapperProps(props);
8788

8889
let state = useCheckboxGroupState(props);
8990
let { groupProps, labelProps } = useCheckboxGroup(props, state);
@@ -108,16 +109,13 @@ function CheckboxGroup(props: WithNullableValue<CubeCheckboxGroupProps>, ref) {
108109
</CheckGroupElement>
109110
);
110111

111-
return (
112-
<FieldWrapper
113-
{...fieldWrapperProps}
114-
ref={domRef}
115-
Component={radioGroup}
116-
fieldProps={groupProps}
117-
labelProps={labelProps}
118-
styles={styles}
119-
/>
120-
);
112+
return wrapWithField(radioGroup, domRef, {
113+
...props,
114+
children: null,
115+
fieldProps: groupProps,
116+
labelProps: mergeProps(baseLabelProps, labelProps),
117+
styles,
118+
});
121119
}
122120

123121
/**

src/components/forms/Form/ComplexForm.stories.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -304,25 +304,26 @@ const Template: StoryFn<typeof Form> = (args) => {
304304
{ required: true, message: 'Specify at least a single option' },
305305
]}
306306
>
307-
<CheckboxGroup orientation="horizontal">
307+
<CheckboxGroup orientation="vertical">
308308
<Checkbox value="one">One</Checkbox>
309309
<Checkbox value="two">Two</Checkbox>
310310
<Checkbox value="three">Three</Checkbox>
311311
</CheckboxGroup>
312312
</Field>
313313
<Field name="radioGroup" label="Radio group">
314-
<Radio.Group orientation="horizontal">
314+
<Radio.Group orientation="vertical">
315315
<Radio value="one">One</Radio>
316316
<Radio value="two">Two</Radio>
317317
<Radio value="three">Three</Radio>
318318
</Radio.Group>
319319
</Field>
320-
<Field
320+
<Checkbox
321321
name="checkbox"
322322
rules={[{ required: true, message: 'This field is required' }]}
323+
label="Checkbox field"
323324
>
324-
<Checkbox label="Checkbox field">Checkbox value</Checkbox>
325-
</Field>
325+
Checkbox value
326+
</Checkbox>
326327
<Switch
327328
name="switch"
328329
label="Switch field"

src/components/forms/Form/Field.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export function Field<T extends FieldTypes>(props: CubeFieldProps<T>) {
121121
isDisabled,
122122
isLoading,
123123
styles,
124+
labelProps,
124125
labelPosition = 'top',
125126
labelStyles,
126127
labelSuffix,
@@ -142,6 +143,14 @@ export function Field<T extends FieldTypes>(props: CubeFieldProps<T>) {
142143

143144
if (!child) return null;
144145

146+
if (id) {
147+
if (!labelProps) {
148+
labelProps = {};
149+
}
150+
151+
labelProps.for = id;
152+
}
153+
145154
if (__props.nonInput) {
146155
return (
147156
<LegacyFieldProvider>
@@ -153,6 +162,7 @@ export function Field<T extends FieldTypes>(props: CubeFieldProps<T>) {
153162
necessityLabel={necessityLabel}
154163
isRequired={isRequired}
155164
label={label}
165+
labelProps={labelProps}
156166
extra={extra}
157167
tooltip={tooltip}
158168
message={message}

src/components/forms/Form/use-field/use-field-props.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ export function useFieldProps<
120120
result.labelProps = {};
121121
}
122122

123-
if (result.labelProps) {
124-
result.labelProps.for = result.id;
125-
}
123+
result.labelProps.for = result.id;
126124
}
127125

128126
if (process.env.NODE_ENV === 'development') {

src/components/forms/RadioGroup/RadioGroup.tsx

+10-37
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import {
1313
Styles,
1414
tasty,
1515
} from '../../../tasty';
16-
import { FieldWrapper } from '../FieldWrapper';
1716
import { FieldBaseProps } from '../../../shared';
1817
import {
1918
castNullableStringValue,
2019
WithNullableValue,
2120
} from '../../../utils/react/nullableValue';
21+
import { mergeProps } from '../../../utils/react';
22+
import { wrapWithField } from '../wrapper';
2223

2324
import { RadioContext } from './context';
2425

@@ -60,23 +61,14 @@ function RadioGroup(props: WithNullableValue<CubeRadioGroupProps>, ref) {
6061
let {
6162
isDisabled,
6263
isRequired,
63-
necessityIndicator,
64-
label,
65-
extra,
6664
labelPosition = 'top',
6765
validationState,
6866
children,
6967
orientation,
70-
message,
71-
description,
72-
labelStyles,
73-
requiredMark = true,
74-
tooltip,
75-
isHidden,
7668
styles,
7769
groupStyles,
7870
insideForm,
79-
labelSuffix,
71+
labelProps: baseLabelProps,
8072
isSolid,
8173
...otherProps
8274
} = props;
@@ -117,32 +109,13 @@ function RadioGroup(props: WithNullableValue<CubeRadioGroupProps>, ref) {
117109
</RadioGroupElement>
118110
);
119111

120-
return (
121-
<FieldWrapper
122-
{...{
123-
labelPosition,
124-
label,
125-
extra,
126-
styles,
127-
isRequired,
128-
labelStyles,
129-
necessityIndicator,
130-
labelProps,
131-
fieldProps,
132-
isDisabled,
133-
validationState,
134-
message,
135-
description,
136-
requiredMark,
137-
tooltip,
138-
isHidden,
139-
orientation,
140-
Component: radioGroup,
141-
ref: domRef,
142-
labelSuffix,
143-
}}
144-
/>
145-
);
112+
return wrapWithField(radioGroup, domRef, {
113+
...props,
114+
children: null,
115+
fieldProps,
116+
labelProps: mergeProps(baseLabelProps, labelProps),
117+
styles,
118+
});
146119
}
147120

148121
/**

0 commit comments

Comments
 (0)