Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Commit

Permalink
Create initialiseField functions instead of magic boolean in setValue…
Browse files Browse the repository at this point in the history
… functions.
  • Loading branch information
DJWassink committed Apr 23, 2017
1 parent 75ab99b commit 51cc2ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/business/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,37 @@ export class Form {
return this.fieldStates[fieldName];
}

public setFieldValues(values: { [fieldName: string]: any }, pristineValues = false) {
public initialiseFieldValues(values: { [fieldName: string]: any }) {
const fieldNames = Object.keys(values);
fieldNames.forEach((fieldName) => {
this.updateFieldValue(fieldName, values[fieldName], pristineValues);
this.updateFieldValue(fieldName, values[fieldName], true);
});

this.triggerMultipleFieldListeners(fieldNames);
}

public setFieldValue(fieldName: string, value: any, pristineValue = false) {
this.updateFieldValue(fieldName, value, pristineValue);
public setFieldValues(values: { [fieldName: string]: any }) {
const fieldNames = Object.keys(values);
fieldNames.forEach((fieldName) => {
this.updateFieldValue(fieldName, values[fieldName], false);
});

this.triggerMultipleFieldListeners(fieldNames);
}

public initialiseFieldValue(fieldName: string, value: any) {
this.updateFieldValue(fieldName, value, true);

this.triggerFieldListeners(fieldName);
}

public setFieldValue(fieldName: string, value: any) {
this.updateFieldValue(fieldName, value, false);

this.triggerFieldListeners(fieldName);
}

private updateFieldValue(fieldName: string, value: any, pristineValue) {
private updateFieldValue(fieldName: string, value: any, pristineValue: boolean) {
this.fieldStates[fieldName] = {
...this.fieldStates[fieldName],
value,
Expand Down
6 changes: 3 additions & 3 deletions src/composers/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export function Field<Props>(WrappedComponent: IncomingField<Props>,

public componentWillMount() {
if (typeof this.props.value !== 'undefined') {
this.props.form.setFieldValue(this.getFieldName(), this.props.value, true);
this.props.form.initialiseFieldValue(this.getFieldName(), this.props.value);
}
}

public componentWillUpdate(nextProps: Props & OwnProps & FormProps) {
if (typeof nextProps.value !== 'undefined' && this.props.value !== nextProps.value) {
this.props.form.setFieldValue(this.getFieldName(), nextProps.value, true);
this.props.form.initialiseFieldValue(this.getFieldName(), nextProps.value);
}
}

Expand All @@ -58,7 +58,7 @@ export function Field<Props>(WrappedComponent: IncomingField<Props>,
}

private handleChange(value: any) {
this.props.form.setFieldValue(this.getFieldName(), value, false);
this.props.form.setFieldValue(this.getFieldName(), value);
}

public render() {
Expand Down

0 comments on commit 51cc2ae

Please sign in to comment.