1.0.5 • Published 3 years ago

react-form-art v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

react-form-design

Demo online

在线查看

install

npm i react-form-art -S

API

Form

属性说明类型默认值
classNameform 标签类名string''
initValues表单项统一初始化Object{}
onFinish表单提交触发Function{}
onFinishFailed表单提交触发Function{}
ref可获取表单仓库实例方法React.RefObject{}
FormItem
属性说明默认值版本
classNameFormItem 容器的类名string-
labelClassNamelabel 的类名string-
controllClassName表单项所在容器的类名string-
requiredClassName必填标识的类名string-
requiredIcon自定义必填标识 iconReact.ReactNode-
labellabel 标签的文本React.ReactNode-
labelWidthlabel 标签的文本number-
name表单项字段名称string-
rules表单项校验逻辑, message 属性针对 rule 为 RegExp 触发{ rule: RegExp 或者 Function:(value, call)=> {call(null)}, message: '' }-
trigger表单项更改的方法名string'onChange'
validateTrigger表单项规则校验触发的方法名string'onBlur'
required表单项是否必填Booleanfalse
messageShow表单项错误提示类型, 错误的时候提示,成功的时候提示,都提示'failed'/'success'/'all'/'hidden''all'

form 实例的方法

function Demo() {
  const form = React.useRef(null)
  return <Form ref={form}>...</Form>
}

form.current 的 API 如下:

属性说明类型
getFieldValue传入表单主键,返回表单值(key: String)=> FieldValue
getFieldsValue获取所有表单值Object => FieldValues
resetFields重置表单所有值values:Object => void
setFields设置表单值values:Object => void
submit提交方法,回掉参数是否校验完成(boolean, FieldValues) => void

基本使用

import { InputItem, Button, Picker } from "antd-mobile";
import Form from "react-form-art";
import React from "react";

const FormItem = Form.FormItem
const seasons = [
  [
    {
      label: '2013',
      value: '2013',
    },
    {
      label: '2014',
      value: '2014',
    },
  ],
  [
    {
      label: '春',
      value: '春',
    },
    {
      label: '夏',
      value: '夏',
    },
  ],
];

function Demo() {
  const form = React.useRef(null)
  React.useEffect(() => {
    console.log(form.current, '<form.current')
  }, [])

  const handleClick = () => {
    form.current.submit((t) => {
      console.log(t)
    })
  }

  return <div style={{ padding: '50px 20px 0 20px' }} >
    <Form
      initialValues={{ name: '我是初始值' }}
      ref={form}
      onFinish
    >
      <FormItem
        label="我的名称"
        labelWidth={150}
        name="name"
        required
        rules={{
          rule: /[\u4e00-\u9fa5]/,
          message: '名称仅支持中文',
        }}
        validateTrigger="onBlur"
      >
        <InputItem
          placeholder="请输入名称(中文)"
        />
      </FormItem>

      <div style={{ color: 'blue', marginBottom: 20 }}>
        (我的名称)输入字数大于两个 的时候要选择夏季
      </div>

      <FormItem
        label="季节选择"
        labelWidth={150}
        name="yearSeason"
        validateTrigger="onOk"
        trigger="onOk"
        messageShow="failed"
        rules={{
          rule: (values, call) => {
            if (form.current.getFieldValue('name').length > 2) {
              if (values[1] !== '夏') {
                call('此时要选择夏季')
              }
            } else call(null)
          }
        }}
      >
        <Picker
          data={seasons}
          title="选择季节"
          cascade={false}
        >
          <List.Item arrow="horizontal" style={{ width: 200 }} />
        </Picker>
      </FormItem>

      <Button onClick={handleClick}>提交</Button>
    </Form>
  </div>
}

export default Demo;
1.0.2

3 years ago

1.0.1

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago