Skip to content

Instantly share code, notes, and snippets.

@thecolorblue
Created January 28, 2018 18:19
Show Gist options
  • Select an option

  • Save thecolorblue/f2ba09a866a2a65ad36f7f44726b3cd1 to your computer and use it in GitHub Desktop.

Select an option

Save thecolorblue/f2ba09a866a2a65ad36f7f44726b3cd1 to your computer and use it in GitHub Desktop.
Form Field Factory
var { fieldFactory,Submit } = createForm('profile');
var FName = fieldFactory('fname');
var LName = fieldFactory('lname');
<FName />
<Submit>submit form</Submit>
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