react-antd-xform 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/_virtual/_commonjsHelpers.js +8 -0
  4. package/dist/_virtual/index.js +4 -0
  5. package/dist/_virtual/index2.js +4 -0
  6. package/dist/_virtual/jsx-runtime.js +4 -0
  7. package/dist/_virtual/react-jsx-runtime.development.js +4 -0
  8. package/dist/_virtual/react-jsx-runtime.production.min.js +4 -0
  9. package/dist/_virtual/use-sync-external-store-shim.development.js +4 -0
  10. package/dist/_virtual/use-sync-external-store-shim.production.js +4 -0
  11. package/dist/form/array-helper.d.ts +12 -0
  12. package/dist/form/array-helper.js +115 -0
  13. package/dist/form/common-utils.d.ts +12 -0
  14. package/dist/form/common-utils.js +90 -0
  15. package/dist/form/context/formEnvContext.d.ts +6 -0
  16. package/dist/form/context/formEnvContext.js +23 -0
  17. package/dist/form/context/modelContext.d.ts +4 -0
  18. package/dist/form/context/modelContext.js +11 -0
  19. package/dist/form/enum.d.ts +9 -0
  20. package/dist/form/enum.js +15 -0
  21. package/dist/form/form-item.d.ts +15 -0
  22. package/dist/form/form-item.js +286 -0
  23. package/dist/form/index.d.ts +22 -0
  24. package/dist/form/index.js +73 -0
  25. package/dist/form/layout.d.ts +4 -0
  26. package/dist/form/layout.js +180 -0
  27. package/dist/form/model-utils.d.ts +23 -0
  28. package/dist/form/model-utils.js +171 -0
  29. package/dist/form/model.d.ts +103 -0
  30. package/dist/form/model.js +504 -0
  31. package/dist/form/type.d.ts +289 -0
  32. package/dist/form-ui/common-utils.d.ts +9 -0
  33. package/dist/form-ui/common-utils.js +17 -0
  34. package/dist/form-ui/default-button.d.ts +3 -0
  35. package/dist/form-ui/default-button.js +39 -0
  36. package/dist/form-ui/default-component.d.ts +2 -0
  37. package/dist/form-ui/default-component.js +63 -0
  38. package/dist/form-ui/type.d.ts +2 -0
  39. package/dist/helpers/AsyncValue.d.ts +38 -0
  40. package/dist/helpers/AsyncValue.js +154 -0
  41. package/dist/index.d.ts +9 -0
  42. package/dist/index.js +20 -0
  43. package/dist/node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js +63 -0
  44. package/dist/node_modules/.pnpm/get-own-enumerable-keys@1.0.0/node_modules/get-own-enumerable-keys/index.js +10 -0
  45. package/dist/node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/browser.js +32 -0
  46. package/dist/node_modules/.pnpm/is-obj@3.0.0/node_modules/is-obj/index.js +7 -0
  47. package/dist/node_modules/.pnpm/is-regexp@3.1.0/node_modules/is-regexp/index.js +7 -0
  48. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Hash.js +23 -0
  49. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_ListCache.js +23 -0
  50. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Map.js +8 -0
  51. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_MapCache.js +23 -0
  52. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Symbol.js +7 -0
  53. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_apply.js +17 -0
  54. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_arrayMap.js +11 -0
  55. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_arrayPush.js +11 -0
  56. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assignValue.js +15 -0
  57. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assocIndexOf.js +15 -0
  58. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseAssignValue.js +18 -0
  59. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseFlatten.js +25 -0
  60. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGet.js +15 -0
  61. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.js +16 -0
  62. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseHasIn.js +7 -0
  63. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsArguments.js +11 -0
  64. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsNative.js +24 -0
  65. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_basePick.js +12 -0
  66. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_basePickBy.js +18 -0
  67. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseSet.js +33 -0
  68. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseSetToString.js +16 -0
  69. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseToString.js +23 -0
  70. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_castPath.js +15 -0
  71. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_coreJsData.js +7 -0
  72. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_defineProperty.js +14 -0
  73. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_flatRest.js +11 -0
  74. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_freeGlobal.js +6 -0
  75. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getMapData.js +10 -0
  76. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getNative.js +11 -0
  77. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getRawTag.js +27 -0
  78. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getValue.js +7 -0
  79. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hasPath.js +27 -0
  80. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashClear.js +10 -0
  81. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashDelete.js +9 -0
  82. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashGet.js +17 -0
  83. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashHas.js +12 -0
  84. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashSet.js +13 -0
  85. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isFlattenable.js +12 -0
  86. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isIndex.js +11 -0
  87. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isKey.js +18 -0
  88. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isKeyable.js +8 -0
  89. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isMasked.js +13 -0
  90. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheClear.js +8 -0
  91. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheDelete.js +22 -0
  92. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheGet.js +10 -0
  93. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheHas.js +9 -0
  94. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheSet.js +16 -0
  95. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheClear.js +16 -0
  96. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheDelete.js +11 -0
  97. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheGet.js +9 -0
  98. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheHas.js +9 -0
  99. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheSet.js +12 -0
  100. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_memoizeCapped.js +17 -0
  101. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_nativeCreate.js +7 -0
  102. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_objectToString.js +9 -0
  103. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_overRest.js +23 -0
  104. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.js +8 -0
  105. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_setToString.js +8 -0
  106. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_shortOut.js +21 -0
  107. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stringToPath.js +18 -0
  108. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_toKey.js +13 -0
  109. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_toSource.js +19 -0
  110. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/constant.js +9 -0
  111. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/eq.js +7 -0
  112. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/flatten.js +10 -0
  113. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/hasIn.js +10 -0
  114. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/identity.js +7 -0
  115. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArguments.js +15 -0
  116. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArray.js +5 -0
  117. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isFunction.js +15 -0
  118. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isLength.js +8 -0
  119. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObject.js +8 -0
  120. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObjectLike.js +7 -0
  121. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isSymbol.js +11 -0
  122. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/memoize.js +24 -0
  123. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/pick.js +12 -0
  124. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/toString.js +9 -0
  125. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/ObserverComponent.js +5 -0
  126. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/index.js +18 -0
  127. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/observer.js +84 -0
  128. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/useObserver.js +72 -0
  129. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/UniversalFinalizationRegistry.js +96 -0
  130. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/assertEnvironment.js +8 -0
  131. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/observerBatching.js +17 -0
  132. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/observerFinalizationRegistry.js +9 -0
  133. package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/printDebugValue.js +7 -0
  134. package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/cjs/react-jsx-runtime.development.js +890 -0
  135. package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/cjs/react-jsx-runtime.production.min.js +33 -0
  136. package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js +12 -0
  137. package/dist/node_modules/.pnpm/stringify-object@5.0.0/node_modules/stringify-object/index.js +94 -0
  138. package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +77 -0
  139. package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js +62 -0
  140. package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/shim/index.js +12 -0
  141. package/dist/types/index.d.ts +2 -0
  142. package/package.json +58 -0
