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

Keep track if a field is pristine or not. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

DJWassink
Copy link
Member

🎉 🌮

@DJWassink DJWassink requested a review from JBlaak April 22, 2017 09:08

this.triggerFieldListeners(fieldName);
}

private updateFieldValue(fieldName: string, value: any, pristineValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implicit any on pristine value

}

public setFieldValues(values: { [fieldName: string]: any }) {
public setFieldValues(values: { [fieldName: string]: any }, pristineValues = false) {
Copy link
Contributor

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

});

this.triggerMultipleFieldListeners(fieldNames);
}

public initialiseFieldValue(fieldName: string, value: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialize?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uk vs us? which do we choose? initialize is US definition with a s is UK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah true, I think we should opt for US, I'm not so much of a tea drinker

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ya bloody imbecile


this.triggerFieldListeners(fieldName);
}

private updateFieldValue(fieldName: string, value: any, pristineValue: boolean) {
Copy link
Contributor

@JBlaak JBlaak Apr 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm expecting the value to be named pristineValue containing the value, do you mean isPristineValue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

@@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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 getFieldState result to a seperate variable and remove the need for a second call to the method

@@ -59,10 +62,11 @@ export function Field<Props>(WrappedComponent: IncomingField<Props>,
}

public render() {
const fieldState = this.props.form.getFieldState(this.getFieldName());
Copy link
Contributor

Choose a reason for hiding this comment

The 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 getFieldState in the render method?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 getFieldState gets called in favor of the getValidationError we had before. Do we wan't to store the validationError localy also?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be the textbook use-case for forceUpdate, since our state is in another place.. so we can just remove local state and call the getFieldState in the render function https://facebook.github.io/react/docs/react-component.html#forceupdate

Copy link
Member Author

Choose a reason for hiding this comment

The 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 keyPressHandler we push it to bonn and let it forceUpdate any listening components? Sounds a bit tricky tho. Gotta be carefull to not render too much (e.g. having mutliple things inside a field would already force multiple renders). But I guess that would also mean a user mis-used the field decorator.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 state from the Field and Listener components and introduce a this.forceUpdate() within the listenForFieldChange callback. Then in the render we just fetch the values we need.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants