-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathTabbedForm.tsx
177 lines (166 loc) · 5.2 KB
/
TabbedForm.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as React from 'react';
import {
Children,
isValidElement,
ReactElement,
ReactNode,
HtmlHTMLAttributes,
} from 'react';
import PropTypes from 'prop-types';
import {
Form,
FormProps,
MutationMode,
RaRecord,
RedirectionSideEffect,
} from 'ra-core';
import get from 'lodash/get';
import { TabbedFormView, TabbedFormViewProps } from './TabbedFormView';
import { useFormRootPath } from './useFormRootPath';
/**
* Form layout where inputs are divided by tab, one input per line.
*
* Pass FormTab components as children.
*
* @example
*
* import * as React from "react";
* import {
* Edit,
* TabbedForm,
* FormTab,
* Datagrid,
* TextField,
* DateField,
* TextInput,
* ReferenceManyField,
* NumberInput,
* DateInput,
* BooleanInput,
* EditButton
* } from 'react-admin';
*
* export const PostEdit = (props) => (
* <Edit {...props}>
* <TabbedForm>
* <FormTab label="summary">
* <TextInput disabled label="Id" source="id" />
* <TextInput source="title" validate={required()} />
* <TextInput multiline source="teaser" validate={required()} />
* </FormTab>
* <FormTab label="body">
* <RichTextInput source="body" validate={required()} label={false} />
* </FormTab>
* <FormTab label="Miscellaneous">
* <TextInput label="Password (if protected post)" source="password" type="password" />
* <DateInput label="Publication date" source="published_at" />
* <NumberInput source="average_note" validate={[ number(), minValue(0) ]} />
* <BooleanInput label="Allow comments?" source="commentable" defaultValue />
* <TextInput disabled label="Nb views" source="views" />
* </FormTab>
* <FormTab label="comments">
* <ReferenceManyField reference="comments" target="post_id" label={false}>
* <Datagrid>
* <TextField source="body" />
* <DateField source="created_at" />
* <EditButton />
* </Datagrid>
* </ReferenceManyField>
* </FormTab>
* </TabbedForm>
* </Edit>
* );
*
* @typedef {Object} Props the props you can use (other props are injected by Create or Edit)
* @prop {ReactElement[]} FormTab elements
* @prop {Object} defaultValues
* @prop {Function} validate
* @prop {string} redirect
* @prop {ReactElement} toolbar The element displayed at the bottom of the form, containing the SaveButton
*
* @param {Props} props
*/
export const TabbedForm = (props: TabbedFormProps) => {
const formRootPathname = useFormRootPath();
return (
<Form formRootPathname={formRootPathname} {...props}>
<TabbedFormView
formRootPathname={formRootPathname}
{...sanitizeRestProps(props)}
/>
</Form>
);
};
const sanitizeRestProps = ({
criteriaMode,
defaultValues,
delayError,
formRootPathname,
mode,
noValidate,
onSubmit,
record,
resolver,
reValidateMode,
shouldFocusError,
shouldUnregister,
shouldUseNativeValidation,
validate,
warnWhenUnsavedChanges,
...rest
}: TabbedFormProps) => rest;
TabbedForm.propTypes = {
children: PropTypes.node,
defaultValues: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
formRootPathname: PropTypes.string,
mutationMode: PropTypes.oneOf(['pessimistic', 'optimistic', 'undoable']),
// @ts-ignore
record: PropTypes.object,
redirect: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.func,
]),
saving: PropTypes.bool,
validate: PropTypes.func,
};
export interface TabbedFormProps
extends Omit<FormProps, 'render'>,
Omit<
HtmlHTMLAttributes<HTMLFormElement>,
'defaultValue' | 'onSubmit' | 'children'
>,
Partial<TabbedFormViewProps> {
children: ReactNode;
className?: string;
defaultValues?: any;
formRootPathname?: string;
mutationMode?: MutationMode;
record?: RaRecord;
redirect?: RedirectionSideEffect;
resource?: string;
syncWithLocation?: boolean;
tabs?: ReactElement;
toolbar?: ReactElement | false;
warnWhenUnsavedChanges?: boolean;
}
export const findTabsWithErrors = (children, errors) => {
console.warn(
'Deprecated. FormTab now wrap their content inside a FormGroupContextProvider. If you implemented custom forms with tabs, please use the FormGroupContextProvider. See https://marmelab.com/react-admin/EditTutorial.html#grouping-inputs'
);
return Children.toArray(children).reduce((acc: any[], child) => {
if (!isValidElement(child)) {
return acc;
}
const inputs = Children.toArray(child.props.children);
if (
inputs.some(
input =>
isValidElement(input) && get(errors, input.props.source)
)
) {
return [...acc, child.props.label];
}
return acc;
}, []);
};