Skip to content

Commit

Permalink
[EuiForm] Added invalidCallout prop to allow conditional rendering …
Browse files Browse the repository at this point in the history
…of error callout (#3585)

Fixes #478
  • Loading branch information
shrey authored Jun 11, 2020
1 parent e0f79ff commit 3b6bd40
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
## [`25.0.0`](https://github.com/elastic/eui/tree/v25.0.0)

- 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const FormValidationExample = {
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.
passed as an array in case you need to list more than one. You can
also hide the callout by passing
<EuiCode>invalidCallout=&ldquo;none&ldquo;</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
Expand Up @@ -15,3 +15,11 @@ exports[`EuiForm renders a form element 1`] = `
data-test-subj="test subject string"
/>
`;

exports[`EuiForm renders without error callout when invalidCallout is "none" 1`] = `
<div
aria-label="aria-label"
class="euiForm testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
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

0 comments on commit 3b6bd40

Please sign in to comment.