@formily版本 @2.2.16�
image.png

  1. @formily/antd 桥接 antd,View层
    1. 基于 antd 封装的针对表单场景的 formily2x组件库,不兼容 formily1x
    2. “antd”: “<=4.22.8”
  2. @formily/core 核心库,ViewModel层
  3. @formily/react ,桥接 react,Model层
  4. @formily/grid
  5. @formily/json-schema
  6. @formily/path
  7. @formily/reactive
  8. @formily/reactive-react
  9. @formily/shared
  10. @formily/validator

Formily 分为了内核层,UI 桥接层,扩展组件层,和配置应用层
@formily/react 就是将 ViewModel 和 View 绑定起来的胶水层
@formily/core 就是 ViewModel,
Component 和 Decorator 就是 View
ViewModel 和 View 的绑定就叫做模型绑定,实现模型绑定的手段主要有useField,也能使用connectmapProps

https://antd-mobile.formilyjs.org/zh-CN

yarn add antd-mobile moment
yarn add @formily/core @formily/react @formily/antd-mobile

https://github.com/lulongwen/designable-antd

@formily/reactive
响应式状态管理方案;依赖追踪,高效更新,按需渲染
https://reactive.formilyjs.org/zh-CN/guide

参考 Mobx,基于 Proxy 劫持来实现的响应式编程模型
Observable,创建一个可订阅对象
Reaction,订阅者,接收一个 tracker 函数
Computed,缓存计算结果的 Reaction
Batch,将更新进行合并

@formily/core
将领域模型从 UI 框架中抽离出来,UI无关
https://core.formilyjs.org/zh-CN/guide

响应式计算能力
校验能力、校验国际化能力
值管理能力
联动管理能力

@formily/react
提供了一系列的组件和 Hooks
https://react.formilyjs.org/zh-CN/guide/concept

基于标准 JSON Schema 来进行驱动渲染的,协议驱动渲染
在标准之上又扩展了一些 x-*属性来表达 UI
Schema
递归渲染
协议绑定

SchemaField 子节点不能随意插 UI 元素,因为 SchemaField 只会解析子节点的所有 Schema 描述标签,
然后转换成 JSON Schema,最终交给RecursionField渲染,
如果想要插入 UI 元素,可以在 VoidField 上传x-content属性来插入 UI 元素

三种开发模式
JSX 开发模式,用 Field/ArrayField/ObjectField/VoidField 组件
JSON Schema 开发模式,SchemaField 的 schema 属性传递 JSON Schema 即可
Markup Schema 开发模式, Schema 开发模式,同样是使用 SchemaField 组件

JSON Schema 在 JSX 环境下很难得到最好的智能提示体验,也不方便维护
JSX 标签的形式可维护性会更好,智能提示也很强。


第三方组件库的无侵入接入 Formily

Formily DevTools
https://chrome.google.com/webstore/detail/formily-devtools/kkocalmbfnplecdmbadaapgapdioecfm/related?hl=zh-CN

@formily/core

