-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5903 from marmelab/fix-tabbedform-error-display-o…
…nsubmit Fix TabbedForm Does not Display Errors in Hidden Tabs on Submit
- Loading branch information
Showing
3 changed files
with
57 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as React from 'react'; | ||
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} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters