order: 8 group: order: 3 title: 高级用法

toc: content

展示的最佳实践

displayType

  • 类型:’row’ | ‘column’ | ‘inline’
  • 默认值: ‘column’
  • 说明:用于控制标签的位置。没有特殊情况,一般建议使用默认的 display: column。注意 displayType 既是 props,又是 schema 的字段,可以
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = displayType => ({
  4. type: 'object',
  5. displayType: displayType,
  6. properties: {
  7. range1: {
  8. title: '日期',
  9. type: 'range',
  10. format: 'date',
  11. },
  12. objectName: {
  13. title: '对象',
  14. bind: 'obj',
  15. description: '这是一个对象类型',
  16. type: 'object',
  17. collapsed: false,
  18. properties: {
  19. input1: {
  20. title: '简单输入框',
  21. type: 'string',
  22. required: true,
  23. },
  24. select1: {
  25. title: '单选',
  26. type: 'string',
  27. enum: ['a', 'b', 'c'],
  28. enumNames: ['早', '中', '晚'],
  29. },
  30. },
  31. },
  32. },
  33. });
  34. export default () => (
  35. <div>
  36. <h2>display: row</h2>
  37. <Form schema={schema('row')} />
  38. <h2>display: column</h2>
  39. <Form schema={schema('column')} />
  40. </div>
  41. );

readOnly

新增了只读模式,在 \

组件上用 props 声明

  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. displayType: 'row',
  6. properties: {
  7. aa: {
  8. title: '对象',
  9. type: 'object',
  10. properties: {
  11. input1: {
  12. title: '简单输入框',
  13. type: 'string',
  14. default: 'hello world',
  15. required: true,
  16. },
  17. check: {
  18. title: 'box',
  19. type: 'boolean',
  20. default: true,
  21. },
  22. select1: {
  23. title: '单选',
  24. type: 'string',
  25. enum: ['a', 'b', 'c'],
  26. enumNames: ['早', '中', '晚'],
  27. default: 'a',
  28. },
  29. },
  30. },
  31. },
  32. };
  33. export default () => <Form readOnly schema={schema} />;

labelWidth

标签的宽度,可以在顶层用 props 声明,或者在每个 schema 中单独声明

width

元素的宽度,在每个 schema 中单独声明。没有特别情况,建议一行一个元素(即默认的 100%),符合用户填写表单的习惯

  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. displayType: 'row',
  6. properties: {
  7. aa: {
  8. title: '对象',
  9. type: 'object',
  10. properties: {
  11. input1: {
  12. title: '简单输入框',
  13. type: 'string',
  14. default: 'hello world',
  15. required: true,
  16. width: '50%',
  17. },
  18. check: {
  19. title: 'box',
  20. type: 'boolean',
  21. default: true,
  22. width: '50%',
  23. labelWidth: 6,
  24. },
  25. select1: {
  26. title: '单选',
  27. type: 'string',
  28. enum: ['a', 'b', 'c'],
  29. enumNames: ['早', '中', '晚'],
  30. default: 'a',
  31. },
  32. },
  33. },
  34. },
  35. };
  36. export default () => <Form labelWidth="200" schema={schema} />;

列表的展示

