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

Added component prop to EuiForm #3010

Merged
merged 15 commits into from
Mar 12, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Exported `dateFormatAliases` as a part of the public API ([#3043](https://github.com/elastic/eui/pull/3043))
- Exported `EuiTextProps` type definition ([#3039](https://github.com/elastic/eui/pull/3039))
- Added a `component` prop to `EuiForm` to render a `<form>`([#3010](https://github.com/elastic/eui/pull/3010))
- Removed `role` attribute from `EuiImage`([#3036](https://github.com/elastic/eui/pull/3036))
- Added `prepend` and `append` ability to `EuiComboBox` single selection only ([#3003](https://github.com/elastic/eui/pull/3003))
- Added `onColumnResize` prop to `EuiDataGrid` of type `EuiDataGridOnColumnResizeHandler` that gets called when column changes it's size ([#2963](https://github.com/elastic/eui/pull/2963))
Expand Down
4 changes: 3 additions & 1 deletion src-docs/src/views/form_layouts/form_layouts_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export const FormLayoutsExample = {
Use the <EuiCode>EuiFormRow</EuiCode> component to easily associate
form components with labels, help text, and error text. Use the{' '}
<EuiCode>EuiForm</EuiCode> component to group{' '}
<EuiCode>EuiFormRow</EuiCode>s.
<EuiCode>EuiFormRow</EuiCode>s. By default EuiForm will render as a
simple div unless you pass{' '}
<EuiCode>component=&quot;form&quot;</EuiCode>.
</p>
),
props: {
Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/form_layouts/form_rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class extends Component {

render() {
return (
<EuiForm>
<EuiForm component="form">
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
<EuiFormRow label="Text field" helpText="I am some friendly help text.">
<EuiFieldText name="first" />
</EuiFormRow>
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 @@ -7,3 +7,11 @@ exports[`EuiForm is rendered 1`] = `
data-test-subj="test subject string"
/>
`;

exports[`EuiForm renders a form element 1`] = `
<form
aria-label="aria-label"
class="euiForm testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
6 changes: 6 additions & 0 deletions src/components/form/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ describe('EuiForm', () => {

expect(component).toMatchSnapshot();
});

test('renders a form element', () => {
const component = render(<EuiForm {...requiredProps} component="form" />);

expect(component).toMatchSnapshot();
});
});
25 changes: 20 additions & 5 deletions src/components/form/form.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React, { FunctionComponent, ReactNode, HTMLAttributes } from 'react';
import React, {
FunctionComponent,
ReactNode,
HTMLAttributes,
FormHTMLAttributes,
} from 'react';
import classNames from 'classnames';
import { EuiCallOut } from '../call_out';
import { EuiI18n } from '../i18n';
import { CommonProps } from '../common';
import { CommonProps, ExclusiveUnion } from '../common';

export type EuiFormProps = CommonProps &
HTMLAttributes<HTMLDivElement> & {
ExclusiveUnion<
{ component: 'form' } & FormHTMLAttributes<HTMLFormElement>,
{ component?: 'div' } & HTMLAttributes<HTMLDivElement>
> & {
isInvalid?: boolean;
/**
* Which HTML element to render `div` or `form`
*/
component?: 'form' | 'div';
error?: ReactNode | ReactNode[];
};

Expand All @@ -15,6 +27,7 @@ export const EuiForm: FunctionComponent<EuiFormProps> = ({
className,
isInvalid,
error,
component = 'div',
...rest
}) => {
const classes = classNames('euiForm', className);
Expand Down Expand Up @@ -53,10 +66,12 @@ export const EuiForm: FunctionComponent<EuiFormProps> = ({
);
}

const Element = component;

return (
<div className={classes} {...rest}>
<Element className={classes} {...rest as HTMLAttributes<HTMLElement>}>
{optionalErrorAlert}
{children}
</div>
</Element>
);
};