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

Fix FormTabHeader Error State Display #5984

Merged
merged 2 commits into from
Mar 3, 2021
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
12 changes: 9 additions & 3 deletions packages/ra-ui-materialui/src/form/FormTabHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
import { useTranslate, useFormGroup } from 'ra-core';
import { useTabbedFormViewStyles } from './TabbedFormView';
import { ClassesOverride } from '../types';
import { useFormState } from 'react-final-form';

export const FormTabHeader = ({
classes,
Expand All @@ -19,6 +20,7 @@ export const FormTabHeader = ({
}: FormTabHeaderProps): ReactElement => {
const translate = useTranslate();
const location = useLocation();
const { submitFailed } = useFormState(UseFormStateOptions);
const formGroup = useFormGroup(value.toString());
const propsForLink = {
component: Link,
Expand All @@ -32,9 +34,7 @@ export const FormTabHeader = ({
icon={icon}
className={classnames('form-tab', className, {
[classes.errorTabButton]:
formGroup.invalid &&
formGroup.touched &&
location.pathname !== value,
formGroup.invalid && (formGroup.touched || submitFailed),
})}
{...(syncWithLocation ? propsForLink : {})} // to avoid TypeScript screams, see https://github.com/mui-org/material-ui/issues/9106#issuecomment-451270521
id={`tabheader-${value}`}
Expand All @@ -44,6 +44,12 @@ export const FormTabHeader = ({
);
};

const UseFormStateOptions = {
subscription: {
submitFailed: true,
},
};

interface FormTabHeaderProps {
basePath?: string;
className?: string;
Expand Down
37 changes: 34 additions & 3 deletions packages/ra-ui-materialui/src/form/TabbedForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,37 @@ describe('<TabbedForm />', () => {
expect(tabs[1].classList.contains('error')).toEqual(true);
});

it('should not set the style of an active Tab button with errors', () => {
it('should set the style of an active Tab button with errors', () => {
const { getAllByRole, getByLabelText } = renderWithRedux(
<MemoryRouter initialEntries={['/posts/1']} initialIndex={0}>
<SaveContextProvider value={saveContextValue}>
<TabbedForm
classes={{ errorTabButton: 'error' }}
resource="posts"
>
<FormTab label="tab1">
<TextInput source="title" validate={required()} />
</FormTab>
<FormTab label="tab2">
<TextInput
source="description"
validate={required()}
/>
</FormTab>
</TabbedForm>
</SaveContextProvider>
</MemoryRouter>
);

const tabs = getAllByRole('tab');
fireEvent.click(tabs[1]);
const input = getByLabelText('resources.posts.fields.description *');
fireEvent.blur(input);
expect(tabs[0].classList.contains('error')).toEqual(false);
expect(tabs[1].classList.contains('error')).toEqual(true);
});

it('should set the style of any Tab button with errors on submit', () => {
const { getAllByRole, getByLabelText } = renderWithRedux(
<MemoryRouter initialEntries={['/posts/1']} initialIndex={0}>
<SaveContextProvider value={saveContextValue}>
Expand All @@ -133,9 +163,10 @@ describe('<TabbedForm />', () => {
const tabs = getAllByRole('tab');
fireEvent.click(tabs[1]);
const input = getByLabelText('resources.posts.fields.description');
fireEvent.change(input, { target: { value: 'foo' } });
fireEvent.blur(input);
expect(tabs[0].classList.contains('error')).toEqual(false);
fireEvent.change(input, { target: { value: 'fooooooooo' } });
fireEvent.click(getByLabelText('ra.action.save'));
expect(tabs[0].classList.contains('error')).toEqual(true);
expect(tabs[1].classList.contains('error')).toEqual(false);
});

Expand Down