Skip to content

Commit

Permalink
Merge pull request #2640 from marmelab/buttons-events
Browse files Browse the repository at this point in the history
[RFR] Allow to extend the buttons onClick handlers
  • Loading branch information
fzaninotto authored Dec 13, 2018
2 parents 523145c + af8ca71 commit 4adfeea
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/ra-ui-materialui/src/button/BulkDeleteButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,21 @@ class BulkDeleteButton extends Component {
selectedIds,
startUndoable,
undoable,
onClick,
} = this.props;
if (undoable) {
startUndoable(crudDeleteMany(resource, selectedIds, basePath));
} else {
dispatchCrudDeleteMany(resource, selectedIds, basePath);
}

if (typeof onClick === 'function') {
onClick();
}
};

render() {
const { classes, label, icon, ...rest } = this.props;
const { classes, label, icon, onClick, ...rest } = this.props;
return (
<Button
onClick={this.handleClick}
Expand Down
23 changes: 22 additions & 1 deletion packages/ra-ui-materialui/src/button/DeleteButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ const styles = theme => ({
},
});

const sanitizeRestProps = ({
basePath,
classes,
dispatchCrudDeleteMany,
filterValues,
label,
resource,
selectedIds,
startUndoable,
undoable,
...rest
}) => rest;

class DeleteButton extends Component {
handleDelete = event => {
event.stopPropagation();
Expand All @@ -34,6 +47,7 @@ class DeleteButton extends Component {
basePath,
redirect,
undoable,
onClick,
} = this.props;
if (undoable) {
startUndoable(
Expand All @@ -42,14 +56,20 @@ class DeleteButton extends Component {
} else {
dispatchCrudDelete(resource, record.id, record, basePath, redirect);
}

if (typeof onClick === 'function') {
onClick();
}
};

render() {
const {
label = 'ra.action.delete',
classes = {},
className,
icon
icon,
onClick,
...rest
} = this.props;
return (
<Button
Expand All @@ -61,6 +81,7 @@ class DeleteButton extends Component {
className
)}
key="button"
{...sanitizeRestProps(rest)}
>
{icon}
</Button>
Expand Down
5 changes: 5 additions & 0 deletions packages/ra-ui-materialui/src/button/ExportButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class ExportButton extends Component {
maxResults,
sort,
resource,
onClick,
} = this.props;
dispatch(
crudGetAll(
Expand All @@ -135,6 +136,10 @@ class ExportButton extends Component {
: downloadCSV(convertToCSV(data), resource)
)
);

if (typeof onClick === 'function') {
onClick();
}
};

render() {
Expand Down
7 changes: 6 additions & 1 deletion packages/ra-ui-materialui/src/button/RefreshButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ class RefreshButton extends Component {
};

handleClick = event => {
const { refreshView, onClick } = this.props;
event.preventDefault();
this.props.refreshView();
refreshView();

if (typeof onClick === 'function') {
onClick();
}
};

render() {
Expand Down
7 changes: 6 additions & 1 deletion packages/ra-ui-materialui/src/button/RefreshIconButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class RefreshButton extends Component {
};

handleClick = event => {
const { refreshView, onClick } = this.props;
event.preventDefault();
this.props.refreshView();
refreshView();

if (typeof onClick === 'function') {
onClick();
}
};

render() {
Expand Down
6 changes: 6 additions & 0 deletions packages/ra-ui-materialui/src/button/SaveButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class SaveButton extends Component {
redirect,
saving,
showNotification,
onClick,
} = this.props;

if (saving) {
Expand All @@ -84,6 +85,10 @@ export class SaveButton extends Component {
}
handleSubmitWithRedirect(redirect)();
}

if (typeof onClick === 'function') {
onClick();
}
};

render() {
Expand All @@ -99,6 +104,7 @@ export class SaveButton extends Component {
translate,
variant = 'raised',
icon,
onClick,
...rest
} = this.props;

Expand Down

0 comments on commit 4adfeea

Please sign in to comment.