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

[WIP] makeStyles and functional components #3461

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fc223ec
makeStyles for functional components
MohammedFaragallah Jul 25, 2019
1faad93
removed createStyles
MohammedFaragallah Jul 25, 2019
f0fe0c7
fixes failing test
MohammedFaragallah Jul 25, 2019
a68ab7c
Merge branch 'next' into useStyles
MohammedFaragallah Jul 25, 2019
f165d35
merge
MohammedFaragallah Jul 26, 2019
0fe39fc
class => functional components / use makeStyles
MohammedFaragallah Jul 26, 2019
4e6c658
fix tests
MohammedFaragallah Jul 26, 2019
1cea7c2
ra-tree-ui-materialui DragLayer
MohammedFaragallah Jul 26, 2019
b2437c6
ra-tree-ui-materialui DragPreview/NodeForm
MohammedFaragallah Jul 26, 2019
1b912c4
ra-tree-ui-materialui RootDropTarget/tree
MohammedFaragallah Jul 26, 2019
651c638
prevent firing ra-tree-ui-materialui/Tree useEffect after every update
MohammedFaragallah Jul 26, 2019
34bb6c3
convert TreeNodeContent/TreeNodeWithChildren
MohammedFaragallah Jul 26, 2019
ad0516a
Merge branch 'next' of https://github.com/marmelab/react-admin into u…
MohammedFaragallah Jul 26, 2019
28ecd0a
makeStyles typescript components
MohammedFaragallah Jul 26, 2019
fd60887
class => functional components
MohammedFaragallah Jul 27, 2019
2b23e4d
FileInputPreview/SingleFieldList
MohammedFaragallah Jul 27, 2019
4e1cca9
SimpleFormIterator
MohammedFaragallah Jul 27, 2019
2d16633
makeStyles TabbedForm
MohammedFaragallah Jul 27, 2019
db7c638
save
MohammedFaragallah Jul 28, 2019
50f69fc
Merge branch 'next' of https://github.com/marmelab/react-admin into u…
MohammedFaragallah Jul 30, 2019
60f87af
Merge branch 'next' of https://github.com/marmelab/react-admin into u…
MohammedFaragallah Jul 31, 2019
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
100 changes: 47 additions & 53 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,50 @@ 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);
}
});

render() {
const {
classes,
beingDragged,
dragPreviewComponent: DragPreview,
itemBeingDragged,
offset,
} = this.props;
if (!beingDragged || !offset) return null;
const CustomDragLayer = ({
beingDragged,
dragPreviewComponent: DragPreview,
itemBeingDragged,
offset,
}) => {
const classes = useStyles();
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>
);
};

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

export default compose(
withStyles(styles),
DragLayer(monitor => ({
itemBeingDragged: monitor.getItem(),
componentType: monitor.getItemType(),
beingDragged: monitor.isDragging(),
offset: monitor.getSourceClientOffset(),
}))
)(CustomDragLayer);
export default DragLayer(monitor => ({
itemBeingDragged: monitor.getItem(),
componentType: monitor.getItemType(),
beingDragged: monitor.isDragging(),
offset: monitor.getSourceClientOffset(),
}))(
React.memo(
CustomDragLayer,
(prevProps, nextProps) => !isEqual(prevProps.offset, nextProps.offset)
)
);
63 changes: 26 additions & 37 deletions packages/ra-tree-ui-materialui/src/DragPreview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { Component } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';
import { translate } from 'ra-core';

const styles = theme => ({
const useStyles = makeStyles(theme => ({
item: {
alignItems: 'center',
backgroundColor: theme.palette.action.active,
Expand All @@ -16,45 +15,35 @@ const styles = theme => ({
paddingLeft: theme.spacing(6),
paddingRight: theme.spacing(4),
},
});
class DragPreview extends Component {
shouldComponentUpdate() {
return false;
}
render() {
const {
children,
className,
classes,
node,
style,
translate,
} = this.props;
return (
<div className={className || classes.item} style={style}>
{children
? typeof children === 'function'
? children({ node, translate })
: children
: translate('ra.tree.drag_preview', {
id: node.id,
smart_count: node.children.length,
})}
</div>
);
}
}
}));

const DragPreview = ({ children, className, node, style, translate }) => {
const classes = useStyles();

return (
<div className={className || classes.item} style={style}>
{children
? typeof children === 'function'
? children({ node, translate })
: children
: translate('ra.tree.drag_preview', {
id: node.id,
smart_count: node.children.length,
})}
</div>
);
};

DragPreview.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
className: PropTypes.string,
classes: PropTypes.object,
node: PropTypes.object,
style: PropTypes.object,
translate: PropTypes.func.isRequired,
};

export default compose(
translate,
withStyles(styles)
)(DragPreview);
export default translate()(
React.memo(DragPreview, () => {
return false;
})
);
46 changes: 22 additions & 24 deletions packages/ra-tree-ui-materialui/src/NodeActions.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import React, { cloneElement, Children, Component } from 'react';
import React, { cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';

const styles = theme => ({
const useStyles = makeStyles(theme => ({
root: {
alignItems: 'center',
marginLeft: 'auto',
marginRight: theme.spacing(4),
},
});
}));

export class NodeActions extends Component {
static propTypes = {
classes: PropTypes.object.isRequired,
basePath: PropTypes.string.isRequired,
children: PropTypes.node,
record: PropTypes.object.isRequired,
resource: PropTypes.string.isRequired,
};
const NodeActions = ({ children, ...props }) => {
const classes = useStyles();

render() {
const { children, classes, ...props } = this.props;
return (
<span className={classes.root}>
{Children.map(children, action =>
action ? cloneElement(action, props) : null
)}
</span>
);
}
}
return (
<span className={classes.root}>
{Children.map(children, action =>
action ? cloneElement(action, props) : null
)}
</span>
);
};

export default withStyles(styles)(NodeActions);
NodeActions.propTypes = {
basePath: PropTypes.string.isRequired,
children: PropTypes.node,
record: PropTypes.object.isRequired,
resource: PropTypes.string.isRequired,
};

export default NodeActions;
Loading