/
TabbedFormView.tsx
152 lines (140 loc) · 4.49 KB
/
TabbedFormView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import * as React from 'react';
import {
ChangeEvent,
Children,
ComponentType,
cloneElement,
isValidElement,
ReactElement,
ReactNode,
useState,
} from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import {
Routes,
Route,
matchPath,
useResolvedPath,
useLocation,
} from 'react-router-dom';
import { CardContent, Divider, SxProps } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useResourceContext } from 'ra-core';
import { Toolbar } from './Toolbar';
import { TabbedFormTabs, getTabbedFormTabFullPath } from './TabbedFormTabs';
export const TabbedFormView = (props: TabbedFormViewProps): ReactElement => {
const {
children,
className,
component: Component = DefaultComponent,
formRootPathname,
syncWithLocation = true,
tabs = DefaultTabs,
toolbar = DefaultToolbar,
...rest
} = props;
const location = useLocation();
const resolvedPath = useResolvedPath('');
const resource = useResourceContext(props);
const [tabValue, setTabValue] = useState(0);
const handleTabChange = (event: ChangeEvent<{}>, value: any): void => {
if (!syncWithLocation) {
setTabValue(value);
}
};
const renderTabHeaders = () =>
cloneElement(
tabs,
{
onChange: handleTabChange,
syncWithLocation,
url: formRootPathname,
value: tabValue,
},
children
);
return (
<Root
className={clsx('tabbed-form', className)}
{...sanitizeRestProps(rest)}
>
{syncWithLocation ? (
<Routes>
<Route path="/*" element={renderTabHeaders()} />
</Routes>
) : (
renderTabHeaders()
)}
<Divider />
<Component>
{/* All tabs are rendered (not only the one in focus), to allow validation
on tabs not in focus. The tabs receive a `hidden` property, which they'll
use to hide the tab using CSS if it's not the one in focus.
See https://github.com/marmelab/react-admin/issues/1866 */}
{Children.map(children, (tab: ReactElement, index) => {
if (!tab) {
return null;
}
const tabPath = getTabbedFormTabFullPath(tab, index);
const hidden = syncWithLocation
? !matchPath(
`${resolvedPath.pathname}/${tabPath}`,
location.pathname
)
: tabValue !== index;
return isValidElement<any>(tab)
? React.cloneElement(tab, {
intent: 'content',
resource,
hidden,
value: syncWithLocation ? tabPath : index,
})
: null;
})}
</Component>
{toolbar !== false ? toolbar : null}
</Root>
);
};
TabbedFormView.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
location: PropTypes.object,
match: PropTypes.object,
// @ts-ignore
resource: PropTypes.string,
tabs: PropTypes.element,
toolbar: PropTypes.oneOfType([PropTypes.element, PropTypes.oneOf([false])]),
validate: PropTypes.func,
value: PropTypes.number,
};
const DefaultTabs = <TabbedFormTabs />;
const DefaultComponent = ({ children }) => (
<CardContent>{children}</CardContent>
);
const DefaultToolbar = <Toolbar />;
export interface TabbedFormViewProps {
children?: ReactNode;
className?: string;
component?: ComponentType<any>;
resource?: string;
formRootPathname?: string;
syncWithLocation?: boolean;
tabs?: ReactElement;
toolbar?: ReactElement | false;
sx?: SxProps;
}
const PREFIX = 'RaTabbedForm';
export const TabbedFormClasses = {
errorTabButton: `${PREFIX}-errorTabButton`,
};
const Root = styled('div', {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
[`& .MuiTab-root.${TabbedFormClasses.errorTabButton}`]: {
color: theme.palette.error.main,
},
}));
const sanitizeRestProps = ({ record, resource, ...rest }: any) => rest;