From 074f9534bedfd991e4b74eece4454a508cce222a Mon Sep 17 00:00:00 2001 From: William Cotton Date: Fri, 16 Mar 2018 13:49:34 -0500 Subject: [PATCH] Add existingState property to Form component --- src/components/form/index.js | 3 ++- src/hocs/form/index.js | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/form/index.js b/src/components/form/index.js index 34a3bb3..9abb058 100644 --- a/src/components/form/index.js +++ b/src/components/form/index.js @@ -9,10 +9,11 @@ class Form extends Component { validateAll: PropTypes.func.isRequired, showError: PropTypes.func.isRequired, hideError: PropTypes.func.isRequired, + existingState: PropTypes.object, }; render() { - const { getValues, validate, validateAll, showError, hideError, ...props } = this.props; + const { getValues, validate, validateAll, showError, hideError, existingState, ...props } = this.props; return (
diff --git a/src/hocs/form/index.js b/src/hocs/form/index.js index 4fe2e9e..908cca1 100644 --- a/src/hocs/form/index.js +++ b/src/hocs/form/index.js @@ -8,7 +8,9 @@ export default function form (WrappedComponent) { return class extends PureComponent { static displayName = `Form(${WrappedComponent.name})`; - static propTypes = {}; + static propTypes = { + existingState: PropTypes.object, + }; static childContextTypes = { _register: PropTypes.func.isRequired, @@ -42,6 +44,11 @@ export default function form (WrappedComponent) { } _register = (component, id) => { + const { existingState } = this.props; + const value = + component.props.value || + (existingState && existingState[component.props.name]) || + ''; this.setState(state => ({ byName: { ...state.byName, @@ -52,7 +59,7 @@ export default function form (WrappedComponent) { [id]: { ...component.props, isCheckable: _isCheckable(component), - value: component.props.value || '', + value, ...(_isCheckable(component) ? { checked: !!component.props.checked } : {}) } }