安装

npm i antd -S
import { Icon, table } from ‘antd’;

定制主题:https://ant-design.gitee.io/docs/react/customize-theme-cn
主题颜色变量:https://ant-design.gitee.io/docs/spec/colors-cn

antd 注意事项 & 问题报错 · 语雀

功能实现 & 应用

Form表单中验证应用

  1. validatorFn = (rule, value, callback) => {
  2. const reg = /[1]+$/;
  3. if (value && !reg.test(value)) {
  4. callback('请输入正确的验证应用');
  5. } else {
  6. callback();
  7. }
  8. }
  9. <FormItem {...loayout} label="验证应用">
  10. {getFieldDecorator('yingyong', {
  11. rules: [
  12. { required: true, message: '验证应用不能为空' },
  13. { validator: this.validatorFn }
  14. ]
  15. })(
  16. <Input placeholder="请输入验证应用">
  17. )}
  18. </FormItem>

Form 动态增减表单项

效果:动态表单.gif 输出: 动态表单输出.png

  1. import React from 'react';
  2. import { Form, Button, Input } from 'antd';
  3. const TestForm: React.FC<{}> = () => {
  4. return (
  5. <div>
  6. <Form form={addForm}>
  7. <Form.List name="formList">
  8. {(fields, { add, remove }) => {
  9. return (
  10. <div>
  11. {fields.map((field, index) => (
  12. <Form.Item {...field}>
  13. <Input placeholder="请输入" style={{ width: 248 }} />
  14. </Form.Item>
  15. ))}
  16. <Button
  17. type="dashed"
  18. onClick={() => {
  19. add();
  20. }}
  21. style={{ width: 248 }}
  22. >
  23. +
  24. </Button>
  25. </div>
  26. );
  27. }}
  28. </Form.List>
  29. </Form>
  30. <Button
  31. style={{ marginTop: 24 }}
  32. onClick={() => {
  33. addForm.validateFields().then((values) => {
  34. console.log(values, 'submit');
  35. });
  36. }}
  37. >
  38. submit
  39. </Button>
  40. </div>
  41. );
  42. };
  43. export default TestForm;