order: 2 group: order: 6 title: API

toc: content

Schema

schema 用于描述表单的基本信息、结构和校验。schema 在结构上遵循 JSON Schema 国际规范

Form-render 中有三种主要的表单元素类型:itemobjectlist

  • item:即最基本的输入框,选择框等
  • object:一个包含其他元素的 block,可用于表单项的分类
  • list:可动态增减的表单项
  1. // 一个基本的 scheme 结构
  2. const schema = {
  3. displayType: 'row',
  4. labelWidth: 130,
  5. type: 'object', // schema 最顶层的 type 总是 object
  6. properties: {
  7. // 一个 item
  8. url: {
  9. title: 'url输入框',
  10. placeholder: '//www.taobao.com',
  11. type: 'string',
  12. format: 'url',
  13. required: true,
  14. },
  15. // 一个 object
  16. contact: {
  17. type: 'object',
  18. properties: {
  19. phone: {
  20. title: '电话',
  21. type: 'string',
  22. },
  23. email: {
  24. title: '邮箱',
  25. type: 'string',
  26. },
  27. },
  28. },
  29. // 一个 list
  30. peopleList: {
  31. title: '人员列表',
  32. type: 'array',
  33. items: {
  34. type: 'object',
  35. properties: {
  36. name: {
  37. title: '姓名',
  38. type: 'string',
  39. },
  40. },
  41. },
  42. },
  43. },
  44. };

Schema

控制整体表单的一些属性。

type

  • 类型:object

schema 的 type 必须为 object

properties

  • 类型:Record<string, object>

表单的所有元素放在这里。

displayType

  • 类型:'row' | 'column'

Label 与 Field 的展示关系,row 表示并排展示,column 表示两排展示。必须写在 schema 顶层。

labelWidth

  • 类型:number | string
  • 默认:110

label 的宽度,数字值单位为 px,也可使用 '20%'/'2rem' 等,写在 schema 顶层时代表表单整体的 label 宽度,也可以写在 item 中,修改单个 item 的 label 宽度。

Item

基本表单元素的配置项。

bind

  • 类型:string | string[] | false

当服务端接口获取的字段与你希望的表单展示结构不同时,可以通过 bind 字段绑定的方式指明表单的某个字段对应的是外部数据的另一个字段。详细例子见 “表单与外界的交互” 的例 3。

用户并不希望纯展示的字段也出现在表单中,此时,使用 bind: false 可避免字段在提交时出现。

请不要 bind 一个数组结构下面的字段,目前没有对此进行处理,所以会无效(在未来这个限制会解除)。

theme

  • 类型:string
  • 值: default | card | tile,详见这里

设置嵌套表单的主题样式

className

  • 类型:string

当前元素的 classname。

title

  • 类型:string

一个 item 的 label。

注意 title 为""时占位,title 不写时不占位。如下例,通过选择性不使用 title,达到展示效果。
  1. import React from 'react';
  2. import FR from '../demo/FR';
  3. import { titleTrick } from '../json/schema';
  4. export default () => <FR schema={titleTrick} />;

description

  • 类型:string

item 的描述信息。

descType

  • 版本:^1.7.0
  • 类型:'text' | 'icon'

当 displayType 为 row 时,无作用;但当 displayType 为 column (默认值)时,描述信息(description)的一般展示为文案形式,如果设定 descType 为 icon, 则会使用 tooltip 形式。

dependencies

  • 版本:^1.6.2
  • 类型:string[]

当依赖的元素更新时,会触发本元素的重新渲染,用于复杂的表单联动。

  1. const schema = {
  2. // ...
  3. list: {
  4. title: '对象数组',
  5. type: 'array',
  6. items: {
  7. type: 'object',
  8. properties: {
  9. select1: {
  10. title: '单选',
  11. type: 'string',
  12. enum: ['a', 'b', 'c'],
  13. enumNames: ['早', '中', '晚'],
  14. },
  15. select2: {
  16. title: '单选2(自定义组件)',
  17. type: 'string',
  18. widget: 'select2', // select2 为自定义组件,具体实现与dependencies的讨论无关,不赘述
  19. dependencies: ['list[].select1'],
  20. },
  21. },
  22. },
  23. },
  24. };

disabled

  • 类型:boolean

组件是否禁用状态。

extra

  • 类型:string

用于在元素下展示更多说明信息,extra 可以是 html string,也可以是纯文案,会展示在元素下面一行紧贴。

  1. const schema = {
  2. // ...
  3. rule: {
  4. title: '单选',
  5. type: 'string',
  6. extra: "<a href='xxx'>详细规范</a>",
  7. },
  8. };

enum

  • 类型:string | number

只在选择类组件中使用,用于描述枚举值的值和文案,与 enumNames 配合使用。

enumNames

  • 类型:string | number

只在选择类组件中使用,用于描述枚举值的值和文案,与 enum 配合使用。

旧版 form-render 会默认选中第一项,但是新版除非通过 default 指明,否则不会选中任何一项,且初始值是 undefined
  1. const schema = {
  2. // ...
  3. // 单选
  4. select1: {
  5. title: '单选',
  6. type: 'string',
  7. enum: ['hz', 'wh', 'gy'],
  8. enumNames: ['杭州', '武汉', '贵阳'],
  9. default: 'hz',
  10. },
  11. // 多选
  12. select2: {
  13. title: '多选',
  14. type: 'array',
  15. items: {
  16. type: 'string',
  17. },
  18. enum: ['hz', 'wh', 'gy'],
  19. enumNames: ['杭州', '武汉', '贵阳'],
  20. },
  21. };

format

  • 类型:'image' | 'textarea' | 'color' | 'email' | 'url' | 'dateTime' | 'date' | 'time' | 'upload'
  • 详细:用来描述输入框的格式,辅助 type 一同用于判断使用哪个组件来渲染,以及校验表单数据

    1. const schema = {
    2. // ..
    3. // 默认 input
    4. input: {
    5. title: "简单输入框",
    6. type: "string",
    7. }
    8. // textarea
    9. textarea: {
    10. title: "简单文本编辑框",
    11. type: "string",
    12. format: "textarea"
    13. }
    14. // 颜色组件
    15. color: {
    16. title: "颜色选择",
    17. type: "string",
    18. format: "color"
    19. }
    20. // 日期组件
    21. date: {
    22. title: "日期选择",
    23. type: "string",
    24. format: "date" // "dateTime"
    25. }
    26. // 时间组件
    27. time: {
    28. title: "时间选择",
    29. type: "string",
    30. format: "time"
    31. }
    32. // 图片链接组件
    33. image: {
    34. title: "图片展示",
    35. type: "string",
    36. format: "image"
    37. }
    38. }

hidden

  • 类型:boolean

组件是否隐藏状态。

min

  • 类型:number

typearray 时,代表最小项数;当 typestring 时,代表最少字数;当 typenumber 时,代表最小值。

max

  • 类型:number

typearray 时,代表最大项数;当 typestring 时,代表最多字数;当 typenumber 时,代表最大值。

props

  • 类型:object

当基础字段不够描述组件的展示时,使用 props 字段作为扩展。props 的具体属性可以查询 antd 的对应组件文档。所有 props 中的属性都会直接透传给组件。

  1. const schema = {
  2. // ...
  3. url: {
  4. title: '网址',
  5. type: 'string',
  6. props: {
  7. // 以下属性会透传给 Input
  8. prefix: 'https://',
  9. suffix: '.com',
  10. },
  11. },
  12. };

order

  • 类型:number
  • 详细:用于对 schema 进行排序,值越小越靠前:
  1. "input1": {
  2. "title": "输入框",
  3. "type": "string",
  4. "order": 2
  5. }
  6. "input2": {
  7. "title": "优先渲染",
  8. "type": "string",
  9. "order": 1
  10. }

凡是包含 props(不区分大小写)的 schema 的 key 值,都会原样传递给自定义组件。方便在自定义组件中分类 props。

  1. const schema = {
  2. // ...
  3. percentInput: {
  4. title: '百分比输入',
  5. type: 'number',
  6. props: {
  7. showInput: false,
  8. },
  9. // inputProps 会原样传给自定义组件
  10. inputProps: {
  11. suffix: '%',
  12. },
  13. // percentProps 会原样传给自定义组件
  14. percentProps: {
  15. step: 10,
  16. },
  17. },
  18. };
  19. // 传递给自定义组件的 props 为
  20. const {
  21. type: 'number',
  22. showInput: false,
  23. inputProps: {
  24. suffix: '%'
  25. },
  26. percentProps: {
  27. step: 10
  28. }
  29. } = props;

order

  • 类型:number