formily核心库,用来操控整个表单、字段数据 https://core.formilyjs.org/zh-CN/guide
负责管理表单的状态,表单校验,联动等
将领域模型从 UI 框架中抽离出来,提升代码可维护性
74个方法,

  1. [
  2. "createEffectContext",
  3. "createEffectHook",
  4. "createForm",
  5. "FormPath",
  6. "getValidateLocaleIOSCode",
  7. "isArrayField",
  8. "isArrayFieldState",
  9. "isDataField",
  10. "isDataFieldState",
  11. "isField",
  12. "isFieldState",
  13. "isForm",
  14. "isFormState",
  15. "isGeneralField",
  16. "isGeneralFieldState",
  17. "isObjectField",
  18. "isObjectFieldState",
  19. "isQuery",
  20. "isVoidField",
  21. "isVoidFieldState",
  22. "LifeCycleTypes",
  23. "onFieldChange",
  24. "onFieldInit",
  25. "onFieldInitialValueChange",
  26. "onFieldInputValueChange",
  27. "onFieldLoading",
  28. "onFieldMount",
  29. "onFieldReact",
  30. "onFieldReset",
  31. "onFieldSubmit",
  32. "onFieldSubmitEnd",
  33. "onFieldSubmitFailed",
  34. "onFieldSubmitStart",
  35. "onFieldSubmitSuccess",
  36. "onFieldSubmitValidateEnd",
  37. "onFieldSubmitValidateFailed",
  38. "onFieldSubmitValidateStart",
  39. "onFieldSubmitValidateSuccess",
  40. "onFieldUnmount",
  41. "onFieldValidateEnd",
  42. "onFieldValidateFailed",
  43. "onFieldValidateStart",
  44. "onFieldValidateSuccess",
  45. "onFieldValidating",
  46. "onFieldValueChange",
  47. "onFormGraphChange",
  48. "onFormInit",
  49. "onFormInitialValuesChange",
  50. "onFormInputChange",
  51. "onFormLoading",
  52. "onFormMount",
  53. "onFormReact",
  54. "onFormReset",
  55. "onFormSubmit",
  56. "onFormSubmitEnd",
  57. "onFormSubmitFailed",
  58. "onFormSubmitStart",
  59. "onFormSubmitSuccess",
  60. "onFormSubmitValidateEnd",
  61. "onFormSubmitValidateFailed",
  62. "onFormSubmitValidateStart",
  63. "onFormSubmitValidateSuccess",
  64. "onFormUnmount",
  65. "onFormValidateEnd",
  66. "onFormValidateFailed",
  67. "onFormValidateStart",
  68. "onFormValidateSuccess",
  69. "onFormValuesChange",
  70. "registerValidateFormats",
  71. "registerValidateLocale",
  72. "registerValidateMessageTemplateEngine",
  73. "registerValidateRules",
  74. "setValidateLanguage",
  75. "useEffectForm"
  76. ]
  1. import { createForm } from '@formily/core';
  2. import { Schema } from '@formily/json-schema';
  3. // 注册字段
  4. Schema.registerPatches(schema => {
  5. const permission = schema['x-permission'];
  6. if (permission) { // 这里做一些处理
  7. schema['x-display'] = 'none';
  8. }
  9. return schema;
  10. });
  11. function App() {
  12. const form = createForm({
  13. validateFirst: true, // 只显示第一个错误
  14. initialValues: {}, // 初始值
  15. effects() {
  16. // 事件处理
  17. },
  18. })
  19. return (
  20. <Form form={form} layout="vertical">
  21. <Field
  22. name="test"
  23. title="测试"
  24. component={[Input, { placeholder: '请输入' }]}
  25. decorator={[FormItem]}
  26. />
  27. // 监听一个字段的变化,但是这个字段并没有出现在UI
  28. <Field name="xx" hidden />
  29. </Form>
  30. )
  31. }

@formily/antd

46个方法,只支持 Formily2.x https://antd.formilyjs.org/zh-CN/components
按照formily要求所封装的UI组件库,针对表单场景的 formily2x组件库,不兼容 formily1x

  • formily不能直接消费UI组件库,例如 antd
  • 需要按照 formily的格式所封装,属于 antd的 UI桥接库

connect链接了 antd组件库,区别

  • 你用 antd 组件库,需要自己去绑定事件和值,value & onChange受控,然后调用 @formily/core 的 API 收集表单值,做校验;
  • @formily/antd 做一层封装,connect了 antd组件,你就可以实现值改变自动更新的效果
    1. [
    2. "ArrayBase",
    3. "ArrayCards",
    4. "ArrayCollapse",
    5. "ArrayItems",
    6. "ArrayTable",
    7. "ArrayTabs",
    8. "BaseItem",
    9. "Cascader",
    10. "Checkbox",
    11. "createFormGrid",
    12. "DatePicker",
    13. "Editable",
    14. "Form",
    15. "FormButtonGroup",
    16. "FormCollapse",
    17. "FormDialog",
    18. "FormDrawer",
    19. "FormGrid",
    20. "FormItem",
    21. "FormLayout",
    22. "FormLayoutDeepContext",
    23. "FormLayoutShallowContext",
    24. "FormStep",
    25. "FormTab",
    26. "GridColumn",
    27. "Input",
    28. "NumberPicker",
    29. "Password",
    30. "PreviewText",
    31. "Radio",
    32. "Reset",
    33. "Select",
    34. "SelectTable",
    35. "Space",
    36. "Submit",
    37. "Switch",
    38. "TimePicker",
    39. "Transfer",
    40. "TreeSelect",
    41. "Upload",
    42. "useFormDeepLayout",
    43. "useFormGrid",
    44. "useFormLayout",
    45. "useFormShallowLayout",
    46. "useGridColumn",
    47. "useGridSpan"
    48. ]

@formily/react

