-
Notifications
You must be signed in to change notification settings - Fork 1
Keep track if a field is pristine or not. #6
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,56 @@ | ||
export class Form { | ||
|
||
private values: { [key: string]: any } = {}; | ||
export interface FieldState { | ||
value: any; | ||
validationError?: string; | ||
pristine: boolean; | ||
} | ||
|
||
private validationErrors: { [fieldName: string]: any } = {}; | ||
export class Form { | ||
|
||
private fieldStates: { [key: string]: FieldState } = {}; | ||
private fieldListeners: { [fieldName: string]: Array<(value: any) => void> } = {}; | ||
|
||
public getFieldValue(fieldName: string): any { | ||
return this.values[fieldName] || ''; | ||
public getFieldState(fieldName: string): FieldState | undefined { | ||
return this.fieldStates[fieldName]; | ||
} | ||
|
||
public setFieldValues(values: { [fieldName: string]: any }) { | ||
public setFieldValues(values: { [fieldName: string]: any }, pristineValues = false) { | ||
const fieldNames = Object.keys(values); | ||
fieldNames.forEach((fieldName) => { | ||
this.values[fieldName] = values[fieldName]; | ||
delete this.validationErrors[fieldName]; | ||
this.updateFieldValue(fieldName, values[fieldName], pristineValues); | ||
}); | ||
|
||
this.triggerMultipleFieldListeners(fieldNames); | ||
} | ||
|
||
public setFieldValue(fieldName: string, value: any) { | ||
this.values[fieldName] = value; | ||
delete this.validationErrors[fieldName]; | ||
public setFieldValue(fieldName: string, value: any, pristineValue = false) { | ||
this.updateFieldValue(fieldName, value, pristineValue); | ||
|
||
this.triggerFieldListeners(fieldName); | ||
} | ||
|
||
private updateFieldValue(fieldName: string, value: any, pristineValue) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implicit any on pristine value |
||
this.fieldStates[fieldName] = { | ||
...this.fieldStates[fieldName], | ||
value, | ||
validationError: undefined, | ||
pristine: pristineValue | ||
}; | ||
} | ||
|
||
public setValidationErrors(errors: { [fieldName: string]: string }) { | ||
this.validationErrors = errors; | ||
const fieldNames = Object.keys(errors); | ||
|
||
const fieldNames = Object.keys(this.validationErrors); | ||
this.triggerMultipleFieldListeners(fieldNames); | ||
} | ||
fieldNames.forEach((fieldName) => { | ||
this.fieldStates[fieldName] = { | ||
...this.fieldStates[fieldName], | ||
validationError: errors[fieldName] | ||
}; | ||
}); | ||
|
||
public getValidationError(fieldName: string): string | undefined { | ||
return this.validationErrors[fieldName]; | ||
this.triggerMultipleFieldListeners(fieldNames); | ||
} | ||
|
||
public listenForFieldChange(fieldName: string, callback: (value: any) => void) { | ||
public listenForFieldChange(fieldName: string, callback: (value: FieldState) => void) { | ||
if (typeof this.fieldListeners[fieldName] === 'undefined') { | ||
this.fieldListeners[fieldName] = []; | ||
} | ||
|
@@ -53,7 +65,7 @@ export class Form { | |
|
||
private triggerFieldListeners(fieldName: string) { | ||
if (typeof this.fieldListeners[fieldName] !== 'undefined') { | ||
this.fieldListeners[fieldName].forEach((callback) => callback(this.getFieldValue(fieldName))); | ||
this.fieldListeners[fieldName].forEach((callback) => callback(this.getFieldState(fieldName))); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as React from 'react'; | ||
import {FormProps} from './bonn'; | ||
import {FieldState} from '../business/form'; | ||
export interface FieldProps { | ||
value: any; | ||
validationError: string | undefined; | ||
|
@@ -20,7 +21,9 @@ export function Field<Props>(WrappedComponent: IncomingField<Props>, | |
return class extends React.Component<Props & OwnProps & FormProps, { value: any }> { | ||
|
||
public state: { value: any } = { | ||
value: this.props.form.getFieldValue(this.getFieldName()) | ||
value: (typeof this.props.form.getFieldState(this.getFieldName()) !== 'undefined') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be better to put this in a seperate method, unreadable like this, if you do that you can extract the |
||
? (this.props.form.getFieldState(this.getFieldName()) as FieldState).value | ||
: '' | ||
}; | ||
|
||
public getFieldName(): string { | ||
|
@@ -36,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); | ||
this.props.form.setFieldValue(this.getFieldName(), this.props.value, true); | ||
} | ||
} | ||
|
||
public componentWillUpdate(nextProps: Props & OwnProps & FormProps) { | ||
if (typeof nextProps.value !== 'undefined' && this.props.value !== nextProps.value) { | ||
this.props.form.setFieldValue(this.getFieldName(), nextProps.value); | ||
this.props.form.setFieldValue(this.getFieldName(), nextProps.value, true); | ||
} | ||
} | ||
|
||
|
@@ -55,14 +58,15 @@ export function Field<Props>(WrappedComponent: IncomingField<Props>, | |
} | ||
|
||
private handleChange(value: any) { | ||
this.props.form.setFieldValue(this.getFieldName(), value); | ||
this.props.form.setFieldValue(this.getFieldName(), value, false); | ||
} | ||
|
||
public render() { | ||
const fieldState = this.props.form.getFieldState(this.getFieldName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we storing a copy of the value if we're still going to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. This is following the initial design we had and maybe should change. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be the textbook use-case for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we would push the local state to the bonn form, so instead of setting the local state in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ehm, this is already what we're doing.. I just say we remove the |
||
return <WrappedComponent | ||
{...this.props} | ||
value={this.state.value} | ||
validationError={this.props.form.getValidationError(this.getFieldName())} | ||
validationError={(typeof fieldState !== 'undefined') ? fieldState.validationError : undefined} | ||
onChange={this.handleChange.bind(this)} | ||
/>; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not digging the magic third boolean, why not introduce something like a
resetFieldValues
method