diff --git a/src/Array.js b/src/Array.js index 6edb9439..479aaca4 100644 --- a/src/Array.js +++ b/src/Array.js @@ -98,8 +98,9 @@ class Array extends Component { }; } - static getDerivedStateFromProps(props, state) { - const propsKey = props.form.key; + static getDerivedStateFromProps(props: Props, state) { + const { form } = props; + const propsKey = form.key; if ( props.form && propsKey === state.formKey && diff --git a/src/ComposedComponent.js b/src/ComposedComponent.js index 9ebf04d0..acc02721 100755 --- a/src/ComposedComponent.js +++ b/src/ComposedComponent.js @@ -20,11 +20,19 @@ const defaultValue = props => { return value; }; +type Props = { + errorText: any, + form: any, + showErrors: boolean, + localization: any, + onChange: any +}; + const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || "Component"; export default (ComposedComponent, defaultProps = {}) => - class Composed extends React.Component { + class Composed extends React.Component { displayName = `ComposedComponent(${getDisplayName(ComposedComponent)})`; constructor(props) { @@ -33,17 +41,11 @@ export default (ComposedComponent, defaultProps = {}) => this.state = this.constructor.getDerivedStateFromProps(this.props); } - static getDerivedStateFromProps(nextProps) { - // eslint-disable-next-line + static getDerivedStateFromProps(nextProps: Props) { const { errorText, form, showErrors, localization } = nextProps; const getLocalizedString = localization && localization.getLocalizedString; const value = defaultValue(nextProps); - const validationResult = utils.validate( - form, - value, - getLocalizedString - ); if (!showErrors) { return { value, @@ -51,13 +53,23 @@ export default (ComposedComponent, defaultProps = {}) => error: "" }; } + + const validationResult = utils.validate( + form, + value, + getLocalizedString + ); + + const error = !validationResult.valid + ? validationResult.error + : undefined; + return { value, valid: validationResult.valid, error: - (!validationResult.valid - ? validationResult.error.message // eslint-disable-line - : null) || errorText + (!validationResult.valid ? error.message : null) || + errorText }; } diff --git a/src/MultiSelect.js b/src/MultiSelect.js index ab930e9e..a3b037dd 100644 --- a/src/MultiSelect.js +++ b/src/MultiSelect.js @@ -44,7 +44,6 @@ const MenuProps = { type Props = { model: any, form: any, - key: any, onChangeValidate: any, classes: any, localization: Localization @@ -64,10 +63,10 @@ class MultiSelect extends Component { } static getDerivedStateFromProps(props: Props) { - if (props.model && props.form.key) { + const { model, form } = props; + if (model && form.key) { return { - currentValue: - utils.getValueFromModel(props.model, props.form.key) || [] + currentValue: utils.getValueFromModel(model, form.key) || [] }; } return null; diff --git a/src/Select.js b/src/Select.js index 36b748cb..e656a9ce 100644 --- a/src/Select.js +++ b/src/Select.js @@ -10,7 +10,6 @@ import type { Localization } from "./types"; type Props = { model: any, - key: any, form: any, onChangeValidate: any, localization: Localization @@ -29,13 +28,11 @@ class Select extends Component { }; } - static getDerivedStateFromProps(props) { - if (props.model && props.form.key) { + static getDerivedStateFromProps(props: Props) { + const { form, model } = props; + if (model && form.key) { return { - currentValue: utils.getValueFromModel( - props.model, - props.form.key - ) + currentValue: utils.getValueFromModel(model, form.key) }; } return null;