列表的展示对于简单需求占位太多,复杂需求定制不够一直是痛点。所以我们给出了 5 种展示,充分满足从极简到复杂的所有需求。详见列表展示

  1. 默认展示使用 widget: ‘cardList’,卡片类型,用于展示数量不太多但结构复杂的 list
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. properties: {
  6. listName2: {
  7. title: '对象数组',
  8. description: '对象数组嵌套功能',
  9. type: 'array',
  10. // widget: 'cardList',
  11. items: {
  12. type: 'object',
  13. properties: {
  14. input1: {
  15. title: '简单输入框',
  16. type: 'string',
  17. required: true,
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. },
  25. obj: {
  26. title: '对象',
  27. type: 'object',
  28. properties: {
  29. input1: {
  30. title: '简单输入框',
  31. type: 'string',
  32. required: true,
  33. },
  34. select1: {
  35. title: '单选',
  36. type: 'string',
  37. enum: ['a', 'b', 'c'],
  38. enumNames: ['早', '中', '晚'],
  39. },
  40. },
  41. },
  42. },
  43. },
  44. },
  45. },
  46. };
  47. const Demo = () => {
  48. return <Form schema={schema} />;
  49. };
  50. export default Demo;
  1. widget: ‘simpleList’ 用于展示每行只有 1-3 个简单元素的情况
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. properties: {
  6. listName2: {
  7. title: '对象数组',
  8. description: '对象数组嵌套功能',
  9. type: 'array',
  10. widget: 'simpleList',
  11. items: {
  12. type: 'object',
  13. properties: {
  14. input1: {
  15. title: '简单输入框',
  16. type: 'string',
  17. required: true,
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. },
  25. },
  26. },
  27. },
  28. },
  29. };
  30. const Demo = () => {
  31. return <Form schema={schema} />;
  32. };
  33. export default Demo;
  1. widget: ‘tableList’ 用于展示每行只有 3 - n 个简单元素的情况,特别是数据量很大需要分页的
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. properties: {
  6. listName2: {
  7. title: '对象数组',
  8. description: '对象数组嵌套功能',
  9. type: 'array',
  10. widget: 'tableList',
  11. items: {
  12. type: 'object',
  13. properties: {
  14. input1: {
  15. title: '简单输入框',
  16. type: 'string',
  17. required: true,
  18. },
  19. input2: {
  20. title: '简单输入框2',
  21. type: 'string',
  22. },
  23. input3: {
  24. title: '简单输入框3',
  25. type: 'string',
  26. },
  27. select1: {
  28. title: '单选',
  29. type: 'string',
  30. enum: ['a', 'b', 'c'],
  31. enumNames: ['早', '中', '晚'],
  32. widget: 'select',
  33. },
  34. },
  35. },
  36. },
  37. },
  38. };
  39. const Demo = () => {
  40. return <Form schema={schema} />;
  41. };
  42. export default Demo;
  1. widget: ‘drawerList’ 用于展示存在列表套列表,列表套对象等复杂元素的情况
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. properties: {
  6. listName2: {
  7. title: '对象数组',
  8. description: '对象数组嵌套功能',
  9. type: 'array',
  10. widget: 'drawerList',
  11. items: {
  12. type: 'object',
  13. properties: {
  14. input1: {
  15. title: '简单输入框',
  16. type: 'string',
  17. required: true,
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. },
  25. listName2: {
  26. title: '对象数组',
  27. description: '对象数组嵌套功能',
  28. type: 'array',
  29. widget: 'simpleList',
  30. props: {
  31. hideMove: true,
  32. },
  33. items: {
  34. type: 'object',
  35. properties: {
  36. input1: {
  37. title: '简单输入框',
  38. type: 'string',
  39. required: true,
  40. },
  41. select1: {
  42. title: '单选',
  43. type: 'string',
  44. enum: ['a', 'b', 'c'],
  45. enumNames: ['早', '中', '晚'],
  46. },
  47. },
  48. },
  49. },
  50. },
  51. },
  52. },
  53. },
  54. };
  55. const Demo = () => {
  56. return <Form schema={schema} />;
  57. };
  58. export default Demo;

主题设置

对于嵌套类型的表单,我们内置了三种主题,分别为 collapse | card | tile, 默认为 collapse 主题

  1. 默认样式:theme: 'collapse' ,支持无边框模式: 'collapse:pure'幽灵模式:'collapse:ghost'
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. displayType: 'row',
  6. properties: {
  7. objectName: {
  8. title: '默认样式',
  9. bind: 'obj',
  10. description: '这是一个对象类型',
  11. type: 'object',
  12. collapsed: true,
  13. properties: {
  14. input1: {
  15. title: '简单输入框',
  16. type: 'string',
  17. required: true,
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. },
  25. },
  26. },
  27. objectName2: {
  28. title: '无边框样式',
  29. bind: 'obj',
  30. description: '这是一个对象类型',
  31. type: 'object',
  32. collapsed: true,
  33. theme: 'collapse:pure',
  34. properties: {
  35. input1: {
  36. title: '简单输入框',
  37. type: 'string',
  38. required: true,
  39. },
  40. select1: {
  41. title: '单选',
  42. type: 'string',
  43. enum: ['a', 'b', 'c'],
  44. enumNames: ['早', '中', '晚'],
  45. },
  46. },
  47. },
  48. objectName3: {
  49. title: '幽灵模式',
  50. bind: 'obj',
  51. description: '这是一个对象类型',
  52. type: 'object',
  53. collapsed: true,
  54. theme: 'collapse:ghost',
  55. properties: {
  56. input1: {
  57. title: '简单输入框',
  58. type: 'string',
  59. required: true,
  60. },
  61. select1: {
  62. title: '单选',
  63. type: 'string',
  64. enum: ['a', 'b', 'c'],
  65. enumNames: ['早', '中', '晚'],
  66. },
  67. },
  68. },
  69. },
  70. };
  71. const Demo = () => {
  72. return <Form schema={schema} />;
  73. };
  74. export default Demo;
  1. 卡片模式: theme: 'card'
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. displayType: 'row',
  6. properties: {
  7. objectName: {
  8. title: '卡片主题',
  9. description: '这是一个对象类型',
  10. type: 'object',
  11. theme: 'card',
  12. properties: {
  13. input1: {
  14. title: '简单输入框',
  15. type: 'string',
  16. required: true,
  17. width: '30%',
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. width: '30%',
  25. },
  26. date: {
  27. title: '时间选择',
  28. type: 'string',
  29. format: 'date',
  30. width: '30%',
  31. },
  32. },
  33. },
  34. objectName2: {
  35. title: '卡片主题',
  36. description: '这是一个对象类型',
  37. type: 'object',
  38. theme: 'card',
  39. properties: {
  40. input1: {
  41. title: '简单输入框',
  42. type: 'string',
  43. required: true,
  44. width: '30%',
  45. },
  46. select1: {
  47. title: '单选',
  48. type: 'string',
  49. enum: ['a', 'b', 'c'],
  50. enumNames: ['早', '中', '晚'],
  51. width: '30%',
  52. },
  53. date: {
  54. title: '时间选择',
  55. type: 'string',
  56. format: 'date',
  57. width: '30%',
  58. },
  59. },
  60. },
  61. },
  62. };
  63. const Demo = () => {
  64. return (
  65. <div>
  66. <Form schema={schema} />
  67. </div>
  68. );
  69. };
  70. export default Demo;
  1. 平铺模式:theme: 'tile'
  1. import React from 'react';
  2. import Form from '../demo/display';
  3. const schema = {
  4. type: 'object',
  5. displayType: 'row',
  6. properties: {
  7. objectName: {
  8. title: '平铺主题',
  9. description: '这是一个对象类型',
  10. type: 'object',
  11. theme: 'tile',
  12. properties: {
  13. input1: {
  14. title: '简单输入框',
  15. type: 'string',
  16. required: true,
  17. width: '30%',
  18. },
  19. select1: {
  20. title: '单选',
  21. type: 'string',
  22. enum: ['a', 'b', 'c'],
  23. enumNames: ['早', '中', '晚'],
  24. width: '30%',
  25. },
  26. date: {
  27. title: '时间选择',
  28. type: 'string',
  29. format: 'date',
  30. width: '30%',
  31. },
  32. },
  33. },
  34. objectName2: {
  35. title: '平铺主题',
  36. description: '这是一个对象类型',
  37. type: 'object',
  38. theme: 'tile',
  39. properties: {
  40. input1: {
  41. title: '简单输入框',
  42. type: 'string',
  43. required: true,
  44. width: '30%',
  45. },
  46. select1: {
  47. title: '单选',
  48. type: 'string',
  49. enum: ['a', 'b', 'c'],
  50. enumNames: ['早', '中', '晚'],
  51. width: '30%',
  52. },
  53. date: {
  54. title: '时间选择',
  55. type: 'string',
  56. format: 'date',
  57. width: '30%',
  58. },
  59. },
  60. },
  61. },
  62. };
  63. const Demo = () => {
  64. return (
  65. <div>
  66. <Form schema={schema} />
  67. </div>
  68. );
  69. };
  70. export default Demo;