UI桥接库,react的项目必须安装,提供了绑定 Form的相关 Field字段
31个方法

  1. [
  2. "ArrayField",
  3. "connect",
  4. "ContextCleaner",
  5. "createSchemaField",
  6. "ExpressionScope",
  7. "Field",
  8. "FieldContext",
  9. "FormConsumer",
  10. "FormContext",
  11. "FormProvider",
  12. "mapProps",
  13. "mapReadPretty",
  14. "ObjectField",
  15. "observer",
  16. "Observer",
  17. "RecordScope",
  18. "RecordsScope",
  19. "RecursionField",
  20. "Schema",
  21. "SchemaComponentsContext",
  22. "SchemaContext",
  23. "SchemaExpressionScopeContext",
  24. "SchemaMarkupContext",
  25. "SchemaOptionsContext",
  26. "useExpressionScope",
  27. "useField",
  28. "useFieldSchema",
  29. "useForm",
  30. "useFormEffects",
  31. "useParentForm",
  32. "VoidField"
  33. ]

@formily/reactive-react

8个方法

  1. [
  2. "observer",
  3. "Observer",
  4. "unstable_useCompatEffect",
  5. "unstable_useCompatFactory",
  6. "unstable_useDidUpdate",
  7. "unstable_useForceUpdate",
  8. "unstable_useLayoutEffect",
  9. "unstable_useObserver"
  10. ]

@formily/shared

47个方法

  1. [
  2. "applyMiddleware",
  3. "camelCase",
  4. "clone",
  5. "defaults",
  6. "deprecate",
  7. "each",
  8. "every",
  9. "find",
  10. "findIndex",
  11. "FormPath",
  12. "getType",
  13. "globalThisPolyfill",
  14. "includes",
  15. "instOf",
  16. "isArr",
  17. "isBool",
  18. "isEmpty",
  19. "isEqual",
  20. "isFn",
  21. "isHTMLElement",
  22. "isMap",
  23. "isNum",
  24. "isNumberLike",
  25. "isObj",
  26. "isPlainObj",
  27. "isReactElement",
  28. "isRegExp",
  29. "isSet",
  30. "isStr",
  31. "isUndef",
  32. "isValid",
  33. "isWeakMap",
  34. "isWeakSet",
  35. "lazyMerge",
  36. "lowerCase",
  37. "map",
  38. "merge",
  39. "paramCase",
  40. "pascalCase",
  41. "reduce",
  42. "shallowClone",
  43. "some",
  44. "stringLength",
  45. "Subscribable",
  46. "toArr",
  47. "uid",
  48. "upperCase"
  49. ]

@formily/reactive

24个方法
响应式编程,事件订阅与派发机制,formily自己实现了一套完整的拖拽引擎

  • 一个类 MobX 的 Reactive 方案作为全局状态管理,所有数据的变更都是可预测
  • 借助 @formily/reactive 去订阅内核的数据变更
  • Formily 内核还将各类表单问题抽象为若干个领域对象,如路径系统、生命周期、响应器等
    1. [
    2. "action",
    3. "autorun",
    4. "batch",
    5. "buildDataTree",
    6. "contains",
    7. "DataChange",
    8. "DataNode",
    9. "define",
    10. "getDataNode",
    11. "hasCollected",
    12. "isAnnotation",
    13. "isObservable",
    14. "isSupportObservable",
    15. "markObservable",
    16. "markRaw",
    17. "model",
    18. "observable",
    19. "observe",
    20. "raw",
    21. "reaction",
    22. "setDataNode",
    23. "toJS",
    24. "Tracker",
    25. "untracked"
    26. ]

@formily/validator

18个方法

  1. [
  2. "getLocaleByPath",
  3. "getValidateFormats",
  4. "getValidateLanguage",
  5. "getValidateLocale",
  6. "getValidateLocaleIOSCode",
  7. "getValidateMessageTemplateEngine",
  8. "getValidateRules",
  9. "isValidateResult",
  10. "parseValidator",
  11. "parseValidatorDescription",
  12. "parseValidatorDescriptions",
  13. "parseValidatorRules",
  14. "registerValidateFormats",
  15. "registerValidateLocale",
  16. "registerValidateMessageTemplateEngine",
  17. "registerValidateRules",
  18. "setValidateLanguage",
  19. "validate"
  20. ]

@formily/grid

  1. ["Grid"]

@formily/json-schema

通过虚拟 TreeNode 节点进行操作,最终再转换生成为 Formily JSON Schema

  1. ["Schema"]

@formily/path

  1. ["Path"]