form icon indicating copy to clipboard operation
form copied to clipboard

Please add prevProps to mapPropsToFields.

Open indigostar-kr opened this issue 7 years ago • 1 comments

If Form#create(options) currently has mapPropsToFields, componentWillReceiveProps always update mapPropsToFields results in FieldStore

However, there are times when you want to apply FieldStore only when the value of a specific Props has changed.

So I want mapPropsToFields to have props and prevProps.

ex)

public componentWillReceiveProps (nextProps) {
  if (mapPropsToFields) {
    const fields = mapPropsToFields (nextProps, this.props)
    if (fields !== null) this.fieldsStore.updateFields (fields)
  }
}

https://github.com/react-component/form/blob/2.4.1/src/createBaseForm.js#L87

indigostar-kr avatar Feb 01 '19 04:02 indigostar-kr

I am currently using it as a hack.

public static create<TOwnProps>(options?: FormCreateOption<TOwnProps>) {
    return <P extends FormComponentProps>(
      component: React.ComponentType<P>
    ): React.ComponentClass<
      RcBaseFormProps & Omit<P, keyof FormComponentProps>
    > => {
      const FormComponent = AntForm.create<TOwnProps>(options)(component)
      const mapPropsToFields = options ? options.mapPropsToFields : undefined

      // // HACK mapPropsToFields(props) => {} TO mapPropsToFields(props, prevProps) => null | {}
      // // If the result of mapPropsToFields is null, the repository is not updated.
      FormComponent.prototype.componentWillReceiveProps = function(
        nextProps: any
      ) {
        if (mapPropsToFields) {
          const fields = mapPropsToFields(nextProps as any, this.props as any)
          if (fields) this.fieldsStore.updateFields(fields)
        }
      }

      return FormComponent
    }
  }
`

indigostar-kr avatar Feb 22 '19 04:02 indigostar-kr