Created
January 28, 2018 18:19
-
-
Save thecolorblue/f2ba09a866a2a65ad36f7f44726b3cd1 to your computer and use it in GitHub Desktop.
Form Field Factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var { fieldFactory,Submit } = createForm('profile'); | |
| var FName = fieldFactory('fname'); | |
| var LName = fieldFactory('lname'); | |
| <FName /> | |
| <Submit>submit form</Submit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function reducerFactory(fieldReducers) { | |
| return function formReducer(state={}, action) { | |
| if (/UPDATE_/.test(action.type) { | |
| // validate field change | |
| var [form, field] = /UPDATE_{[a-z]}_{[a-z}/.match(action.type); | |
| if (state[form][field]) { | |
| return { | |
| ...state, | |
| [form]: { | |
| ...state[form], | |
| [field]: fieldReducers[field](state[form][field], action) | |
| } | |
| } | |
| } | |
| } | |
| return state; | |
| } | |
| } | |
| import fieldReducer from './fieldReducer.js'; | |
| return reducerFactory({ | |
| 'field': fieldReducer | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment