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 NodeForm component to hooks #3599

Merged
merged 4 commits into from
Aug 27, 2019
Merged
Changes from 2 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
201 changes: 98 additions & 103 deletions packages/ra-tree-ui-materialui/src/NodeForm.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { cloneElement, Children, Component } from 'react';
import React, { cloneElement, Children, useCallback } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';
import { Form } from 'react-final-form';
import {
crudUpdate as crudUpdateAction,
Expand All @@ -11,13 +10,13 @@ import {

import NodeFormActions from './NodeFormActions';

const styles = {
const useStyles = makeStyles({
root: {
alignItems: 'center',
display: 'flex',
flexGrow: 1,
},
};
});

const sanitizeRestProps = ({
anyTouched,
Expand Down Expand Up @@ -71,58 +70,53 @@ const sanitizeRestProps = ({
validate,
...props
}) => props;
class NodeForm extends Component {
static propTypes = {
actions: PropTypes.node,
basePath: PropTypes.string.isRequired,
cancelDropOnChildren: PropTypes.bool,
children: PropTypes.node,
classes: PropTypes.object,
dispatchCrudUpdate: PropTypes.func.isRequired,
handleSubmit: PropTypes.func.isRequired,
invalid: PropTypes.bool,
node: PropTypes.object.isRequired,
pristine: PropTypes.bool,
resource: PropTypes.string.isRequired,
saving: PropTypes.bool,
startUndoable: PropTypes.func.isRequired,
submitOnEnter: PropTypes.bool,
undoable: PropTypes.bool,
};

static defaultProps = {
actions: <NodeFormActions />,
};
function NodeForm({
actions,
basePath,
children,
classes: classesOverride,
handleSubmit: handleSubmitProp,
invalid,
node,
pristine,
resource,
saving,
submitOnEnter = true,
...props
}) {
const classes = useStyles({ classes: classesOverride });

handleClick = event => {
const handleClick = useCallback(event => {
event.persist();
// This ensure clicking on an input or button does not collapse/expand a node
// When clicking on the form (empty spaces around inputs) however, it should
// propagate to the parent
if (event.target.tagName.toLowerCase() !== 'form') {
event.stopPropagation();
}
};
}, []);

handleDrop = event => {
event.persist();
if (this.props.cancelDropOnChildren) {
event.preventDefault();
}
};
const { cancelDropOnChildren } = props;
const handleDrop = useCallback(
event => {
event.persist();
if (cancelDropOnChildren) {
event.preventDefault();
}
},
[cancelDropOnChildren]
);

handleSubmit = () => {
const handleSubmit = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not wrap this one inside a useCallback too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, it looked a bit non-trivial than the other two and I wasn't sure if it would subtly break anything if I wrapped it the wrong way. I'll give it a try.

const {
basePath,
dispatchCrudUpdate,
handleSubmit,
node: { record },
resource,
startUndoable,
undoable = true,
} = this.props;
} = props;

return handleSubmit(values =>
return handleSubmitProp(values =>
undoable
? startUndoable(
crudUpdateAction(
Expand All @@ -145,74 +139,75 @@ class NodeForm extends Component {
);
};

render() {
const {
actions,
basePath,
children,
classes,
handleSubmit,
invalid,
node,
pristine,
resource,
saving,
submitOnEnter = true,
...props
} = this.props;

return (
<Form
onSubmit={this.handleSubmit}
render={({ handleSubmit }) => (
<form
className={classes.root}
onClick={this.handleClick}
onSubmit={handleSubmit}
{...sanitizeRestProps(props)}
>
{Children.map(children, field =>
field
? cloneElement(field, {
basePath:
field.props.basePath || basePath,
onDrop: this.handleDrop,
record: node.record,
resource,
})
: null
)}
{actions &&
cloneElement(actions, {
basePath,
record: node.record,
resource,
handleSubmit: this.handleSubmit,
handleSubmitWithRedirect: this.handleSubmit,
invalid,
pristine,
saving,
submitOnEnter,
})}
</form>
)}
/>
);
}
return (
<Form
onSubmit={handleSubmit}
render={({ handleSubmit }) => (
<form
className={classes.root}
onClick={handleClick}
onSubmit={handleSubmit}
{...sanitizeRestProps(props)}
>
{Children.map(children, field =>
field
? cloneElement(field, {
basePath: field.props.basePath || basePath,
onDrop: handleDrop,
record: node.record,
resource,
})
: null
)}
{actions &&
cloneElement(actions, {
basePath,
record: node.record,
resource,
handleSubmit: handleSubmit,
handleSubmitWithRedirect: handleSubmit,
invalid,
pristine,
saving,
submitOnEnter,
})}
</form>
)}
/>
);
}

NodeForm.propTypes = {
actions: PropTypes.node,
basePath: PropTypes.string.isRequired,
cancelDropOnChildren: PropTypes.bool,
children: PropTypes.node,
classes: PropTypes.object,
dispatchCrudUpdate: PropTypes.func.isRequired,
handleSubmit: PropTypes.func.isRequired,
invalid: PropTypes.bool,
node: PropTypes.object.isRequired,
pristine: PropTypes.bool,
resource: PropTypes.string.isRequired,
saving: PropTypes.bool,
startUndoable: PropTypes.func.isRequired,
submitOnEnter: PropTypes.bool,
undoable: PropTypes.bool,
};

NodeForm.defaultProps = {
actions: <NodeFormActions />,
};

const mapStateToProps = (state, { node }) => ({
initialValues: node.record,
record: node.record,
});

export default compose(
connect(
mapStateToProps,
{
dispatchCrudUpdate: crudUpdateAction,
startUndoable: startUndoableAction,
}
),
withStyles(styles)
export default connect(
mapStateToProps,
{
dispatchCrudUpdate: crudUpdateAction,
startUndoable: startUndoableAction,
}
)(NodeForm);