用于对 item 进行排序,值越小越靠前。

  1. const schema = {
  2. // ...
  3. input1: {
  4. title: '输入框',
  5. type: 'string',
  6. order: 2,
  7. },
  8. input2: {
  9. title: '输入框但是更靠前',
  10. type: 'string',
  11. order: 1,
  12. },
  13. };

placeholder

  • 类型:string

Input 等元素的 placeholder。

注意 placeholder 的值遵循底层对应组件所需要的值的书写规范。
  1. const schema = {
  2. // ...
  3. dateRange: {
  4. title: '日期范围',
  5. type: 'range',
  6. format: 'dateTime',
  7. placeholder: ['开始', '结束'],
  8. },
  9. };

rules

  • 类型:object[]

用于描述细致的、定制化的校验,支持 async-validator 所有的 api。Form-render 为常用规则提供了语法糖,如:typeformatminmaxrequired等。可通过 validateMessages 定制校验文案。

  1. const schema = {
  2. // ...
  3. count: {
  4. title: '代号',
  5. type: 'string',
  6. rules: [
  7. {
  8. pattern: '^[A-Za-z0-9]+$',
  9. message: '只允许填写英文字母和数字',
  10. },
  11. ],
  12. },
  13. zip: {
  14. title: 'zip code',
  15. type: 'string',
  16. rules: [{ len: 8, message: '长度不能超过8' }],
  17. },
  18. };

当常规字段不能满足时,可使用 validator 动态校验,详见 async-validator

  1. const schema = {
  2. // ...
  3. name: {
  4. type: 'string',
  5. required: true,
  6. rules: [{ validator: (rule, value) => value === 'muji' }],
  7. },
  8. };

required

  • 类型:boolean

用于判断是否必填,会校验表单数据。

  1. const schema = {
  2. // ...
  3. requiredInput: {
  4. title: '输入框',
  5. type: 'string',
  6. required: true,
  7. },
  8. };

readOnly

  • 类型:boolean

组件是否只读状态。

type

  • 类型:'string' | 'number' | 'boolean' | 'array' | 'range' | 'html'
  • 详细:type 描述里组件的值的数据类型。用于校验数据类型,也用于判断使用哪个组件来渲染,以及校验表单数据。
  1. const schema = {
  2. // ...
  3. date: {
  4. title: '日期范围',
  5. type: 'range',
  6. format: 'dateTime',
  7. placeholder: ['开始', '结束'],
  8. },
  9. html: {
  10. title: '纯字符串',
  11. type: 'html',
  12. default: 'hello world',
  13. },
  14. };

readOnlyWidget

  • 类型:string

指定只读模式下用哪个自定义组件渲染。

readOnly=true 的情况,FormRender 默认使用 html 组件渲染。特殊情况 html 组件无法满足需求,此时通过指明 readOnlyWidget 的方式自定义渲染。
  1. const schema = {
  2. // ...
  3. name: {
  4. title: '单选',
  5. type: 'string',
  6. widget: 'myWidget', // 指明使用 myWidget 来渲染
  7. readOnlyWidget: 'myReadOnlyWidget', // 指明在只读模式使用 myReadOnlyWidget 来渲染
  8. },
  9. };

width

  • 类型:string

单个元素的展示宽度(带 label 一起),例如 '20%'

widget

  • 类型:string

指定使用哪个组件来渲染,是优先级最高的一个字段。用于明确指定使用某个内置组件自定义组件

  1. const schema = {
  2. // ...
  3. address: {
  4. title: '单选',
  5. type: 'string',
  6. enum: ['hz', 'wh', 'gy'],
  7. enumNames: ['杭州', '武汉', '贵阳'],
  8. widget: 'select', // 明确指明使用下拉选择组件
  9. },
  10. };

List

列表组件的配置项。

items

只在列表类组件中使用,描述 Array 中每个 item 的结构。items 目前只支持是对象。

  1. const schema = {
  2. // ...
  3. tickets: {
  4. title: '对象数组',
  5. type: 'array',
  6. min: 1,
  7. max: 3,
  8. widget: 'cardList',
  9. items: {
  10. type: 'object',
  11. properties: {
  12. tickets: {
  13. title: '门票数',
  14. type: 'number',
  15. },
  16. },
  17. },
  18. },
  19. };

min

  • 类型:number

代表列表的最小项数。

max

  • 类型:number

代表列表的最大项数。

props

列表组件的额外属性,除了下列内置属性外,根据 widget 的不同,可选择更多属性传入。

props.buttons

  • 类型:{ html: string, callback: string }[]

