Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Convert DragLayer component to useStyles #3591

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 41 additions & 46 deletions packages/ra-tree-ui-materialui/src/DragLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Custom DragLayer from Alejandro Hernandez
* See https://github.com/react-dnd/react-dnd/issues/592#issuecomment-399287474
*/
import React, { Component } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import { DragLayer } from 'react-dnd';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';
import isEqual from 'lodash/isEqual';
const styles = {

const useStyles = makeStyles({
layer: {
position: 'fixed',
pointerEvents: 'none',
Expand All @@ -19,56 +19,51 @@ const styles = {
height: '100%',
},
item: {},
};

class CustomDragLayer extends Component {
static propTypes = {
beingDragged: PropTypes.bool,
classes: PropTypes.object.isRequired,
dragPreviewComponent: PropTypes.oneOfType([
PropTypes.element,
PropTypes.func,
]).isRequired,
itemBeingDragged: PropTypes.object,
offset: PropTypes.object,
};
});

shouldComponentUpdate(nextProps) {
return !isEqual(this.props.offset, nextProps.offset);
}
function CustomDragLayer({
classes: classesOverride,
beingDragged,
dragPreviewComponent: DragPreview,
itemBeingDragged,
offset,
}) {
const classes = useStyles({ classes: classesOverride });

render() {
const {
classes,
beingDragged,
dragPreviewComponent: DragPreview,
itemBeingDragged,
offset,
} = this.props;
if (!beingDragged || !offset) return null;
if (!beingDragged || !offset) return null;

return (
<div className={classes.layer}>
<div
role="presentation"
className={classes.item}
style={{
transform: `translate(${offset.x}px, ${offset.y}px)`,
}}
>
<DragPreview node={itemBeingDragged} />
</div>
return (
<div className={classes.layer}>
<div
role="presentation"
className={classes.item}
style={{
transform: `translate(${offset.x}px, ${offset.y}px)`,
}}
>
<DragPreview node={itemBeingDragged} />
</div>
);
}
</div>
);
}

export default compose(
withStyles(styles),
CustomDragLayer.propTypes = {
beingDragged: PropTypes.bool,
classes: PropTypes.object,
dragPreviewComponent: PropTypes.oneOfType([
PropTypes.element,
PropTypes.func,
]).isRequired,
itemBeingDragged: PropTypes.object,
offset: PropTypes.object,
};

export default React.memo(
DragLayer(monitor => ({
itemBeingDragged: monitor.getItem(),
componentType: monitor.getItemType(),
beingDragged: monitor.isDragging(),
offset: monitor.getSourceClientOffset(),
}))
)(CustomDragLayer);
}))(CustomDragLayer),
(prevProps, nextProps) => isEqual(prevProps.offset, nextProps.offset)
);