Skip to content

Commit 071ede9

Browse files
committed
Auto-hide labels of box fields nested in FormLayoutCustomField via context
Creates `FormLayoutCustomFieldContext` and has `FormLayoutCustomField` provide it to its children. `TextField`, `TextArea`, `SelectField`, and `FileInputField` consume the context to automatically hide their labels when rendered inside a `FormLayoutCustomField`, making the behavior a hard rule rather than a recommendation. Updates the `FormLayout` README to reflect the automatic behavior and removes all now-redundant `isLabelVisible={false}` from code examples. Removes the prop from `FormLayoutCustomField` test fixtures and the `FormLayout` story accordingly. Also adds per-component `formLayout` test coverage for `labelWidth` and the new `formLayoutCustomField` visual tests to `TextField`, `TextArea`, `SelectField`, `FileInputField`, `Button`, `CheckboxField`, `Radio`, and `Toggle`.
1 parent 2ea3e1f commit 071ede9

59 files changed

Lines changed: 333 additions & 72 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/components/Button/__tests__/Button.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
propTests,
99
} from '../../../../tests/playwright';
1010
import {
11-
ButtonForTest,
1211
ButtonForRefTest,
13-
ButtonInVerticalFormLayoutForTest,
12+
ButtonForTest,
1413
ButtonInHorizontalFormLayoutForTest,
14+
ButtonInVerticalFormLayoutForTest,
1515
} from './Button.story';
1616

