cisen / blog

Time waits for no one.
135 stars 20 forks source link

finalform相关和源码 #282

Open cisen opened 5 years ago

cisen commented 5 years ago

说明

cisen commented 5 years ago

源码

流程

final-form

final-form的,总的来说就是一个发布订阅模块,只是变量很多

react-final-form-hooks

问答

好东西学习

export const ReactFinalFormContext = React.createContext(null)

export const getDisplayName = Component => {
  const displayName = Component.displayName || Component.name || 'Component'

  return `ReactFinalForm(${displayName})`
}
// 一个高阶组件
export const withReactFinalForm = Component => {
  return class extends React.Component {
    // 返回一个组件,组件的名字还可以自己定义
    static displayName = getDisplayName(Component)

    render() {
    // 这样相当于直接生成context用
    /* <ThemeContext.Consumer>
      {theme => (
      <div><div>)
      }
</ThemeContext.Consumer>
*/
      return React.createElement(ReactFinalFormContext.Consumer, {
       // reactFinalForm是context直接传下来的
        children: reactFinalForm =>
          // 传给子组件
          React.createElement(Component, {
            reactFinalForm,
            ...this.props
          })
      })
    }
  }
}
cisen commented 5 years ago

数据结构

createForm的config

let {
    debug,
    destroyOnUnregister,
    keepDirtyOnReinitialize,
    initialValues,
    mutators,
    onSubmit,
    validate,
    validateOnBlur
  } = config

createForm返回的api结构如下

const api: FormApi = {
  batch: (fn: () => void) => {
  },
  blur: (name: string) => {
  },
  change: (name: string, value: ?any) => {
  },
  focus: (name: string) => {
  },
  mutators: mutatorsApi,
  getFieldState: name => {
  },
  getRegisteredFields: () => Object.keys(state.fields),
  getState: () => calculateNextFormState(),
  initialize: (data: Object | ((values: Object) => Object)) => {
  },
  isValidationPaused: () => validationPaused,
  pauseValidation: () => {
  },
  registerField: (
    name: string,
    subscriber: FieldSubscriber,
    subscription: FieldSubscription = {},
    fieldConfig?: FieldConfig
  ): Unsubscribe => {
  },
  reset: (initialValues = state.formState.initialValues) => {
  },
  resumeValidation: () => {
  },
  setConfig: (name: string, value: any): void => {
  },
  submit: () => {
  },
  subscribe: (
    subscriber: FormSubscriber,
    subscription: FormSubscription
  ): Unsubscribe => {
  }
}

createFrom闭包初始化的state数据

const state: InternalState = {
    subscribers: { index: 0, entries: {} },
    fieldSubscribers: {},
    fields: {},
    formState: {
      dirtySinceLastSubmit: false,
      errors: {},
      initialValues: initialValues && { ...initialValues },
      invalid: false,
      pristine: true,
      submitting: false,
      submitFailed: false,
      submitSucceeded: false,
      valid: true,
      validating: 0,
      values: initialValues ? { ...initialValues } : {}
    },
    lastFormState: undefined
  }

每个field的数据结构

state.fields[name] = {
          active: false,
          blur: () => api.blur(name),
          change: value => api.change(name, value),
          data: {},
          focus: () => api.focus(name),
          isEqual: (fieldConfig && fieldConfig.isEqual) || tripleEquals,
          lastFieldState: undefined,
          modified: false,
          name,
          touched: false,
          valid: true,
          validateFields: fieldConfig && fieldConfig.validateFields,
          validators: {},
          visited: false
        }