All files / src/form/_example FormWithReactFinalFormExample.jsx

77.27% Statements 17/22
37.5% Branches 9/24
93.33% Functions 14/15
75% Lines 15/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                          1x     10x     10x 10x                     1x                   1x             2x                 1x             2x                               2x     2x                                 3x       2x                         4x       2x                                                                
// @ts-nocheck
import React from "react";
import { Form } from "@tencent/tea-component/lib/form";
import { Input } from "@tencent/tea-component/lib/input";
import { RadioGroup, Radio } from "@tencent/tea-component/lib/radio";
import { CheckboxGroup, Checkbox } from "@tencent/tea-component/lib/checkbox";
import { Card } from "@tencent/tea-component/lib/card";
import { InputNumber } from "@tencent/tea-component/lib/inputnumber";
import { Button } from "@tencent/tea-component/lib/button";
 
// eslint-disable-next-line
import { Form as FinalForm, Field } from "react-final-form";
 
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
 
function getStatus(meta, validating) {
  Iif (meta.active && validating) {
    return "validating";
  }
  Eif (!meta.touched) {
    return null;
  }
  return meta.error ? "error" : "success";
}
 
export default function FormWithReactFinalFormExample() {
  async function onSubmit(values) {
    await sleep(1500);
    alert(JSON.stringify(values));
  }
 
  return (
    <div className="example-stage">
      <Card>
        <Card.Body>
          {/*
            `initialValuesEqual` 默认为 shallowEqual,如果初始值有多层,会导致重渲染
            https://github.com/final-form/react-final-form#initialvaluesequal-object-object--boolean
          */}
          <FinalForm
            onSubmit={onSubmit}
            initialValuesEqual={() => true}
            initialValues={{
              age: 18,
              hobbies: [],
            }}
          >
            {({ handleSubmit, validating, submitting }) => {
              return (
                <form onSubmit={handleSubmit}>
                  <Form.Title>表单验证</Form.Title>
                  <Form>
                    <Field
                      name="name"
                      validateOnBlur
                      validateFields={[]}
                      validate={async value => {
                        await sleep(1500);
                        return !value || value.length < 4
                          ? "昵称太短了哦"
                          : undefined;
                      }}
                    >
                      {({ input, meta }) => (
                        <Form.Item
                          required
                          label="昵称"
                          status={getStatus(meta, validating)}
                          message={
                            getStatus(meta, validating) === "error" &&
                            meta.error
                          }
                        >
                          <Input {...input} placeholder="你是谁" />
                        </Form.Item>
                      )}
                    </Field>
                    <Field
                      name="sex"
                      validateFields={[]}
                      validate={value => (!value ? "请选择性别" : undefined)}
                    >
                      {({ input, meta }) => (
                        <Form.Item
                          required
                          label="性别"
                          status={getStatus(meta)}
                          message={getStatus(meta) === "error" && meta.error}
                        >
                          <RadioGroup {...input}>
                            <Radio name="male">男</Radio>
                            <Radio name="female">女</Radio>
                          </RadioGroup>
                        </Form.Item>
                      )}
                    </Field>
                    <Field
                      name="age"
                      validateFields={[]}
                      validate={value =>
                        value < 18 ? "你好像还未成年哦" : undefined
                      }
                    >
                      {({ input, meta }) => (
                        <Form.Item
                          label="年龄"
                          status={meta.error ? "error" : "success"}
                          message={meta.error}
                        >
                          <InputNumber {...input} min={12} max={100} />
                        </Form.Item>
                      )}
                    </Field>
                    <Field
                      name="hobbies"
                      validateFields={[]}
                      validate={value =>
                        value.length < 1 ? "请至少选择一个哦" : undefined
                      }
                    >
                      {({ input, meta }) => (
                        <Form.Item
                          label="兴趣"
                          status={getStatus(meta)}
                          message="选择一项或多项爱好"
                        >
                          <CheckboxGroup {...input}>
                            <Checkbox name="code">编程</Checkbox>
                            <Checkbox name="web">抠图</Checkbox>
                            <Checkbox name="jinli">超越</Checkbox>
                          </CheckboxGroup>
                        </Form.Item>
                      )}
                    </Field>
                  </Form>
                  <Form.Action>
                    <Button
                      type="primary"
                      htmlType="submit"
                      loading={submitting}
                    >
                      提交
                    </Button>
                  </Form.Action>
                </form>
              );
            }}
          </FinalForm>
        </Card.Body>
      </Card>
    </div>
  );
}