@01group/form-builder 中文文档教程

发布于 3年前 浏览 22 项目主页 更新于 3年前

@01group/form-builder

这是一个轻量级的 React Hook 库,用于简化构建 formik 表单的方法。

Story behind

在我们的案例中,我们经常发现我们的开发过程是夫妻为了建立形式而进行的过程。

我们找到了一种更好的方法来提高效率。

Installation

yarn add @01group/form-builder

或者如果你更喜欢选择 npm

npm install -S @01group/form-builder

Usage

这个主库正在使用一个反应钩子 useZeroForm。 它将返回 fieldsvalues

Description

fields: 用于处理表单的对象字段列表

values: 值的对象,可以是在formik<中填充initialValues /code>

Example

Nb. 以下是我们的方法。 如果您认为这不够适合您,您可以使用自己的。

import { Formik, Form } from 'formik'
import { useZeroForm, ZeroFormTypes } from '@01group/form-builder';

export const DATA = {
  name: '',
  province_id: '',
  city_id: '',
  date_of_birth: '',
};

export const generalOptions = {
  name: {
    type: 'text',
    label: 'Your name',
    isRequired: true,
  },
  province_id: {
    type: 'select',
    label: 'Province',
    placeholder: 'Select a province',
  },
  city_id: {
    type: 'select',
    label: 'City',
    placeholder: 'Select a city',
  },
};

export function Example() {
  const { fields, values } = useZeroForm({ data: DATA as ZeroFormTypes, options: generalOptions });

  return (
    <Formik
      initialValues={values}
      validationSchema={validationSchema}
      onSubmit={handleSubmit}
      enableReinitialize
    >
      <Form>
        {(fields ?? []).map((field, i) => (
          <GenerateField key={i} isReadonly={isReadonly} {...field} />
        ))}
      </Form>
    </Formik>
  )
}

export function GenerateField(props: ZeroFieldItem) {
  const inputProps = {
    id: props.name,
    name: props.name,
    label: props.label,
    placeholder: props.placeholder,
    isRequired: props.isRequired,
    isReadOnly: props.isReadonly,
  };
  const selectProps = {
    ...inputProps,
    items: props.selectOptions ?? [],
  };

  const elements: Partial<Record<Partial<ZeroFieldTypes>, JSX.Element>> = {
    select: <Select {...selectProps} />,
    text: <TextField {...inputProps} />,
    row: <Textarea {...inputProps} />,
    file: <Dropzone width="full" {...inputProps} />,
  };

  return elements[props.type] ?? <TextField {...inputProps} />;
}

@01group/form-builder

This is a lightweight react hook library to simplify approach in building formik form.

Story behind

In our cases, we often found our development process was couples doing process in order to building form.

We find a better approach to make this more efficiency.

Installation

yarn add @01group/form-builder

or if you prefer to choose npm

npm install -S @01group/form-builder

Usage

This main library is using a react hook useZeroForm. It will return fields and values.

Description

fields: list of object field that useful to handle form

values: object of value, it could be to fill initialValues in formik

Example

Nb. Below is our approach. You might used your own, if you think this not suit enough for you.

import { Formik, Form } from 'formik'
import { useZeroForm, ZeroFormTypes } from '@01group/form-builder';

export const DATA = {
  name: '',
  province_id: '',
  city_id: '',
  date_of_birth: '',
};

export const generalOptions = {
  name: {
    type: 'text',
    label: 'Your name',
    isRequired: true,
  },
  province_id: {
    type: 'select',
    label: 'Province',
    placeholder: 'Select a province',
  },
  city_id: {
    type: 'select',
    label: 'City',
    placeholder: 'Select a city',
  },
};

export function Example() {
  const { fields, values } = useZeroForm({ data: DATA as ZeroFormTypes, options: generalOptions });

  return (
    <Formik
      initialValues={values}
      validationSchema={validationSchema}
      onSubmit={handleSubmit}
      enableReinitialize
    >
      <Form>
        {(fields ?? []).map((field, i) => (
          <GenerateField key={i} isReadonly={isReadonly} {...field} />
        ))}
      </Form>
    </Formik>
  )
}

export function GenerateField(props: ZeroFieldItem) {
  const inputProps = {
    id: props.name,
    name: props.name,
    label: props.label,
    placeholder: props.placeholder,
    isRequired: props.isRequired,
    isReadOnly: props.isReadonly,
  };
  const selectProps = {
    ...inputProps,
    items: props.selectOptions ?? [],
  };

  const elements: Partial<Record<Partial<ZeroFieldTypes>, JSX.Element>> = {
    select: <Select {...selectProps} />,
    text: <TextField {...inputProps} />,
    row: <Textarea {...inputProps} />,
    file: <Dropzone width="full" {...inputProps} />,
  };

  return elements[props.type] ?? <TextField {...inputProps} />;
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文