forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateButton.tsx
117 lines (109 loc) · 3.16 KB
/
CreateButton.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
import * as React from 'react';
import { ReactElement, memo, useMemo } from 'react';
import PropTypes from 'prop-types';
import { Fab, useMediaQuery, Theme } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import ContentAdd from '@material-ui/icons/Add';
import classnames from 'classnames';
import { Link } from 'react-router-dom';
import { useTranslate, useResourceContext } from 'ra-core';
import Button, { ButtonProps, sanitizeButtonRestProps } from './Button';
/**
* Opens the Create view of a given resource
*
* Renders as a regular button on desktop, and a Floating Action Button
* on mobile.
*
* @example // basic usage
* import { CreateButton } from 'react-admin';
*
* const CommentCreateButton = () => (
* <CreateButton basePath="/comments" label="Create comment" />
* );
*/
const CreateButton = (props: CreateButtonProps) => {
const {
basePath = '',
className,
classes: classesOverride,
icon = defaultIcon,
label = 'ra.action.create',
scrollToTop = true,
variant,
...rest
} = props;
const classes = useStyles(props);
const translate = useTranslate();
const isSmall = useMediaQuery((theme: Theme) =>
theme.breakpoints.down('sm')
);
const resource = useResourceContext();
const location = useMemo(
() => ({
pathname: basePath ? `${basePath}/create` : `/${resource}/create`,
state: { _scrollToTop: scrollToTop },
}),
[basePath, resource, scrollToTop]
);
return isSmall ? (
<Fab
component={Link}
color="primary"
className={classnames(classes.floating, className)}
to={location}
aria-label={label && translate(label)}
{...sanitizeButtonRestProps(rest)}
>
{icon}
</Fab>
) : (
<Button
component={Link}
to={location}
className={className}
label={label}
variant={variant}
{...(rest as any)}
>
{icon}
</Button>
);
};
const defaultIcon = <ContentAdd />;
const useStyles = makeStyles(
theme => ({
floating: {
color: theme.palette.getContrastText(theme.palette.primary.main),
margin: 0,
top: 'auto',
right: 20,
bottom: 60,
left: 'auto',
position: 'fixed',
zIndex: 1000,
},
}),
{ name: 'RaCreateButton' }
);
interface Props {
basePath?: string;
icon?: ReactElement;
scrollToTop?: boolean;
}
export type CreateButtonProps = Props & ButtonProps;
CreateButton.propTypes = {
basePath: PropTypes.string,
classes: PropTypes.object,
className: PropTypes.string,
icon: PropTypes.element,
label: PropTypes.string,
};
export default memo(CreateButton, (prevProps, nextProps) => {
return (
prevProps.basePath === nextProps.basePath &&
prevProps.label === nextProps.label &&
prevProps.translate === nextProps.translate &&
prevProps.to === nextProps.to &&
prevProps.disabled === nextProps.disabled
);
});