Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiForm] specify default fullWidth with root component #6229

Merged
merged 13 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions scripts/jest/setup/throw_on_console_error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { format } from 'util'

// Fail if a test ends up `console.error`-ing, e.g. if React logs because of a
// failed prop types check.
console.error = (message) => {
console.error = (message, ...rest) => {
// @see https://github.com/emotion-js/emotion/issues/1105
// This error that Emotion throws doesn't apply to Jest, so
// we're just going to straight up ignore the first/nth-child warning
Expand All @@ -14,5 +16,5 @@ console.error = (message) => {
return;
}

throw new Error(message);
throw new Error(format(message, ...rest));
};
22 changes: 15 additions & 7 deletions src-docs/src/views/form_layouts/form_layouts_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,21 @@ export const FormLayoutsExample = {
{
title: 'Full-width',
text: (
<p>
Form elements will automatically flex to a max-width of{' '}
<EuiCode>400px</EuiCode>. You can optionally pass the{' '}
<EuiCode>fullWidth</EuiCode> prop to the row and form control to
expand to their container. This should be done rarely and usually you
will only need it for isolated controls like search bars and sliders.
</p>
<>
<p>
Form elements will automatically flex to a max-width of{' '}
<EuiCode>400px</EuiCode>. You can optionally pass the{' '}
<EuiCode>fullWidth</EuiCode> prop to the row and form control to
expand to their container. This should be done rarely and usually
you will only need it for isolated controls like search bars and
sliders.
</p>
<p>
To set all the row and controls in a form to{' '}
<EuiCode>fullWidth</EuiCode>, specify the prop on the root{' '}
<EuiCode>EuiForm</EuiCode> component.
</p>
</>
),
props: {
EuiFormRow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ exports[`EuiDescribedFormGroup is rendered 1`] = `
<EuiFormRow
describedByIds={Array []}
display="row"
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
labelType="label"
Expand Down Expand Up @@ -81,7 +80,6 @@ exports[`EuiDescribedFormGroup props description is not rendered when it's not p
<EuiFormRow
describedByIds={Array []}
display="row"
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
labelType="label"
Expand Down Expand Up @@ -181,7 +179,6 @@ exports[`EuiDescribedFormGroup props gutterSize is rendered 1`] = `
<EuiFormRow
describedByIds={Array []}
display="row"
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
labelType="label"
Expand Down Expand Up @@ -232,7 +229,6 @@ exports[`EuiDescribedFormGroup props props for the flex item containers are pass
<EuiFormRow
describedByIds={Array []}
display="row"
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
labelType="label"
Expand Down Expand Up @@ -332,7 +328,6 @@ exports[`EuiDescribedFormGroup props titleSize is rendered 1`] = `
<EuiFormRow
describedByIds={Array []}
display="row"
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
labelType="label"
Expand Down Expand Up @@ -388,7 +383,6 @@ exports[`EuiDescribedFormGroup ties together parts for accessibility 1`] = `
"Error two",
]
}
fullWidth={false}
hasChildLabel={true}
hasEmptyLabelSpace={false}
helpText="Help text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { shallow, render } from 'enzyme';
import { requiredProps } from '../../../test';

import { EuiForm } from '../form';
import { EuiFormRow } from '../form_row';
import { EuiDescribedFormGroup } from './described_form_group';

Expand Down Expand Up @@ -127,4 +128,24 @@ describe('EuiDescribedFormGroup', () => {
expect(component).toMatchSnapshot();
});
});

describe('inherits', () => {
test('fullWidth from <EuiForm />', () => {
const component = render(
<EuiForm fullWidth>
<EuiDescribedFormGroup {...props} />
</EuiForm>
);

if (
!component
.find('.euiDescribedFormGroup')
.hasClass('euiDescribedFormGroup--fullWidth')
) {
throw new Error(
'expected EuiDescribedFormGroup to inherit fullWidth from EuiForm'
);
}
});
});
});
43 changes: 24 additions & 19 deletions src/components/form/described_form_group/described_form_group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
EuiFlexGroupGutterSize,
EuiFlexItemProps,
} from '../../flex';
import { useFormContext } from '../eui_form_context';

export type EuiDescribedFormGroupProps = CommonProps &
Omit<HTMLAttributes<HTMLDivElement>, 'title'> & {
Expand All @@ -33,6 +34,7 @@ export type EuiDescribedFormGroupProps = CommonProps &
gutterSize?: EuiFlexGroupGutterSize;
/**
* Expand to fill 100% of the parent.
* Defaults to `fullWidth` prop of `<EuiForm>`.
* Default max-width is 800px.
*/
fullWidth?: boolean;
Expand Down Expand Up @@ -64,19 +66,24 @@ export type EuiDescribedFormGroupProps = CommonProps &
fieldFlexItemProps?: PropsOf<typeof EuiFlexItem>;
};

