qiniu / formstate-x

Manage state of form with ease.
https://qiniu.github.io/formstate-x
MIT License
34 stars 10 forks source link

reset for FormState #31

Closed nighca closed 3 years ago

nighca commented 4 years ago

It may be more reasonable to do real reset in FormState instance method reset.

Current behavior

Call method reset of each field:

https://github.com/qiniu/formstate-x/blob/f6a3e3ed6835a6560d389298621d8a9429c3916f/src/formState.ts#L174-L176

Expected behavior

Reset fields' value (use value of initialFields to replace current value of $) & call method reset of each field.

class FormState {

  private initialFields

  constructor(initialFields) {
    // we should do shallow clone here in case of fields' in-place modification 
    this.initialFields = shallowClone(initialFields)
  }

  reset() {
    // reset fields' value
    this.$ = this.initialFields
    // reset each field's value
    this.fields.forEach(
      field => field.reset()
    )
  }

}