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] Added invalidCallout prop to allow conditional rendering of error callout #3585

Merged
merged 12 commits into from
Jun 11, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added conditional rendering of the title element in `EuiCallOut` to avoid usage of additional space caused by the rendered `<div>` element ([#3549](https://github.com/elastic/eui/pull/3549))
- Added `invalidCallout` prop to `EuiForm` to allow conditional rendering of error callout([#3585](https://github.com/elastic/eui/pull/3585))

**Bug fixes**

Expand Down
8 changes: 5 additions & 3 deletions src-docs/src/views/form_validation/form_validation_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export const FormValidationExample = {
text: (
<p>
Validation is achieved by applying <EuiCode>isInvalid</EuiCode> and
optionally error props onto the <strong>EuiForm</strong> or{' '}
<strong>EuiFormRow</strong> components. Errors are optional and are
passed as an array in case you need to list many errors.
the optional <EuiCode>error<EuiCode> props onto the{' '}
<strong>EuiForm</strong> and <strong>EuiFormRow</strong> components.
Errors are optional and are passed as an array in case you need to
list more than one. You can also hide the callout by passing{' '}
<EuiCode language="ts">{'invalidCallout="none"'}</EuiCode>
</p>
),
source: [
Expand Down
8 changes: 8 additions & 0 deletions src/components/form/__snapshots__/form.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiForm checks if invalidCallout works 1`] = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to update the snapshots again as well.

<div
aria-label="aria-label"
class="euiForm testClass1 testClass2"
data-test-subj="test subject string"
/>
`;

exports[`EuiForm is rendered 1`] = `
<div
aria-label="aria-label"
Expand Down
7 changes: 7 additions & 0 deletions src/components/form/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describe('EuiForm', () => {
test('renders a form element', () => {
const component = render(<EuiForm {...requiredProps} component="form" />);

expect(component).toMatchSnapshot();
});
test('renders without error callout when invalidCallout is "none"', () => {
const component = render(
<EuiForm {...requiredProps} isInvalid invalidCallout="none" />
);

expect(component).toMatchSnapshot();
});
});
9 changes: 7 additions & 2 deletions src/components/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export type EuiFormProps = CommonProps &
*/
component?: 'form' | 'div';
error?: ReactNode | ReactNode[];
/*
* Where to display the callout with the list of errors
*/
invalidCallout?: 'above' | 'none';
};

export const EuiForm: FunctionComponent<EuiFormProps> = ({
Expand All @@ -47,6 +51,7 @@ export const EuiForm: FunctionComponent<EuiFormProps> = ({
isInvalid,
error,
component = 'div',
invalidCallout = 'above',
...rest
}) => {
const classes = classNames('euiForm', className);
Expand All @@ -68,11 +73,11 @@ export const EuiForm: FunctionComponent<EuiFormProps> = ({

let optionalErrorAlert;

if (isInvalid) {
if (isInvalid && invalidCallout === 'above') {
optionalErrorAlert = (
<EuiI18n
token="euiForm.addressFormErrors"
default="Please address the errors in your form.">
default="Please address the highlighted errors.">
{(addressFormErrors: string) => (
<EuiCallOut
className="euiForm__errors"
Expand Down