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

toc: content

覆盖默认组件(mapping)

有些情况你对 FR 的默认组件的展示不满意,或者甚至想换一套全新的 UI 组件(非 antd),这时候使用自定义组件一次次地去声明 widget: ‘myCustomWidget’ 就显得极为繁琐。FR 提供了 mapping 这个 props,用于对 FR 内置的默认组件进行覆盖。先上个例子

  1. import React from 'react';
  2. import { Button } from 'antd';
  3. import FormRender, { useForm } from 'form-render';
  4. const CustomComA = props => {
  5. const { schema } = props;
  6. if (schema.$id === '#') {
  7. return <div>{props.children}</div>;
  8. }
  9. return (
  10. <div>
  11. my custom object:
  12. {props.children}
  13. </div>
  14. );
  15. };
  16. const schema = {
  17. type: 'object',
  18. properties: {
  19. object2: {
  20. title: '对象',
  21. type: 'object',
  22. properties: {
  23. input1: {
  24. title: '输入框',
  25. type: 'string',
  26. },
  27. },
  28. },
  29. },
  30. labelWidth: 120,
  31. displayType: 'row',
  32. };
  33. const Demo = () => {
  34. const form = useForm();
  35. const onFinish = (formData, errors) => {
  36. console.log('formData:', formData, 'errors', errors);
  37. };
  38. return (
  39. <div>
  40. <FormRender
  41. form={form}
  42. schema={schema}
  43. onFinish={onFinish}
  44. mapping={{ object: 'CustomComA' }}
  45. widgets={{ CustomComA }}
  46. />
  47. <Button type="primary" onClick={form.submit}>
  48. 提交
  49. </Button>
  50. </div>
  51. );
  52. };
  53. export default Demo;