Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ class Array extends Component<Props, State> {
};
}

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 &&
Expand Down
34 changes: 23 additions & 11 deletions src/ComposedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props> {
displayName = `ComposedComponent(${getDisplayName(ComposedComponent)})`;

constructor(props) {
Expand All @@ -33,31 +41,35 @@ 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,
valid: true,
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
};
}

Expand Down
7 changes: 3 additions & 4 deletions src/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const MenuProps = {
type Props = {
model: any,
form: any,
key: any,
onChangeValidate: any,
classes: any,
localization: Localization
Expand All @@ -64,10 +63,10 @@ class MultiSelect extends Component<Props, State> {
}

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;
Expand Down
11 changes: 4 additions & 7 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { Localization } from "./types";

type Props = {
model: any,
key: any,
form: any,
onChangeValidate: any,
localization: Localization
Expand All @@ -29,13 +28,11 @@ class Select extends Component<Props, State> {
};
}

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;
Expand Down