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

Commit 0f19eb2

Browse files
committed
Merge pull request #114 from ataube/form-deep-state-path
support deep state paths in <Form />
2 parents 2aa8f4f + 7d3ffed commit 0f19eb2

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/utils/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import isPlainObject from 'lodash/isPlainObject';
44
import every from 'lodash/every';
55
import some from 'lodash/some';
66
import findKey from 'lodash/findKey';
7+
import get from 'lodash/get';
78

89
import { initialFieldState } from '../reducers/form-reducer';
910

@@ -48,9 +49,10 @@ function getValue(value) {
4849
}
4950

5051
function getForm(state, model) {
51-
const formStateKey = findKey(state, { model });
52-
53-
return state[formStateKey];
52+
const path = model.split('.');
53+
const modelRoot = path.length === 1 ? state : get(state, path.slice(0, path.length - 1));
54+
const formStateKey = findKey(modelRoot, { model });
55+
return modelRoot && modelRoot[formStateKey];
5456
}
5557

5658
function getValidity(validators, value) {

test/form-component-spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,34 @@ describe('<Form> component', () => {
592592
assert.equal(timesSubmitCalled, 1);
593593
});
594594
});
595+
596+
describe('deep state path', () => {
597+
const fromsReducer = combineReducers({
598+
testForm: formReducer('forms.test'),
599+
test: modelReducer('forms.test', {
600+
foo: '',
601+
bar: '',
602+
}),
603+
});
604+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
605+
forms: fromsReducer,
606+
}));
607+
608+
const form = TestUtils.renderIntoDocument(
609+
<Provider store={store}>
610+
<Form model="forms.test" onSubmit={() => {}} />
611+
</Provider>
612+
);
613+
614+
const component = TestUtils.findRenderedComponentWithType(form, Form);
615+
const props = component.renderedElement.props;
616+
617+
it('should resolve the model value', () => {
618+
assert.containSubset(props.modelValue, { foo: '', bar: '' });
619+
});
620+
621+
it('should resolve the form value', () => {
622+
assert.containSubset(props.formValue, { valid: true, model: 'forms.test' });
623+
});
624+
});
595625
});

0 commit comments

Comments
 (0)