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

Commit 7d3ffed

Browse files
author
Andreas Taube
committed
support deep state paths in <Form />
1 parent b9a4eaa commit 7d3ffed

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
@@ -586,4 +586,34 @@ describe('<Form> component', () => {
586586
assert.equal(timesSubmitCalled, 1);
587587
});
588588
});
589+
590+
describe('deep state path', () => {
591+
const fromsReducer = combineReducers({
592+
testForm: formReducer('forms.test'),
593+
test: modelReducer('forms.test', {
594+
foo: '',
595+
bar: '',
596+
}),
597+
});
598+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
599+
forms: fromsReducer,
600+
}));
601+
602+
const form = TestUtils.renderIntoDocument(
603+
<Provider store={store}>
604+
<Form model="forms.test" onSubmit={() => {}} />
605+
</Provider>
606+
);
607+
608+
const component = TestUtils.findRenderedComponentWithType(form, Form);
609+
const props = component.renderedElement.props;
610+
611+
it('should resolve the model value', () => {
612+
assert.containSubset(props.modelValue, { foo: '', bar: '' });
613+
});
614+
615+
it('should resolve the form value', () => {
616+
assert.containSubset(props.formValue, { valid: true, model: 'forms.test' });
617+
});
618+
});
589619
});

0 commit comments

Comments
 (0)