forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloneButton.tsx
69 lines (59 loc) · 1.79 KB
/
CloneButton.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
import * as React from 'react';
import { memo, ReactElement } from 'react';
import PropTypes from 'prop-types';
import Queue from '@material-ui/icons/Queue';
import { Link } from 'react-router-dom';
import { stringify } from 'query-string';
import { Record, useResourceContext } from 'ra-core';
import Button, { ButtonProps } from './Button';
export const CloneButton = (props: CloneButtonProps) => {
const {
basePath = '',
label = 'ra.action.clone',
scrollToTop = true,
record,
icon = defaultIcon,
...rest
} = props;
const resource = useResourceContext();
const pathname = basePath ? `${basePath}/create` : `/${resource}/create`;
return (
<Button
component={Link}
to={
record
? {
pathname,
search: stringify({
source: JSON.stringify(omitId(record)),
}),
state: { _scrollToTop: scrollToTop },
}
: pathname
}
label={label}
onClick={stopPropagation}
{...rest}
>
{icon}
</Button>
);
};
const defaultIcon = <Queue />;
// useful to prevent click bubbling in a datagrid with rowClick
const stopPropagation = e => e.stopPropagation();
const omitId = ({ id, ...rest }: Record) => rest;
interface Props {
basePath?: string;
record?: Record;
icon?: ReactElement;
scrollToTop?: boolean;
}
export type CloneButtonProps = Props & ButtonProps;
CloneButton.propTypes = {
basePath: PropTypes.string,
icon: PropTypes.element,
label: PropTypes.string,
record: PropTypes.any,
};
export default memo(CloneButton);