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

Fix ReferenceField when used inside form #4147

Merged
merged 2 commits into from
Dec 12, 2019
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Dec 12, 2019

Closes #4124

The bug is a bit tricky: FormInput decorates Field components with Labeled, passing them their props. Then, it renders the Field, passing them the record.

export const FormInput = ({ input, classes: classesOverride, ...rest }) => {
    const classes = useStyles({ classes: classesOverride });
    return input ? (
        <div
            className={classnames(
                'ra-input',
                `ra-input-${input.props.source}`,
                input.props.formClassName
            )}
        >
            {input.props.addLabel ? (
                <Labeled
                    id={input.props.id || input.props.source}
                    {...input.props}
                    {...sanitizeRestProps(rest)}
                >
                    {React.cloneElement(input, {
                        className: classnames(
                            {
                                [classes.input]: !input.props.fullWidth,
                            },
                            input.props.className
                        ),
                        id: input.props.id || input.props.source,
                        ...rest,
                    })}
                </Labeled>
            ) : (
                React.cloneElement(input, {
                    className: classnames(
                        {
                            [classes.input]: !input.props.fullWidth,
                        },
                        input.props.className
                    ),
                    id: input.props.id || input.props.source,
                    ...rest,
                })
            )}
        </div>
    ) : null;
};

The problem is that the record value for ReferenceField is {} due to defaultProps, so it overrides the record passed by FormInput.

Then you might ask: how come this used to work in 2.x? Well, my friend, that's because in 2.x we didn't export ReferenceField, but withStyles(styles)(ReferenceField), so the defaultProps of ReferenceField were never passed to the Labeled element...

And you might also ask: Why did we need a defaultValue in the first place? It probably dates before we used lodash.get to get the id form the record, or before the useGetMany implementation ignored undefined ids. Anyway, it's no longer necessary in 3.x.

@fzaninotto fzaninotto added the RFR Ready For Review label Dec 12, 2019
@fzaninotto fzaninotto added this to the 3.0.4 milestone Dec 12, 2019
@djhi djhi merged commit 7b5cf78 into master Dec 12, 2019
@djhi djhi deleted the fix-referencefield-in-simpleform branch December 12, 2019 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReferenceField inside Edit component is not requesting getMany
3 participants