Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 3450962

Browse files
committed
Merge pull request #216 from spalger/getFieldFromState/nestedFieldsAndForms
Fix handling of nested fields/forms in getFieldFromState
2 parents 9ce4047 + fee8019 commit 3450962

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

src/utils/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ function getFieldFromState(state, model) {
7272

7373
if (!form) return null;
7474

75-
return getField(form, toPath(model).slice(1));
75+
const formPath = toPath(form.model);
76+
const fieldPath = toPath(model).slice(formPath.length);
77+
return getField(form, fieldPath.length ? [fieldPath.join('.')] : []);
7678
}
7779

7880
function getValidity(validators, value) {

test/errors-component-spec.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -577,26 +577,28 @@ describe('<Errors />', () => {
577577
}),
578578
}));
579579

580-
assert.doesNotThrow(() => {
581-
const form = TestUtils.renderIntoDocument(
582-
<Provider store={store}>
583-
<form>
584-
<Errors model="forms.test.foo"
585-
messages={{
586-
required: 'This field is required',
587-
}}
588-
/>
589-
<Field model="forms.test.foo"
590-
validators={{
591-
required: (v) => v && v.length,
592-
}}
593-
>
594-
<input type="text" />
595-
</Field>
596-
</form>
597-
</Provider>
598-
);
599-
});
580+
const form = TestUtils.renderIntoDocument(
581+
<Provider store={store}>
582+
<form>
583+
<Errors model="forms.test.foo"
584+
messages={{
585+
required: 'This field is required',
586+
}}
587+
/>
588+
<Field model="forms.test.foo"
589+
validators={{
590+
required: (v) => v && v.length,
591+
}}
592+
>
593+
<input type="text" />
594+
</Field>
595+
</form>
596+
</Provider>
597+
);
598+
599+
const spans = TestUtils.scryRenderedDOMComponentsWithTag(form, 'span')
600+
assert.lengthOf(spans, 1);
601+
assert.equal(spans[0].innerHTML, 'This field is required')
600602
});
601603
});
602604

test/utils-spec.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import mapValues from 'lodash/mapValues';
77
import _get from 'lodash/get';
88
import { assert } from 'chai';
99

10-
import { utils } from '../src/index';
10+
import { actions, formReducer, utils } from '../src';
1111

1212
describe('utils', () => {
1313
describe('invertValidators()', () => {
@@ -79,5 +79,35 @@ describe('utils', () => {
7979
it('should exist', () => {
8080
assert.isFunction(utils.getFieldFromState);
8181
});
82+
83+
context('standard form', () => {
84+
it('finds the field in state', () => {
85+
const reducer = formReducer('person');
86+
const personForm = reducer(undefined, actions.change('person.name', 'john'));
87+
88+
const field = utils.getFieldFromState({ personForm }, 'person.name');
89+
assert.strictEqual(personForm.fields.name, field);
90+
});
91+
});
92+
93+
context('nested form', () => {
94+
it('finds the field in state', () => {
95+
const reducer = formReducer('app.car');
96+
const carForm = reducer(undefined, actions.change('app.car.make', 'mazda'));
97+
98+
const field = utils.getFieldFromState({ carForm }, 'app.car.make');
99+
assert.strictEqual(carForm.fields.make, field);
100+
});
101+
});
102+
103+
context('nested field', () => {
104+
it('finds the field in state', () => {
105+
const reducer = formReducer('district.library');
106+
const libraryForm = reducer(undefined, actions.change('district.library.hours.open', 8));
107+
108+
const field = utils.getFieldFromState({ libraryForm }, 'district.library.hours.open');
109+
assert.strictEqual(libraryForm.fields['hours.open'], field);
110+
});
111+
});
82112
});
83113
});

0 commit comments

Comments
 (0)