-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathSimpleForm.tsx
More file actions
144 lines (134 loc) · 3.6 KB
/
SimpleForm.tsx
File metadata and controls
144 lines (134 loc) · 3.6 KB
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
import * as React from 'react';
import type { ReactNode } from 'react';
import { Form, type FormProps } from 'ra-core';
import {
Stack,
CardContent,
type SxProps,
type StackProps,
} from '@mui/material';
import {
type ComponentsOverrides,
styled,
type Theme,
useThemeProps,
} from '@mui/material/styles';
import { Toolbar } from './Toolbar';
/**
* Form with a one column layout, one input per line.
*
* Pass input components as children.
*
* @example
*
* import * as React from "react";
* import { Create, Edit, SimpleForm, TextInput, DateInput, ReferenceManyField, Datagrid, TextField, DateField, EditButton } from 'react-admin';
* import RichTextInput from 'ra-input-rich-text';
*
* export const PostCreate = () => (
* <Create>
* <SimpleForm>
* <TextInput source="title" />
* <TextInput source="teaser" options={{ multiline: true }} />
* <RichTextInput source="body" />
* <DateInput label="Publication date" source="published_at" defaultValue={new Date()} />
* </SimpleForm>
* </Create>
* );
*
* @typedef {Object} Props the props you can use (other props are injected by Create or Edit)
* @prop {ReactNode[]} children Input elements
* @prop {Object} defaultValues
* @prop {Function} validate
* @prop {ReactNode} toolbar The element displayed at the bottom of the form, containing the SaveButton
*
* @param {Props} props
*/
export const SimpleForm = (inProps: SimpleFormProps) => {
const props = useThemeProps({
props: inProps,
name: PREFIX,
});
const {
children,
className,
component: Component = DefaultComponent,
sx,
toolbar = DefaultToolbar,
...rest
} = props;
return (
<Form {...rest}>
<Component className={className} sx={sx}>
<Stack
{...sanitizeRestProps(props)}
sx={{
alignItems: 'flex-start',
}}
>
{children}
</Stack>
</Component>
{toolbar}
</Form>
);
};
export interface SimpleFormProps<RecordType = any>
extends Omit<FormProps<RecordType>, 'render'>,
Omit<StackProps, 'onSubmit'> {
children: ReactNode;
className?: string;
component?: React.ComponentType<any>;
defaultValues?: any;
toolbar?: ReactNode;
sx?: SxProps<Theme>;
}
const PREFIX = 'RaSimpleForm';
const DefaultComponent = styled(CardContent, {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
[theme.breakpoints.down('sm')]: {
paddingBottom: '5em',
},
}));
const DefaultToolbar = <Toolbar />;
const sanitizeRestProps = ({
children,
className,
component,
criteriaMode,
defaultValues,
delayError,
onSubmit,
record,
resource,
reValidateMode,
sx,
toolbar,
validate,
resetOptions,
resolver,
sanitizeEmptyValues,
shouldFocusError,
shouldUnregister,
shouldUseNativeValidation,
warnWhenUnsavedChanges,
...props
}: SimpleFormProps) => props;
declare module '@mui/material/styles' {
interface ComponentNameToClassKey {
RaSimpleForm: 'root';
}
interface ComponentsPropsList {
RaSimpleForm: Partial<SimpleFormProps>;
}
interface Components {
RaSimpleForm?: {
defaultProps?: ComponentsPropsList['RaSimpleForm'];
styleOverrides?: ComponentsOverrides<
Omit<Theme, 'components'>
>['RaSimpleForm'];
};
}
}