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 TabbedForm Does not Display Errors in Hidden Tabs on Submit #5903

Merged
merged 2 commits into from
Feb 15, 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
43 changes: 2 additions & 41 deletions packages/ra-ui-materialui/src/form/FormTab.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import * as React from 'react';
import { FC, ReactElement, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { Link, useLocation } from 'react-router-dom';
import MuiTab from '@material-ui/core/Tab';
import classnames from 'classnames';
import {
FormGroupContextProvider,
useTranslate,
Record,
useFormGroup,
} from 'ra-core';
import { FormGroupContextProvider, Record } from 'ra-core';

import FormInput from './FormInput';
import { FormTabHeader } from './FormTabHeader';

const hiddenStyle = { display: 'none' };

Expand Down Expand Up @@ -73,38 +66,6 @@ const FormTab: FC<FormTabProps> = ({
return intent === 'header' ? renderHeader() : renderContent();
};

export const FormTabHeader = ({
classes,
label,
value,
icon,
className,
...rest
}) => {
const translate = useTranslate();
const location = useLocation();
const formGroup = useFormGroup(value);

return (
<MuiTab
label={translate(label, { _: label })}
value={value}
icon={icon}
className={classnames('form-tab', className, {
[classes.errorTabButton]:
formGroup.invalid &&
formGroup.touched &&
location.pathname !== value,
})}
component={Link}
to={{ ...location, pathname: value }}
id={`tabheader-${value}`}
aria-controls={`tabpanel-${value}`}
{...rest}
/>
);
};

FormTab.propTypes = {
basePath: PropTypes.string,
className: PropTypes.string,
Expand Down
54 changes: 54 additions & 0 deletions packages/ra-ui-materialui/src/form/FormTabHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as React from 'react';
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
import { useEffect } from 'react';
import { Link, useLocation } from 'react-router-dom';
import MuiTab from '@material-ui/core/Tab';
import classnames from 'classnames';
import { useTranslate, useFormGroup } from 'ra-core';
import { useForm } from 'react-final-form';

export const FormTabHeader = ({
classes,
label,
value,
icon,
className,
...rest
}) => {
const translate = useTranslate();
const location = useLocation();
const formGroup = useFormGroup(value);
const form = useForm();
const [showError, setShowError] = React.useState(false);

useEffect(() => {
const unsubscribe = form.subscribe(
state => {
if (!showError && (state.submitting || state.submitFailed)) {
setShowError(true);
}
},
{ submitting: true, submitFailed: true }
);

return unsubscribe;
}, [form, showError]);

return (
<MuiTab
label={translate(label, { _: label })}
value={value}
icon={icon}
className={classnames('form-tab', className, {
[classes.errorTabButton]:
formGroup.invalid &&
(formGroup.touched || showError) &&
location.pathname !== value,
})}
component={Link}
to={{ ...location, pathname: value }}
id={`tabheader-${value}`}
aria-controls={`tabpanel-${value}`}
{...rest}
/>
);
};
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TabbedForm, { TabbedFormProps } from './TabbedForm';
import TabbedFormTabs from './TabbedFormTabs';
import Toolbar, { ToolbarProps } from './Toolbar';
import getFormInitialValues from './getFormInitialValues';
export * from './FormTabHeader';

export type {
SimpleFormProps,
Expand Down