@@ -0,0 +1,289 @@
1
+ import { ReactNode } from 'react';
2
+ import { Field, FormModel } from './model';
3
+ import { FieldType, ModelType } from './enum';
4
+ type valueOf<T> = T[keyof T];
5
+ export type XName<D> = 0 extends D & 1 ? any : D extends (infer U)[] ? number | `${number}` | `${number}.${XName<U>}` : D extends object ? valueOf<{
6
+ [K in keyof D & string]: K | `${K}.${XName<D[K]>}`;
7
+ }> : never;
8
+ type IfAny<T, TRUE, FALSE> = 0 extends T & 1 ? TRUE : FALSE;
9
+ export type ResolveXName<D, Path extends string | number> = 0 extends Path & 1 ? any : string extends Path ? IfAny<D, any, unknown> : Path extends number ? D extends Array<infer U> ? U : unknown : Path extends keyof D ? D[Path] : Path extends `${infer K}.${infer R}` ? K extends keyof D ? ResolveXName<D[K], R> : unknown : unknown;
10
+ export type ValueShape = 'array' | 'object';
11
+ export type FieldValidateTrigger = '*' | 'blur' | 'change' | 'mount';
12
+ export interface FieldConfig<D> {
13
+ htmlId?: string;
14
+ label?: React.ReactNode;
15
+ help?: React.ReactNode;
16
+ tip?: React.ReactNode;
17
+ asterisk?: boolean;
18
+ afterChange?(...args: any[]): void;
19
+ /** 覆盖 FormItem 上的错误信息 */
20
+ error?: React.ReactNode;
21
+ /** 在 `<FormItem />` 上设置的 props.value(也可能是 props.checked,取决于组件类型) */
22
+ valueProp?: any;
23
+ defaultValue?: any;
24
+ /** 在 `<FormItem />` 上设置的 props.defaultValue */
25
+ defaultValueProp?: any;
26
+ isEmpty?(value: any): boolean;
27
+ required?: boolean;
28
+ requiredMessage?: string;
29
+ writeDefaultValueToModel?: boolean | 'force';
30
+ autoUnmount?: boolean;
31
+ validate?(value: any, field: Field<D>, trigger: FieldValidateTrigger): undefined | null | string | Promise<any>;
32
+ validateOnMount?: boolean;
33
+ validateOnChange?: boolean;
34
+ validateOnBlur?: boolean;
35
+ disabled?: boolean;
36
+ readOnly?: boolean;
37
+ status?: string;
38
+ }
39
+ export interface CheckConfig<D> {
40
+ validate(values: D, model: FormModel<D>): undefined | null | string | Promise<any>;
41
+ validateOnMount?: boolean;
42
+ }
43
+ export interface FieldState {
44
+ error?: any;
45
+ validating?: boolean;
46
+ cancelValidation?(): void;
47
+ [key: string]: any;
48
+ }
49
+ type FieldCreateCommon = {
50
+ parent: FormModel;
51
+ name: string;
52
+ forkName?: string;
53
+ };
54
+ export type FieldCreateOptions = ({
55
+ fieldType: FieldType.normal;
56
+ } & FieldCreateCommon) | ({
57
+ fieldType: FieldType.tuple;
58
+ tupleParts: string[];
59
+ } & FieldCreateCommon) | ({
60
+ fieldType: FieldType.readonly;
61
+ value: any;
62
+ } & FieldCreateCommon);
63
+ export type FormModelCreateOptions = {
64
+ modelType: ModelType.rootModel;
65
+ } | {
66
+ modelType: ModelType.subModel;
67
+ parent: FormModel;
68
+ name: string;
69
+ };
70
+ export interface FormEnvContextType {
71
+ /** 提交表单时的回调函数,需配合 <Form.Submit /> 使用 */
72
+ onSubmit?(submitValues: any, model: FormModel<any>): void;
73
+ /** 提交表单时的出错回调函数,需配合 <Form.Submit /> 使用 */
74
+ onError?(errors: any, model: FormModel<any>): void;
75
+ /** 清空表单时的回调函数,需配合 <Form.Reset /> 使用 */
76
+ onReset?(model: FormModel<any>): void;
77
+ /**
78
+ * 是否为预览态
79
+ * @default false
80
+ * */
81
+ isPreview?: boolean;
82
+ /**
83
+ * 组件加载时是否触发校验
84
+ * @default false
85
+ * */
86
+ validateOnMount?: boolean;
87
+ /**
88
+ * 值修改时是否触发校验
89
+ * @default true
90
+ * */
91
+ validateOnChange?: boolean;
92
+ /**
93
+ * 组件失去焦点时是否触发校验
94
+ * @default true
95
+ * */
96
+ validateOnBlur?: boolean;
97
+ /**
98
+ * 是否将 FormItem 上的 defaultValue 回写到 model 中.
99
+ * 注意只有「不为 undefined」且「通过 FormItem props 设置的」的默认值才会被回写到 model 中.
100
+ * @default false
101
+ * */
102
+ writeDefaultValueToModel?: boolean | 'force';
103
+ /**
104
+ * FormItem 卸载时,是否自动将对应数据清除
105
+ * @default false
106
+ */
107
+ autoUnmount?: boolean;
108
+ /**
109
+ * 表单内控件的 html id 前缀。
110
+ *
111
+ * 表单会以该属性为前缀为各个控件分配 id 属性,使控件与 html label 标签产生对应关系,点击 label 可以聚焦到相应控件上。
112
+ *
113
+ * 默认情况下,表单内部会自动生成一个随机字符串作为前缀。
114
+ * 将该属性设置为 null 可以关闭 label 关联行为;设置为指定字符串则可使用指定前缀。
115
+ *
116
+ * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/label
117
+ * */
118
+ htmlIdPrefix?: string;
119
+ }
120
+ export interface FormLayoutParams {
121
+ /** 标签位置,可选 'left' 或 'top' */
122
+ labelPosition?: 'left' | 'top';
123
+ /** 标签宽度 */
124
+ labelWidth?: string | number;
125
+ /** 控件宽度 */
126
+ controlWidth?: string | number;
127
+ /** 两个 form item 之间的间距 */
128
+ formItemGap?: string | number;
129
+ /** labelPosition=top 时,是否内联展示 error 消息 */
130
+ inlineError?: boolean;
131
+ }
132
+ export interface FormProps extends FormEnvContextType {
133
+ /** 受控用法。 xform 模型对象,一般由上层通过 new FormModel(...) 创建而成 */
134
+ model?: FormModel;
135
+ /** 非受控用法。 表单的默认值 */
136
+ defaultValue?: unknown;
137
+ /** @category 布局 */
138
+ style?: React.CSSProperties;
139
+ /** @category 布局 */
140
+ className?: string;
141
+ /**
142
+ * 透传给最外层 div 的 props
143
+ * @category 布局
144
+ * */
145
+ containerProps?: React.HTMLProps<HTMLDivElement>;
146
+ /**
147
+ * 表单布局参数
148
+ * @category 布局
149
+ * */
150
+ layout?: FormLayoutParams;
151
+ children?: React.ReactNode;
152
+ }
153
+ export interface FormLayoutParams {
154
+ /** 标签位置,可选 'left' 或 'top' */
155
+ labelPosition?: 'left' | 'top';
156
+ /** 标签宽度 */
157
+ labelWidth?: string | number;
158
+ /** 控件宽度 */
159
+ controlWidth?: string | number;
160
+ /** 两个 form item 之间的间距 */
161
+ formItemGap?: string | number;
162
+ /** labelPosition=top 时,是否内联展示 error 消息 */
163
+ inlineError?: boolean;
164
+ }
165
+ export interface FormLayoutProps extends Partial<FormLayoutParams> {
166
+ style?: React.CSSProperties;
167
+ className?: string;
168
+ children?: ReactNode;
169
+ containerProps?: any;
170
+ defaultLabelTopPosition?: number | string;
171
+ }
172
+ export interface FormItemGroupProps {
173
+ label?: React.ReactNode;
174
+ tip?: React.ReactNode;
175
+ asterisk?: boolean;
176
+ children?: React.ReactNode;
177
+ labelWidth?: number | string;
178
+ labelTopPosition?: number | string;
179
+ controlWidth?: number | string;
180
+ className?: string;
181
+ style?: React.CSSProperties;
182
+ inline?: boolean;
183
+ }
184
+ export interface FormItemComponentProps {
185
+ value?: any;
186
+ onChange?(...args: any[]): void;
187
+ onFocus?(...args: any[]): void;
188
+ onBlur?(...args: any[]): void;
189
+ readOnly?: any;
190
+ disabled?: any;
191
+ isPreview?: any;
192
+ [prop: string]: any;
193
+ }
194
+ export interface FormItemCreationOptions {
195
+ /** 名称 */
196
+ name: string;
197
+ /** 是否为隐藏元素 */
198
+ hidden?: boolean;
199
+ /** 是否需要将 field 对象传递给组件 */
200
+ withField?: boolean;
201
+ /** 控件对应的 React 组件,例如 `<FormItem component="select" />` 对应 `Select` 组件. */
202
+ component?: React.ComponentType<FormItemComponentProps>;
203
+ /** 控件渲染方法,与 component 参数二选一,优先级高于 component */
204
+ render?(arg: FormItemComponentProps): React.ReactElement;
205
+ /** 组件值的属性名称,默认为 `'value'` */
206
+ valuePropName?: string;
207
+ /** 组件状态值的属性名称,默认为 `'state'` */
208
+ statusPropName?: string;
209
+ /** 预览态下组件的渲染方法。如果不设置该方法,预览态下将使用 render/component 作为后备方案. */
210
+ renderPreview?(props: FormItemComponentProps): React.ReactNode;
211
+ /** 默认值 */
212
+ defaultValue?: any;
213
+ /** 组件类型默认的空值判断方法 */
214
+ isEmpty?(value: any): boolean;
215
+ /** 组件是否具有固有宽度,默认为 true。该选项为 true 时,controlWidth 将不对组件产生效果 */
216
+ hasIntrinsicWidth?: boolean;
217
+ }
218
+ export interface FormItemProps extends Omit<FieldConfig<any>, 'defaultValueProp' | 'valueProp' | 'htmlId'> {
219
+ use?: boolean;
220
+ component: string | React.ComponentType<FormItemComponentProps>;
221
+ componentProps?: any;
222
+ dataSource?: any;
223
+ style?: React.CSSProperties;
224
+ className?: string;
225
+ name?: string;
226
+ field?: Field;
227
+ value?: any;
228
+ onChange?(nextValue: any): void;
229
+ onFocus?(): void;
230
+ onBlur?(): void;
231
+ renderPreview?(props: FormItemProps): React.ReactNode;
232
+ labelWidth?: number | string;
233
+ labelTopPosition?: number | string;
234
+ controlWidth?: number | string;
235
+ labelStyle?: React.CSSProperties;
236
+ controlStyle?: React.CSSProperties;
237
+ rightNode?: React.ReactNode;
238
+ isPreview?: boolean;
239
+ }
240
+ export interface FormItemViewProps {
241
+ /** `<label />` 的 id 属性 */
242
+ htmlId?: string;
243
+ /** 标签 */
244
+ label?: React.ReactNode;
245
+ /** 帮助文本 */
246
+ help?: React.ReactNode;
247
+ /** 提示信息 */
248
+ tip?: React.ReactNode;
249
+ /** 是否展示「*」 */
250
+ asterisk?: boolean;
251
+ /** 错误信息 */
252
+ error?: React.ReactNode;
253
+ children?: React.ReactNode;
254
+ style?: React.CSSProperties;
255
+ className?: string;
256
+ /** 标签宽度 */
257
+ labelWidth?: string | number;
258
+ /** 标签内联样式 */
259
+ labelStyle?: React.CSSProperties;
260
+ /** 控件宽度 */
261
+ controlWidth?: string | number;
262
+ /** 控件内联样式 */
263
+ controlStyle?: React.CSSProperties;
264
+ /** 在控件右侧添加自定义内容 */
265
+ rightNode?: React.ReactNode;
266
+ }
267
+ export type SubmitExtraOptions = {
268
+ valueFilter?: 'mounted' | 'all';
269
+ mergeDefaultValue?: boolean;
270
+ scrollToFirstError?: boolean;
271
+ animateErrorFields?: boolean;
272
+ };
273
+ export type SubmitOptions = Pick<FormEnvContextType, 'onSubmit' | 'onError'> & SubmitExtraOptions;
274
+ export interface FormArrayLayoutInput {
275
+ /** 数组的表单模型 */
276
+ arrayModel: FormModel<unknown[]>;
277
+ /** 数组长度,即 arrayModel.values.length */
278
+ itemCount: number;
279
+ /** 获取每个数组元素对应的子表单 */
280
+ itemContent(itemIndex: number, itemModel: FormModel<any>): React.ReactNode;
281
+ }
282
+ export interface FormArrayProps<T> {
283
+ use?: boolean;
284
+ arrayModel?: FormModel<T[]>;
285
+ name?: string;
286
+ layout?(input: FormArrayLayoutInput): React.ReactElement;
287
+ children?: React.ReactNode | ((index: number, model: FormModel<T>) => React.ReactNode);
288
+ }
289
+ export {};
@@ -0,0 +1,9 @@
1
+ export declare function isEmptyValue(value: any): boolean;
2
+ /**
3
+ * antd中某些组件的onChange并不是直接返回value,而是e.target.value
4
+ */
5
+ export declare const withValueChangeHandler: <T extends {
6
+ onChange?: (...args: any[]) => void;
7
+ }>(Component: React.ComponentType<T>) => (props: Omit<T, "onChange"> & {
8
+ onChange?: (value: any) => void;
9
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,17 @@
1
+ import { j as jsxRuntimeExports } from "../node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js";
2
+ function isEmptyValue(value) {
3
+ return value == null || value === "";
4
+ }
5
+ const withValueChangeHandler = (Component) => {
6
+ return (props) => {
7
+ const handleChange = (e) => {
8
+ var _a;
9
+ (_a = props.onChange) == null ? void 0 : _a.call(props, e.target.value);
10
+ };
11
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...props, onChange: handleChange });
12
+ };
13
+ };
14
+ export {
15
+ isEmptyValue,
16
+ withValueChangeHandler
17
+ };
@@ -0,0 +1,3 @@
1
+ import { SubmitExtraOptions } from '../form/type';
2
+ import { ButtonProps } from './type';
3
+ export declare function FormSubmit({ type, children, valueFilter, mergeDefaultValue, animateErrorFields, scrollToFirstError, ...props }: ButtonProps & SubmitExtraOptions): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,39 @@
1
+ import { j as jsxRuntimeExports } from "../node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js";
2
+ import cx from "../node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js";
3
+ import { Button } from "antd";
4
+ import { modelUtils } from "../form/model-utils.js";
5
+ import { useModel } from "../form/context/modelContext.js";
6
+ import { useFormEnv } from "../form/context/formEnvContext.js";
7
+ function FormSubmit({
8
+ type = "primary",
9
+ children = "提交",
10
+ valueFilter,
11
+ mergeDefaultValue,
12
+ animateErrorFields,
13
+ scrollToFirstError,
14
+ ...props
15
+ }) {
16
+ const model = useModel();
17
+ const { onSubmit, onError } = useFormEnv();
18
+ const submitOptions = {
19
+ onSubmit,
20
+ onError,
21
+ valueFilter,
22
+ mergeDefaultValue,
23
+ animateErrorFields,
24
+ scrollToFirstError
25
+ };
26
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(
27
+ Button,
28
+ {
29
+ onClick: () => modelUtils.submit(model, submitOptions),
30
+ type,
31
+ children,
32
+ ...props,
33
+ className: cx("form-submit-button", props.className)
34
+ }
35
+ );
36
+ }
37
+ export {
38
+ FormSubmit
39
+ };
@@ -0,0 +1,2 @@
1
+ import { FormItemCreationOptions } from '../form/type';
2
+ export declare const ALL_COMPONENTS: FormItemCreationOptions[];
@@ -0,0 +1,63 @@
1
+ import { Select, Checkbox, InputNumber, Slider, Switch, Input, Radio } from "antd";
2
+ import { isEmptyValue, withValueChangeHandler } from "./common-utils.js";
3
+ const ALL_COMPONENTS = [
4
+ {
5
+ name: "input",
6
+ component: withValueChangeHandler(Input),
7
+ defaultValue: "",
8
+ isEmpty: isEmptyValue,
9
+ hasIntrinsicWidth: false
10
+ },
11
+ {
12
+ name: "textArea",
13
+ component: withValueChangeHandler(Input.TextArea),
14
+ defaultValue: "",
15
+ isEmpty: isEmptyValue,
16
+ hasIntrinsicWidth: false
17
+ },
18
+ {
19
+ name: "select",
20
+ component: Select,
21
+ defaultValue: "",
22
+ isEmpty: isEmptyValue,
23
+ hasIntrinsicWidth: false
24
+ },
25
+ {
26
+ name: "radio",
27
+ component: withValueChangeHandler(Radio.Group),
28
+ defaultValue: "",
29
+ isEmpty: isEmptyValue,
30
+ hasIntrinsicWidth: false
31
+ },
32
+ {
33
+ name: "checkbox",
34
+ component: Checkbox.Group,
35
+ defaultValue: "",
36
+ isEmpty: isEmptyValue,
37
+ hasIntrinsicWidth: false
38
+ },
39
+ {
40
+ name: "inputNumber",
41
+ component: InputNumber,
42
+ defaultValue: "",
43
+ isEmpty: isEmptyValue,
44
+ hasIntrinsicWidth: false
45
+ },
46
+ {
47
+ name: "slider",
48
+ component: Slider,
49
+ defaultValue: "",
50
+ isEmpty: isEmptyValue,
51
+ hasIntrinsicWidth: false
52
+ },
53
+ {
54
+ name: "switch",
55
+ component: Switch,
56
+ defaultValue: false,
57
+ isEmpty: isEmptyValue,
58
+ hasIntrinsicWidth: false
59
+ }
60
+ ];
61
+ export {
62
+ ALL_COMPONENTS
63
+ };
@@ -0,0 +1,2 @@
1
+ import { Button } from 'antd';
2
+ export type ButtonProps = React.PropsWithChildren<React.ComponentProps<typeof Button>>;
@@ -0,0 +1,38 @@
1
+ export declare class AsyncValue<T> {
2
+ private _disposers;
3
+ private _error;
4
+ private _reaction;
5
+ private _status;
6
+ private _current;
7
+ private _cancelLastGetter;
8
+ private _atom;
9
+ private _refreshAtom;
10
+ private fetcher;
11
+ private options;
12
+ name: string;
13
+ static _globalCount: number;
14
+ static SKIP: string;
15
+ static StillLoading: {
16
+ new (message?: string): {
17
+ name: string;
18
+ message: string;
19
+ stack?: string;
20
+ };
21
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
22
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
23
+ stackTraceLimit: number;
24
+ };
25
+ constructor(fetcher: () => Promise<T>, initValue: T | null, options?: Record<string, unknown>);
26
+ get _mobxGlobal(): any;
27
+ _start: () => void;
28
+ _stop: () => void;
29
+ refresh(): void;
30
+ dispose(): void;
31
+ get status(): string;
32
+ get _inner_current(): T | null;
33
+ get current(): T | null;
34
+ get error(): any;
35
+ get isLoading(): boolean;
36
+ get isReady(): boolean;
37
+ get isError(): boolean;
38
+ }
@@ -0,0 +1,154 @@
1
+ import { Reaction, runInAction, createAtom, makeObservable, computed, onBecomeObserved, onBecomeUnobserved, _getGlobalState } from "mobx";
2
+ const _AsyncValue = class _AsyncValue {
3
+ constructor(fetcher, initValue, options = {}) {
4
+ this._start = () => {
5
+ if (this._reaction) {
6
+ return;
7
+ }
8
+ this._reaction = new Reaction(`${this.name}#reaction`, () => {
9
+ if (this._status !== "loading") {
10
+ this._status = "loading";
11
+ this._atom.reportChanged();
12
+ }
13
+ if (this._reaction) {
14
+ this._reaction.track(() => {
15
+ let cancelled = false;
16
+ this._cancelLastGetter = () => {
17
+ cancelled = true;
18
+ };
19
+ const prevInXFormAsyncValueFetcher = this._mobxGlobal.inXFormAsyncValueFetcher ?? false;
20
+ this._mobxGlobal.inXFormAsyncValueFetcher = true;
21
+ this._refreshAtom.reportObserved();
22
+ let promise;
23
+ try {
24
+ promise = this.fetcher();
25
+ } catch (err) {
26
+ promise = Promise.reject(err);
27
+ }
28
+ Promise.resolve(promise).then((newValue) => {
29
+ if (cancelled) {
30
+ return;
31
+ }
32
+ runInAction(() => {
33
+ this._status = "ready";
34
+ if (newValue !== _AsyncValue.SKIP) {
35
+ this._current = newValue;
36
+ }
37
+ this._error = null;
38
+ this._atom.reportChanged();
39
+ });
40
+ }).catch((err) => {
41
+ if (!(err instanceof _AsyncValue.StillLoading)) {
42
+ runInAction(() => {
43
+ this._status = "error";
44
+ this._error = err;
45
+ this._atom.reportChanged();
46
+ });
47
+ throw err;
48
+ }
49
+ });
50
+ this._mobxGlobal.inXFormAsyncValueFetcher = prevInXFormAsyncValueFetcher;
51
+ });
52
+ }
53
+ });
54
+ this._reaction.schedule_();
55
+ };
56
+ this._stop = () => {
57
+ this._status = "loading";
58
+ if (this._reaction) {
59
+ this._reaction.dispose();
60
+ this._reaction = null;
61
+ }
62
+ };
63
+ this._disposers = [];
64
+ this._error = null;
65
+ this._reaction = null;
66
+ this._status = "loading";
67
+ this._current = initValue;
68
+ this._cancelLastGetter = null;
69
+ this.fetcher = fetcher;
70
+ this.options = options;
71
+ this.name = typeof options.name === "string" ? options.name : `AsyncValue_${_AsyncValue._globalCount++}`;
72
+ this._atom = createAtom(`${this.name}#state`);
73
+ this._refreshAtom = createAtom(`${this.name}#refresh`);
74
+ makeObservable(
75
+ this,
76
+ {
77
+ status: computed,
78
+ error: computed,
79
+ _inner_current: computed({ name: `${this.name}.inner_current` }),
80
+ isLoading: computed,
81
+ isError: computed,
82
+ isReady: computed
83
+ },
84
+ { name: this.name }
85
+ );
86
+ this._disposers.push(this._stop);
87
+ this._disposers.push(onBecomeObserved(this._atom, this._start));
88
+ this._disposers.push(
89
+ onBecomeUnobserved(this._atom, () => {
90
+ if (!this.options.keepAlive) {
91
+ this._stop();
92
+ }
93
+ })
94
+ );
95
+ this._disposers.push(() => {
96
+ this._status = "ready";
97
+ this._current = null;
98
+ this._error = null;
99
+ });
100
+ }
101
+ get _mobxGlobal() {
102
+ return _getGlobalState();
103
+ }
104
+ refresh() {
105
+ this._refreshAtom.reportChanged();
106
+ }
107
+ dispose() {
108
+ for (const fn of this._disposers) {
109
+ fn();
110
+ }
111
+ }
112
+ get status() {
113
+ this._atom.reportObserved();
114
+ return this._status;
115
+ }
116
+ get _inner_current() {
117
+ this._atom.reportObserved();
118
+ return this._current;
119
+ }
120
+ get current() {
121
+ if (this._mobxGlobal.inXFormAsyncValueFetcher) {
122
+ this._atom.reportObserved();
123
+ if (this._status === "loading") {
124
+ throw new _AsyncValue.StillLoading();
125
+ } else if (this._status === "error") {
126
+ throw this._error;
127
+ } else {
128
+ return this._current;
129
+ }
130
+ }
131
+ return this._inner_current;
132
+ }
133
+ get error() {
134
+ this._atom.reportObserved();
135
+ return this._error;
136
+ }
137
+ get isLoading() {
138
+ return this.status === "loading";
139
+ }
140
+ get isReady() {
141
+ return this.status === "ready";
142
+ }
143
+ get isError() {
144
+ return this.status === "error";
145
+ }
146
+ };
147
+ _AsyncValue._globalCount = 0;
148
+ _AsyncValue.SKIP = "AsyncValue@SKIP";
149
+ _AsyncValue.StillLoading = class StillLoading extends Error {
150
+ };
151
+ let AsyncValue = _AsyncValue;
152
+ export {
153
+ AsyncValue
154
+ };
@@ -0,0 +1,9 @@
1
+ import { Form } from './form';
2
+ import { useModel } from './form/context/modelContext';
3
+ import { FormEnvProvider, useFormEnv } from './form/context/formEnvContext';
4
+ import { FormItem, createFormItem } from './form/form-item';
5
+ import { FormModel } from './form/model';
6
+ import { arrayHelpers } from './form/array-helper';
7
+ import { AsyncValue } from './helpers/AsyncValue';
8
+ import { modelUtils } from './form/model-utils';
9
+ export { Form, FormEnvProvider, useFormEnv, useModel, FormItem, createFormItem, FormModel, arrayHelpers, AsyncValue, modelUtils, };
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import { Form } from "./form/index.js";
2
+ import { useModel } from "./form/context/modelContext.js";
3
+ import { FormEnvProvider, useFormEnv } from "./form/context/formEnvContext.js";
4
+ import { FormItem, createFormItem } from "./form/form-item.js";
5
+ import { FormModel } from "./form/model.js";
6
+ import { arrayHelpers } from "./form/array-helper.js";
7
+ import { AsyncValue } from "./helpers/AsyncValue.js";
8
+ import { modelUtils } from "./form/model-utils.js";
9
+ export {
10
+ AsyncValue,
11
+ Form,
12
+ FormEnvProvider,
13
+ FormItem,
14
+ FormModel,
15
+ arrayHelpers,
16
+ createFormItem,
17
+ modelUtils,
18
+ useFormEnv,
19
+ useModel
20
+ };