From 51cc2ae651b90065b1547c555dfcc4bb80d2f284 Mon Sep 17 00:00:00 2001 From: Dirk-Jan Wassink Date: Sun, 23 Apr 2017 21:39:43 +0200 Subject: [PATCH] Create initialiseField functions instead of magic boolean in setValue functions. --- src/business/form.ts | 25 ++++++++++++++++++++----- src/composers/field.tsx | 6 +++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/business/form.ts b/src/business/form.ts index 3e368e0..f08dba2 100644 --- a/src/business/form.ts +++ b/src/business/form.ts @@ -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, diff --git a/src/composers/field.tsx b/src/composers/field.tsx index ab180eb..dfdd9ea 100644 --- a/src/composers/field.tsx +++ b/src/composers/field.tsx @@ -39,13 +39,13 @@ export function Field(WrappedComponent: IncomingField, 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); } } @@ -58,7 +58,7 @@ export function Field(WrappedComponent: IncomingField, } private handleChange(value: any) { - this.props.form.setFieldValue(this.getFieldName(), value, false); + this.props.form.setFieldValue(this.getFieldName(), value); } public render() {