用于添加更多列表操作按钮。

  1. const schema = {
  2. // ...
  3. list: {
  4. type: 'array',
  5. props: {
  6. buttons: [
  7. {
  8. html: 'excel 导入',
  9. // Form-render 会到 window.someCallback 上寻找回调函数
  10. // 此回调函数可接受参数 value和 schema, 返回值会作为新的列表值
  11. callback: 'someCallback',
  12. },
  13. {
  14. // html 字段可使用正常的 string 值,也可以使用任何 html 片段
  15. html: "<span style='color: red'>拉取数据</span>",
  16. callback: 'getData',
  17. },
  18. ],
  19. },
  20. },
  21. };
  22. // value: 整个数组的值
  23. // onChange: 传入改变后的数组值,触发state更新
  24. window.someCallback = ({ value, onChange, schema }) => {
  25. onChange([...value, { a: 'hello' }]);
  26. };

props.hideDelete

  • 类型:boolean

隐藏删除按钮。

props.hideAdd

  • 类型:boolean

隐藏新增/复制按钮。

props.hideCopy

  • 类型:boolean

隐藏复制按钮。

props.hideMove

  • 类型:boolean

隐藏上下移动 item 的按钮。

props.hideTitle

  • 类型:boolean

仅当 widgetsimpleList 可用,隐藏 title,展示更紧凑。

props.type

  • 类型:editable-card

仅当 widgettabList 可用,切换列表组件为可新增/关闭的 Tabs 标签页。

  1. const schema = {
  2. // ...
  3. list: {
  4. type: 'array',
  5. widget: 'tabList',
  6. props: {
  7. type: 'editable-card'
  8. tabName: '项目'
  9. }
  10. }
  11. }

props.tabName

  • 类型:string

仅当 widgettabList 可用,代表选项卡头文案,对应 antd 中 Tabs 的 tab 属性。

  1. const schema = {
  2. // ...
  3. list: {
  4. type: 'array',
  5. widget: 'tabList',
  6. props: {
  7. tabName: '项目',
  8. },
  9. },
  10. };

other table props

仅当 widgettableListdrawerListvirtualList 时,可传入所有 antd table 的 props。

  1. const schema = {
  2. // ...
  3. list: {
  4. type: 'array',
  5. widget: 'tableList',
  6. props: {
  7. // 可传入 antd table 的 props
  8. scrollX: 2000,
  9. size: 'small',
  10. },
  11. },
  12. };

itemProps

列表中每一项的属性。

itemProps.addBtnProps

  • 类型:object

“新增一条”按钮的样式和文案修改,所有 antd 的 button 的 props 都支持传入。

  1. const schema = {
  2. // ...
  3. itemProps: {
  4. addBtnProps: {
  5. children: '新增企业',
  6. type: 'primary',
  7. },
  8. },
  9. };

other columns itemProps

仅当 widgettableListdrawerListvirtualList 时,所有 columns 的单个配置都可以透传,会作用到 columns 的所有 item

  1. const schema = {
  2. // ...
  3. list: {
  4. type: 'array',
  5. itemProps: {
  6. width: 200, // 设置 table 的所有单元格(除了“操作”那一列)宽度为 200 px
  7. },
  8. },
  9. };

widget

  • 类型:'cardList' | 'simpleList' | 'tableList' | 'drawerList' | 'tabList' | 'virtualList'
  • 默认:cardList

Form-render 给出 5 种列表的展示形式,充分满足从极简到复杂的列表需求,详见 Playground。根据 widget 类型的不同,可以在 props 中传入更多额外属性。

Object

对象类型组件的配置。

collapsed

  • 类型:boolean
  • 默认:false

只在嵌套的对象类型组件中使用,用于控制面板是否折叠。

  1. const schema = {
  2. // ...
  3. input: {
  4. type: 'object',
  5. properties: {
  6. objectName: {
  7. type: 'object',
  8. description: '这是一个对象类型',
  9. collapsed: false,
  10. properties: {
  11. input1: {
  12. title: '简单输入框',
  13. type: 'string',
  14. required: true,
  15. },
  16. },
  17. },
  18. },
  19. },
  20. };

properties

只在对象组件中使用,properties 用于包裹对象的子属性。

  1. const schema = {
  2. // ...
  3. ticket: {
  4. title: '对象',
  5. type: 'object',
  6. properties: {
  7. tickets: {
  8. title: '门票数',
  9. type: 'number',
  10. },
  11. },
  12. },
  13. };