export const EuiDescribedFormGroup: FunctionComponent<EuiDescribedFormGroupProps> = ({
children,
className,
gutterSize = 'l',
fullWidth = false,
ratio = 'half',
titleSize = 'xs',
title,
description,
descriptionFlexItemProps,
fieldFlexItemProps,
...rest
}) => {
export const EuiDescribedFormGroup: FunctionComponent<EuiDescribedFormGroupProps> = (
props
) => {
const { defaultFullWidth } = useFormContext();

const {
children,
className,
gutterSize = 'l',
fullWidth = defaultFullWidth,
ratio = 'half',
titleSize = 'xs',
title,
description,
descriptionFlexItemProps,
fieldFlexItemProps,
...rest
} = props;
const classes = classNames(
'euiDescribedFormGroup',
{
Expand All @@ -93,18 +100,16 @@ export const EuiDescribedFormGroup: FunctionComponent<EuiDescribedFormGroupProps
let renderedDescription: ReactNode;

if (description) {
// If the description is just a string, wrap it in a paragraph element
if (typeof description === 'string') {
description = <p>{description}</p>;
}

renderedDescription = (
<EuiText
size="s"
color="subdued"
className="euiDescribedFormGroup__description"
>
{description}
{
// If the description is just a string, wrap it in a paragraph element
typeof description === 'string' ? <p>{description}</p> : description
}
</EuiText>
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/components/form/eui_form_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';

export interface FormContextValue {
defaultFullWidth: boolean;
}

export const FormContext = React.createContext<FormContextValue>({
defaultFullWidth: false,
});

export function useFormContext() {
return React.useContext(FormContext);
}
19 changes: 19 additions & 0 deletions src/components/form/field_number/field_number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../../test/required_props';

import { EuiForm } from '../form';
import { EuiFieldNumber } from './field_number';

jest.mock('../form_control_layout', () => {
Expand Down Expand Up @@ -89,4 +90,22 @@ describe('EuiFieldNumber', () => {
});
});
});

describe('inherits', () => {
test('fullWidth from <EuiForm />', () => {
const component = render(
<EuiForm fullWidth>
<EuiFieldNumber />
</EuiForm>
);

if (
!component.find('.euiFieldNumber').hasClass('euiFieldNumber--fullWidth')
) {
throw new Error(
'expected EuiFieldNumber to inherit fullWidth from EuiForm'
);
}
});
});
});
46 changes: 26 additions & 20 deletions src/components/form/field_number/field_number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { EuiValidatableControl } from '../validatable_control';

import { IconType } from '../../icon';
import { useFormContext } from '../eui_form_context';
import { getFormControlClassNameForIconCount } from '../form_control_layout/_num_icons';

export type EuiFieldNumberProps = Omit<
Expand Down Expand Up @@ -64,26 +65,31 @@ export type EuiFieldNumberProps = Omit<
compressed?: boolean;
};

export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = ({
className,
icon,
id,
placeholder,
name,
min,
max,
value,
isInvalid,
fullWidth = false,
isLoading = false,
compressed = false,
prepend,
append,
inputRef,
readOnly,
controlOnly,
...rest
}) => {
export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
props
) => {
const { defaultFullWidth } = useFormContext();
const {
className,
icon,
id,
placeholder,
name,
min,
max,
value,
isInvalid,
fullWidth = defaultFullWidth,
isLoading = false,
compressed = false,
prepend,
append,
inputRef,
readOnly,
controlOnly,
...rest
} = props;

const numIconsClass = getFormControlClassNameForIconCount({
isInvalid,
isLoading,
Expand Down
21 changes: 21 additions & 0 deletions src/components/form/field_password/field_password.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import { render, mount } from 'enzyme';
import { requiredProps } from '../../../test/required_props';

import { EuiForm } from '../form';
import { EuiFieldPassword, EuiFieldPasswordProps } from './field_password';

jest.mock('../validatable_control', () => ({
Expand Down Expand Up @@ -135,4 +136,24 @@ describe('EuiFieldPassword', () => {
});
});
});

describe('inherits', () => {
test('fullWidth from <EuiForm />', () => {
const component = render(
<EuiForm fullWidth>
<EuiFieldPassword />
</EuiForm>
);

if (
!component
.find('.euiFieldPassword')
.hasClass('euiFieldPassword--fullWidth')
) {
throw new Error(
'expected EuiFieldPassword to inherit fullWidth from EuiForm'
);
}
});
});
});
Loading