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] Fix FormDataConsumer not working with custom form name #2656

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
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
26 changes: 18 additions & 8 deletions packages/ra-core/src/form/FormDataConsumer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getFormValues } from 'redux-form';
import { getFormValues, FormName } from 'redux-form';
import get from 'lodash/get';

import warning from '../util/warning';

const REDUX_FORM_NAME = 'record-form';

/**
* Get the current (edited) value of the record from the form and pass it
* to child function
Expand Down Expand Up @@ -45,8 +44,9 @@ const REDUX_FORM_NAME = 'record-form';
* </Edit>
* );
*/
export const FormDataConsumer = ({
export const FormDataConsumerView = ({
children,
form,
formData,
source,
index,
Expand Down Expand Up @@ -100,13 +100,23 @@ export const FormDataConsumer = ({
return ret === undefined ? null : ret;
};

FormDataConsumer.propTypes = {
FormDataConsumerView.propTypes = {
children: PropTypes.func.isRequired,
data: PropTypes.object,
};

const mapStateToProps = (state, { record }) => ({
formData: getFormValues(REDUX_FORM_NAME)(state) || record,
const mapStateToProps = (state, { form, record }) => ({
formData: getFormValues(form)(state) || record,
});

export default connect(mapStateToProps)(FormDataConsumer);
const ConnectedFormDataConsumerView = connect(mapStateToProps)(
FormDataConsumerView
);

const FormDataConsumer = props => (
<FormName>
{({ form }) => <ConnectedFormDataConsumerView form={form} {...props} />}
</FormName>
);

export default FormDataConsumer;
16 changes: 11 additions & 5 deletions packages/ra-core/src/form/FormDataConsumer.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import { shallow } from 'enzyme';

import { FormDataConsumer } from './FormDataConsumer';
import { FormDataConsumerView } from './FormDataConsumer';

describe('FormDataConsumer', () => {
describe('FormDataConsumerView', () => {
it('does not call its children function with scopedFormData and getSource if it did not receive an index prop', () => {
const children = jest.fn();
const formData = { id: 123, title: 'A title' };

shallow(
<FormDataConsumer formData={formData}>{children}</FormDataConsumer>
<FormDataConsumerView formData={formData}>
{children}
</FormDataConsumerView>
);

expect(children).toHaveBeenCalledWith({
Expand All @@ -24,9 +26,13 @@ describe('FormDataConsumer', () => {
const formData = { id: 123, title: 'A title', authors: [{ id: 0 }] };

shallow(
<FormDataConsumer source="authors[0]" index={0} formData={formData}>
<FormDataConsumerView
source="authors[0]"
index={0}
formData={formData}
>
{children}
</FormDataConsumer>
</FormDataConsumerView>
);

expect(children.mock.calls[0][0].formData).toEqual(formData);
Expand Down