-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathCreateView.tsx
123 lines (111 loc) · 3.06 KB
/
CreateView.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
import * as React from 'react';
import { ReactNode } from 'react';
import PropTypes from 'prop-types';
import { Card } from '@mui/material';
import { styled } from '@mui/material/styles';
import { RaRecord, CreateControllerProps, useCreateContext } from 'ra-core';
import clsx from 'clsx';
import { CreateProps } from '../types';
import { Title } from '../layout';
export const CreateView = (props: CreateViewProps) => {
const {
actions,
aside,
children,
className,
component: Content = Card,
title,
...rest
} = props;
const { resource, defaultTitle } = useCreateContext(props);
return (
<Root
className={clsx('create-page', className)}
{...sanitizeRestProps(rest)}
>
<Title
title={title}
defaultTitle={defaultTitle}
preferenceKey={`${resource}.create.title`}
/>
{actions}
<div
className={clsx(CreateClasses.main, {
[CreateClasses.noActions]: !actions,
})}
>
<Content className={CreateClasses.card}>{children}</Content>
{aside}
</div>
</Root>
);
};
interface CreateViewProps<RecordType extends RaRecord = any>
extends CreateProps<RecordType>,
Omit<CreateControllerProps<RecordType>, 'resource'> {
children: ReactNode;
}
CreateView.propTypes = {
actions: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]),
aside: PropTypes.element,
children: PropTypes.node,
className: PropTypes.string,
defaultTitle: PropTypes.any,
hasList: PropTypes.bool,
hasShow: PropTypes.bool,
mutationOptions: PropTypes.object,
record: PropTypes.object,
redirect: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.func,
]),
resource: PropTypes.string,
save: PropTypes.func,
title: PropTypes.node,
};
/* eslint-disable @typescript-eslint/no-unused-vars */
const sanitizeRestProps = ({
addMiddleware = null,
defaultTitle = null,
hasCreate = null,
hasEdit = null,
hasList = null,
hasShow = null,
history = null,
isFetching = null,
isLoading = null,
location = null,
match = null,
mutationOptions = null,
options = null,
permissions = null,
save = null,
saving = null,
transform = null,
removeMiddleware = null,
...rest
}) => rest;
/* eslint-enable @typescript-eslint/no-unused-vars */
const PREFIX = 'RaCreate';
export const CreateClasses = {
main: `${PREFIX}-main`,
noActions: `${PREFIX}-noActions`,
card: `${PREFIX}-card`,
};
const Root = styled('div', {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
[`& .${CreateClasses.main}`]: {
display: 'flex',
},
[`& .${CreateClasses.noActions}`]: {
[theme.breakpoints.up('sm')]: {
marginTop: '1em',
},
},
[`& .${CreateClasses.card}`]: {
flex: '1 1 auto',
},
}));