1717
test.describe('Button', () => {
1.59 KB
Loading
1.73 KB
Loading

src/components/Button/__tests__/Button.story.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
} from 'react';
55
import type { ButtonHTMLAttributes } from 'react';
66
import { FormLayout } from '../../FormLayout';
7+
import { TextField } from '../../TextField';
78
import { Button } from '..';
89

910
// Types for story component will be improved when we have full TypeScript support
@@ -46,6 +47,7 @@ export const ButtonInVerticalFormLayoutForTest = ({
4647
...props
4748
}: ButtonForTestProps) => (
4849
<FormLayout fieldLayout="vertical">
50+
<TextField label="Text field" />
4951
<Button
5052
label="Button"
5153
{...props}
@@ -57,6 +59,7 @@ export const ButtonInHorizontalFormLayoutForTest = ({
5759
...props
5860
}: ButtonForTestProps) => (
5961
<FormLayout fieldLayout="horizontal">
62+
<TextField label="Text field" />
6063
<Button
6164
label="Button"
6265
{...props}

src/components/CheckboxField/__tests__/CheckboxField.spec.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import {
77
mixPropTests,
88
propTests,
99
} from '../../../../tests/playwright';
10+
import type { CheckboxForFormLayoutTestsProps } from './CheckboxField.story';
1011
import {
11-
CheckboxFieldForTest,
1212
CheckboxFieldForRefTest,
13+
CheckboxFieldForTest,
14+
CheckboxForFormLayoutLabelWidthTests,
1315
CheckboxForFormLayoutTests,
1416
} from './CheckboxField.story';
15-
import type { CheckboxForFormLayoutTestsProps } from './CheckboxField.story';
1617

1718
test.describe('CheckboxField', () => {
1819
test.describe('base', () => {
@@ -139,6 +140,13 @@ test.describe('CheckboxField', () => {
139140

140141
test.describe('formLayout', () => {
141142
test.describe('visual', () => {
143+
test('labelWidth:string=100px', async ({ mount }) => {
144+
const component = await mount(<CheckboxForFormLayoutLabelWidthTests />);
145+
146+
const screenshot = await component.screenshot();
147+
expect(screenshot).toMatchSnapshot();
148+
});
149+
142150
[
143151
...propTests.layoutPropTest,
144152
].forEach(({
Loading

src/components/CheckboxField/__tests__/CheckboxField.story.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import React, {
44
useRef,
55
} from 'react';
66
import type { LabelHTMLAttributes } from 'react';
7-
import { FormLayoutContext } from '../../FormLayout';
7+
import {
8+
FormLayout,
9+
FormLayoutContext,
10+
} from '../../FormLayout';
811
import { CheckboxField } from '..';
912

1013
// Types for story component will be improved when we have full TypeScript support
@@ -48,6 +51,13 @@ export const CheckboxFieldForRefTest = ({
4851
);
4952
};
5053

54+
export const CheckboxForFormLayoutLabelWidthTests = () => (
55+
<FormLayout fieldLayout="horizontal" labelWidth="100px">
56+
<CheckboxField label={defaultLabel} />
57+
<CheckboxField label="another-test-label" />
58+
</FormLayout>
59+
);
60+
5161
export const CheckboxForFormLayoutTests = ({
5262
layout,
5363
...props

src/components/FileInputField/FileInputField.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import { getRootValidationStateClassName } from '../_helpers/getRootValidationSt
1616
import { resolveContextOrProp } from '../_helpers/resolveContextOrProp';
1717
import { InputGroupContext } from '../InputGroup';
1818
import { Text } from '../Text';
19-
import { FormLayoutContext } from '../FormLayout';
19+
import {
20+
FormLayoutContext,
21+
FormLayoutCustomFieldContext,
22+
} from '../FormLayout';
2023
import styles from './FileInputField.module.scss';
2124

2225
/* istanbul ignore file */
@@ -40,6 +43,7 @@ export const FileInputField = React.forwardRef((props, ref) => {
4043
} = props;
4144

4245
const formLayoutContext = useContext(FormLayoutContext);
46+
const formLayoutCustomFieldContext = useContext(FormLayoutCustomFieldContext);
4347
const inputGroupContext = useContext(InputGroupContext);
4448
const translations = useContext(TranslationsContext);
4549

@@ -172,7 +176,7 @@ export const FileInputField = React.forwardRef((props, ref) => {
172176
<label
173177
className={classNames(
174178
styles.label,
175-
(!isLabelVisible || inputGroupContext) && styles.isLabelHidden,
179+
(!isLabelVisible || inputGroupContext || formLayoutCustomFieldContext) && styles.isLabelHidden,
176180
)}
177181
htmlFor={id}
178182
id={`${id}__labelText`}
@@ -286,6 +290,9 @@ FileInputField.propTypes = {
286290
/**
287291
* If `false`, the label will be visually hidden (but remains accessible by assistive
288292
* technologies).
293+
*
294+
* Automatically set to `false` when the component is rendered within `FormLayoutCustomField`
295+
* component.
289296
*/
290297
isLabelVisible: PropTypes.bool,
291298
/**

src/components/FileInputField/__tests__/FileInputField.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
} from '../../../../tests/playwright';
1010
import type { FileInputFieldForFormLayoutTestsProps } from './FileInputField.story';
1111
import {
12+
FileInputFieldForFormLayoutCustomFieldTests,
13+
FileInputFieldForFormLayoutLabelWidthTests,
1214
FileInputFieldForFormLayoutTests,
1315
FileInputFieldForRefTest,
1416
FileInputFieldForTest,
@@ -314,6 +316,13 @@ test.describe('FileInputField', () => {
314316

315317
test.describe('formLayout', () => {
316318
test.describe('visual', () => {
319+
test('labelWidth:string=100px', async ({ mount }) => {
320+
const component = await mount(<FileInputFieldForFormLayoutLabelWidthTests />);
321+
322+
const screenshot = await component.screenshot();
323+
expect(screenshot).toMatchSnapshot();
324+
});
325+
317326
[
318327
...propTests.layoutPropTest,
319328
].forEach(({
@@ -346,4 +355,15 @@ test.describe('FileInputField', () => {
346355
});
347356
});
348357
});
358+
359+
test.describe('formLayoutCustomField', () => {
360+
test.describe('visual', () => {
361+
test('label:hidden', async ({ mount }) => {
362+
const component = await mount(<FileInputFieldForFormLayoutCustomFieldTests />);
363+
364+
const screenshot = await component.screenshot();
365+
expect(screenshot).toMatchSnapshot();
366+
});
367+
});
368+
});
349369
});
Loading

0 commit comments

Comments
 (0)