Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Button/__tests__/Button.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
propTests,
} from '../../../../tests/playwright';
import {
ButtonForTest,
ButtonForRefTest,
ButtonInVerticalFormLayoutForTest,
ButtonForTest,
ButtonInHorizontalFormLayoutForTest,
ButtonInVerticalFormLayoutForTest,
} from './Button.story';

test.describe('Button', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/components/Button/__tests__/Button.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
} from 'react';
import type { ButtonHTMLAttributes } from 'react';
import { FormLayout } from '../../FormLayout';
import { TextField } from '../../TextField';
import { Button } from '..';

// Types for story component will be improved when we have full TypeScript support
Expand Down Expand Up @@ -46,6 +47,7 @@ export const ButtonInVerticalFormLayoutForTest = ({
...props
}: ButtonForTestProps) => (
<FormLayout fieldLayout="vertical">
<TextField label="Text field" />
<Button
label="Button"
{...props}
Expand All @@ -57,6 +59,7 @@ export const ButtonInHorizontalFormLayoutForTest = ({
...props
}: ButtonForTestProps) => (
<FormLayout fieldLayout="horizontal">
<TextField label="Text field" />
<Button
label="Button"
{...props}
Expand Down
12 changes: 10 additions & 2 deletions src/components/CheckboxField/__tests__/CheckboxField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
mixPropTests,
propTests,
} from '../../../../tests/playwright';
import type { CheckboxForFormLayoutTestsProps } from './CheckboxField.story';
import {
CheckboxFieldForTest,
CheckboxFieldForRefTest,
CheckboxFieldForTest,
CheckboxForFormLayoutLabelWidthTests,
CheckboxForFormLayoutTests,
} from './CheckboxField.story';
import type { CheckboxForFormLayoutTestsProps } from './CheckboxField.story';

test.describe('CheckboxField', () => {
test.describe('base', () => {
Expand Down Expand Up @@ -139,6 +140,13 @@ test.describe('CheckboxField', () => {

test.describe('formLayout', () => {
test.describe('visual', () => {
test('labelWidth:string=100px', async ({ mount }) => {
const component = await mount(<CheckboxForFormLayoutLabelWidthTests />);

const screenshot = await component.screenshot();
expect(screenshot).toMatchSnapshot();
});

[
...propTests.layoutPropTest,
].forEach(({
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion src/components/CheckboxField/__tests__/CheckboxField.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import React, {
useRef,
} from 'react';
import type { LabelHTMLAttributes } from 'react';
import { FormLayoutContext } from '../../FormLayout';
import {
FormLayout,
FormLayoutContext,
} from '../../FormLayout';
import { CheckboxField } from '..';

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

export const CheckboxForFormLayoutLabelWidthTests = () => (
<FormLayout fieldLayout="horizontal" labelWidth="100px">
<CheckboxField label={defaultLabel} />
<CheckboxField label="another-test-label" />
</FormLayout>
);

export const CheckboxForFormLayoutTests = ({
layout,
...props
Expand Down
11 changes: 9 additions & 2 deletions src/components/FileInputField/FileInputField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { getRootValidationStateClassName } from '../_helpers/getRootValidationSt
import { resolveContextOrProp } from '../_helpers/resolveContextOrProp';
import { InputGroupContext } from '../InputGroup';
import { Text } from '../Text';
import { FormLayoutContext } from '../FormLayout';
import {
FormLayoutContext,
FormLayoutCustomFieldContext,
} from '../FormLayout';
import styles from './FileInputField.module.scss';

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

const formLayoutContext = useContext(FormLayoutContext);
const formLayoutCustomFieldContext = useContext(FormLayoutCustomFieldContext);
const inputGroupContext = useContext(InputGroupContext);
const translations = useContext(TranslationsContext);

Expand Down Expand Up @@ -172,7 +176,7 @@ export const FileInputField = React.forwardRef((props, ref) => {
<label
className={classNames(
styles.label,
(!isLabelVisible || inputGroupContext) && styles.isLabelHidden,
(!isLabelVisible || inputGroupContext || formLayoutCustomFieldContext) && styles.isLabelHidden,
)}
htmlFor={id}
id={`${id}__labelText`}
Expand Down Expand Up @@ -286,6 +290,9 @@ FileInputField.propTypes = {
/**
* If `false`, the label will be visually hidden (but remains accessible by assistive
* technologies).
*
* Automatically set to `false` when the component is rendered within `FormLayoutCustomField`
* component.
*/
isLabelVisible: PropTypes.bool,
/**
Expand Down
20 changes: 20 additions & 0 deletions src/components/FileInputField/__tests__/FileInputField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from '../../../../tests/playwright';
import type { FileInputFieldForFormLayoutTestsProps } from './FileInputField.story';
import {
FileInputFieldForFormLayoutCustomFieldTests,
FileInputFieldForFormLayoutLabelWidthTests,
FileInputFieldForFormLayoutTests,
FileInputFieldForRefTest,
FileInputFieldForTest,
Expand Down Expand Up @@ -314,6 +316,13 @@ test.describe('FileInputField', () => {

test.describe('formLayout', () => {
test.describe('visual', () => {
test('labelWidth:string=100px', async ({ mount }) => {
const component = await mount(<FileInputFieldForFormLayoutLabelWidthTests />);

const screenshot = await component.screenshot();
expect(screenshot).toMatchSnapshot();
});

[
...propTests.layoutPropTest,
].forEach(({
Expand Down Expand Up @@ -346,4 +355,15 @@ test.describe('FileInputField', () => {
});
});
});

test.describe('formLayoutCustomField', () => {
test.describe('visual', () => {
test('label:hidden', async ({ mount }) => {
const component = await mount(<FileInputFieldForFormLayoutCustomFieldTests />);

const screenshot = await component.screenshot();
expect(screenshot).toMatchSnapshot();
});
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 27 additions & 1 deletion src/components/FileInputField/__tests__/FileInputField.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import React, {
} from 'react';
import type { HTMLAttributes } from 'react';
import { FileInputField } from '..';
import { FormLayoutContext } from '../../FormLayout';
import {
FormLayout,
FormLayoutContext,
FormLayoutCustomFieldContext,
} from '../../FormLayout';

// Types for story component will be improved when we have full TypeScript support
type FileInputFieldForTestProps = HTMLAttributes<HTMLDivElement>;
Expand Down Expand Up @@ -99,6 +103,13 @@ export const FileInputFieldForRefTest = ({
);
};

export const FileInputFieldForFormLayoutLabelWidthTests = () => (
<FormLayout fieldLayout="horizontal" labelWidth="100px">
<FileInputField id="testId" label="FirstLabel" onFilesChanged={() => {}} />
<FileInputField id="anotherTestId" label="SecondLabel" onFilesChanged={() => {}} />
</FormLayout>
);

export const FileInputFieldForFormLayoutTests = ({
layout,
...props
Expand Down Expand Up @@ -126,3 +137,18 @@ export const FileInputFieldForFormLayoutTests = ({
</FormLayoutContext.Provider>
);
};

export const FileInputFieldForFormLayoutCustomFieldTests = ({
...props
}: FileInputFieldForTestProps) => (
<InputWrapper {...props}>
<FormLayoutCustomFieldContext.Provider value>
<FileInputField
id="testId"
label="Attachment"
onFilesChanged={() => {}}
{...props}
/>
</FormLayoutCustomFieldContext.Provider>
</InputWrapper>
);
5 changes: 4 additions & 1 deletion src/components/FormLayout/FormLayoutCustomField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getRootSizeClassName } from '../_helpers/getRootSizeClassName';
import { getRootValidationStateClassName } from '../_helpers/getRootValidationStateClassName';
import { isChildrenEmpty } from '../../helpers/isChildrenEmpty/isChildrenEmpty';
import { FormLayoutContext } from './FormLayoutContext';
import { FormLayoutCustomFieldContext } from './FormLayoutCustomFieldContext';
import styles from './FormLayoutCustomField.module.scss';

const renderLabel = (id, label, labelForId) => {
Expand Down Expand Up @@ -73,7 +74,9 @@ export const FormLayoutCustomField = ({
className={styles.field}
id={id && `${id}__field`}
>
{children}
<FormLayoutCustomFieldContext.Provider value>
Comment thread
adamkudrna marked this conversation as resolved.
{children}
</FormLayoutCustomFieldContext.Provider>
</div>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/FormLayout/FormLayoutCustomFieldContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export const FormLayoutCustomFieldContext = React.createContext(null);
25 changes: 23 additions & 2 deletions src/components/FormLayout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,29 @@ and its styles may affect contained form fields through CSS cascade, don't
forget to mirror the aforementioned properties to the contained form fields too
as API options as such are **not** inherited.

### Nested Fields Labels

When placing box form fields — TextField, TextArea, SelectField, or
FileInputField — inside a FormLayoutCustomField, their labels are hidden
automatically.

```docoff-react-preview
<FormLayout fieldLayout="horizontal" labelWidth="auto">
<FormLayoutCustomField
innerFieldSize="medium"
label="Custom field with invisible label"
>
<TextField
label="This field's label is hidden"
/>
</FormLayoutCustomField>
</FormLayout>
```

ℹ️ Labels are hidden regardless of `fieldLayout`. In a horizontal FormLayout,
the nested field's own label would conflict with the FormLayout grid and
produce incorrect alignment, so it is always hidden for consistency.

### Label Alignment

If you are in a situation with one or more box form fields inside your
Expand All @@ -346,7 +369,6 @@ this task.
label="Custom field label aligned to inner text input"
>
<TextField
isLabelVisible={false}
label="A form element"
placeholder="Text field with invisible label"
/>
Expand Down Expand Up @@ -391,7 +413,6 @@ React.createElement(() => {
>
<TextField
id="my-text-field-custom-accessibility"
isLabelVisible={false}
label="A form element"
placeholder="Text field with invisible label"
/>
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions src/components/FormLayout/__tests__/FormLayout.story.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react';
import type { HTMLAttributes } from 'react';
import {
FormLayout,
FormLayoutCustomField,
} from '..';
import { TextField } from '../../TextField';
import { Button } from '../../Button';
import { CheckboxField } from '../../CheckboxField';
import { FileInputField } from '../../FileInputField';
import { Radio } from '../../Radio';
import { SelectField } from '../../SelectField';
import { TextArea } from '../../TextArea';
import { TextField } from '../../TextField';
import { Toggle } from '../../Toggle';
import {
FormLayout,
FormLayoutCustomField,
} from '..';

// Types for story component will be improved when we have full TypeScript support
type FormLayoutForTestProps = HTMLAttributes<HTMLDivElement>;
Expand Down Expand Up @@ -69,6 +70,7 @@ export const FormLayoutForTest = ({
<TextField label="Label7" />
</FormLayoutCustomField>
<FileInputField label="Attachment label 8" />
<Button label="Submit" />
</FormLayout>
);

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import type { HTMLAttributes } from 'react';
import { TextField } from '../../TextField';
import {
FormLayout,
FormLayoutCustomField,
} from '..';
import { TextField } from '../../TextField';

// Types for story component will be improved when we have full TypeScript support
type FormLayoutForTestProps = HTMLAttributes<HTMLDivElement>;
type FormLayoutCustomFieldForTestProps = FormLayoutForTestProps & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const customFieldLayoutBaseProps = {
};

const customFieldBaseProps = {
isLabelVisible: false,
label: 'Custom field label',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,17 @@ export const customFieldLabelAlignmentPropTest: CustomFieldTestsType = [
{
customFieldLayoutProps: customFieldLayoutBaseProps,
customFieldProps: {
isLabelVisible: true,
label: 'Custom field label',
},
name: 'labelVisible:boolean=true fieldLayout:string=horizontal',
name: 'fieldLayout:string=horizontal',
props: formLayoutHorizontalBaseProps,
},
{
customFieldLayoutProps: customFieldLayoutBaseProps,
customFieldProps: {
isLabelVisible: false,
label: 'Custom field label',
},
name: 'labelVisible:boolean=false fieldLayout:string=horizontal',
props: formLayoutHorizontalBaseProps,
},
{
customFieldLayoutProps: customFieldLayoutBaseProps,
customFieldProps: {
isLabelVisible: true,
label: 'Custom field label',
},
name: 'labelVisible:boolean=true fieldLayout:string=vertical',
props: formLayoutVerticalBaseProps,
},
{
customFieldLayoutProps: customFieldLayoutBaseProps,
customFieldProps: {
isLabelVisible: false,
label: 'Custom field label',
},
name: 'labelVisible:boolean=false fieldLayout:string=vertical',
name: 'fieldLayout:string=vertical',
props: formLayoutVerticalBaseProps,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const customFieldLayoutBaseProps = {
};

const customFieldBaseProps = {
isLabelVisible: false,
label: 'Custom field label',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const customFieldValidationStatesDisabledPropTest: CustomFieldTestsType =
...propTest.props,
},
customFieldProps: {
isLabelVisible: false,
label: 'Custom field label',
},
name: `${propTest.name} disabled:boolean=true`,
Expand Down
Loading
Loading