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,286 @@
1
+ import { j as jsxRuntimeExports } from "../node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js";
2
+ import require$$0, { useLayoutEffect, useEffect } from "react";
3
+ import cx from "../node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js";
4
+ import pick from "../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/pick.js";
5
+ import "../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";
6
+ import stringifyObject from "../node_modules/.pnpm/stringify-object@5.0.0/node_modules/stringify-object/index.js";
7
+ import { toJS, reaction, runInAction } from "mobx";
8
+ import { FieldType } from "./enum.js";
9
+ import { Field } from "./model.js";
10
+ import { useModel } from "./context/modelContext.js";
11
+ import { useFormEnv } from "./context/formEnvContext.js";
12
+ import { ALL_COMPONENTS } from "../form-ui/default-component.js";
13
+ import { isFalsyOrEmptyArray, composeValue, asCSSLength } from "./common-utils.js";
14
+ import { observer } from "../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";
15
+ function processCreationOptions(options) {
16
+ const render = options.render ?? ((props) => require$$0.createElement(options.component, props));
17
+ return {
18
+ name: options.name,
19
+ hidden: options.hidden,
20
+ withField: Boolean(options.withField),
21
+ statusPropName: composeValue(options.statusPropName, "state"),
22
+ valuePropName: composeValue(options.valuePropName, "value"),
23
+ hasIntrinsicWidth: options.hasIntrinsicWidth !== false,
24
+ defaultValue: Object.keys(options).includes("defaultValue") ? options.defaultValue : null,
25
+ isEmpty: options.isEmpty ?? isFalsyOrEmptyArray,
26
+ render,
27
+ renderPreview: options.renderPreview ?? render
28
+ };
29
+ }
30
+ function resolveField(fieldProp, model, name, valueProp) {
31
+ let field;
32
+ if (fieldProp != null) {
33
+ field = fieldProp;
34
+ } else if (name === "&") {
35
+ field = model._asField();
36
+ } else if (name != null) {
37
+ field = model.getField(name);
38
+ } else {
39
+ field = new Field({
40
+ fieldType: FieldType.readonly,
41
+ value: valueProp,
42
+ name: "(readonly)",
43
+ forkName: Field.ORIGINAL,
44
+ parent: model
45
+ });
46
+ }
47
+ return field;
48
+ }
49
+ function FormItemView({
50
+ htmlId,
51
+ label = "",
52
+ help,
53
+ tip,
54
+ asterisk,
55
+ error,
56
+ children,
57
+ className,
58
+ style,
59
+ labelWidth,
60
+ controlWidth,
61
+ rightNode,
62
+ labelStyle,
63
+ controlStyle
64
+ }) {
65
+ return /* @__PURE__ */ jsxRuntimeExports.jsxs(
66
+ "div",
67
+ {
68
+ "data-xform-id": htmlId,
69
+ className: cx("form-item", className),
70
+ style: {
71
+ "--label-width": asCSSLength(labelWidth),
72
+ "--control-width": asCSSLength(controlWidth),
73
+ ...style
74
+ },
75
+ children: [
76
+ label == null && tip == null ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs("label", { className: "form-item-label", htmlFor: htmlId, style: labelStyle, children: [
77
+ asterisk && /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "required-indicator" }),
78
+ label && /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "form-item-label-text", children: label })
79
+ ] }),
80
+ /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "form-item-control", style: controlStyle, children: [
81
+ children,
82
+ help && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "help", children: help }),
83
+ error && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "error-message", children: error })
84
+ ] }),
85
+ rightNode
86
+ ]
87
+ }
88
+ );
89
+ }
90
+ function createFormItem(inputOptions) {
91
+ const options = processCreationOptions(inputOptions);
92
+ function FormItemComponent({
93
+ defaultValue: defaultValueProp,
94
+ isEmpty = options.isEmpty,
95
+ renderPreview = options.renderPreview,
96
+ componentProps: componentPropsProp,
97
+ name,
98
+ field: fieldProp,
99
+ ...props
100
+ }) {
101
+ const formEnv = useFormEnv();
102
+ const model = useModel();
103
+ const field = resolveField(fieldProp, model, name, props.value);
104
+ const isPreview = composeValue(props.isPreview, formEnv.isPreview);
105
+ const error = composeValue(props.error, field.state.error);
106
+ const defaultValue = composeValue(defaultValueProp, options.defaultValue);
107
+ const value = toJS(composeValue(field.value, defaultValue));
108
+ const htmlId = Field.getHtmlId(formEnv.htmlIdPrefix, field);
109
+ const componentProps = {
110
+ id: htmlId,
111
+ ...isPreview ? { isPreview: true } : null,
112
+ // dataSource, readOnly, disabled,options 允许直接透传
113
+ ...pick(props, ["dataSource", "readOnly", "disabled", "options"]),
114
+ ...componentPropsProp,
115
+ // status 优先用 prop 中的值,然后再根据 error 自动判断
116
+ [options.statusPropName]: composeValue(
117
+ componentPropsProp == null ? void 0 : componentPropsProp[options.statusPropName],
118
+ composeValue(props[options.statusPropName], error ? "error" : void 0)
119
+ ),
120
+ [options.valuePropName]: composeValue(props[options.valuePropName], value),
121
+ onChange: composeValue(props.onChange, field.handleChange),
122
+ onFocus: composeValue(props.onFocus, field.handleFocus),
123
+ onBlur: composeValue(props.onBlur, field.handleBlur)
124
+ };
125
+ if (options.withField) {
126
+ componentProps.field = field;
127
+ }
128
+ const fieldConfig = {
129
+ htmlId,
130
+ valueProp: props[options.valuePropName],
131
+ defaultValue,
132
+ defaultValueProp,
133
+ isEmpty,
134
+ validateOnChange: formEnv.validateOnChange,
135
+ validateOnBlur: formEnv.validateOnBlur,
136
+ validateOnMount: formEnv.validateOnMount,
137
+ writeDefaultValueToModel: formEnv.writeDefaultValueToModel,
138
+ autoUnmount: formEnv.autoUnmount,
139
+ ...props
140
+ };
141
+ useLayoutEffect(() => field._track(fieldConfig));
142
+ useLayoutEffect(() => {
143
+ if (fieldConfig.writeDefaultValueToModel === "force") {
144
+ return reaction(
145
+ () => field.value,
146
+ () => {
147
+ if (field.value === void 0 && fieldConfig.defaultValueProp !== void 0) {
148
+ field.value = fieldConfig.defaultValueProp;
149
+ }
150
+ },
151
+ { fireImmediately: true }
152
+ );
153
+ } else if (fieldConfig.writeDefaultValueToModel) {
154
+ if (field.value === void 0 && fieldConfig.defaultValueProp !== void 0) {
155
+ runInAction(() => {
156
+ field.value = fieldConfig.defaultValueProp;
157
+ });
158
+ }
159
+ }
160
+ return () => {
161
+ if (fieldConfig.autoUnmount) {
162
+ field.clear();
163
+ }
164
+ };
165
+ }, []);
166
+ useEffect(() => {
167
+ if (fieldConfig.validateOnMount) {
168
+ field.validate("mount");
169
+ const cancel = field.state.cancelValidation;
170
+ return () => {
171
+ cancel == null ? void 0 : cancel();
172
+ };
173
+ }
174
+ }, []);
175
+ if (options.hidden) {
176
+ if (props.renderHiddenFormItemView === false) {
177
+ return null;
178
+ }
179
+ }
180
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(
181
+ FormItemView,
182
+ {
183
+ htmlId,
184
+ label: props.label,
185
+ help: props.help,
186
+ asterisk: props.asterisk ?? props.required,
187
+ error,
188
+ tip: props.tip,
189
+ style: props.style,
190
+ className: cx(props.className, {
191
+ "form-item-hidden": options.hidden,
192
+ "form-item-preview": isPreview,
193
+ "auto-control-width": options.hasIntrinsicWidth
194
+ }),
195
+ labelWidth: props.labelWidth,
196
+ labelStyle: props.labelStyle,
197
+ controlWidth: props.controlWidth,
198
+ controlStyle: props.controlStyle,
199
+ rightNode: props.rightNode,
200
+ children: isPreview ? renderPreview(componentProps) : options.render(componentProps)
201
+ }
202
+ );
203
+ }
204
+ FormItemComponent.displayName = `FormItem__${options.name}`;
205
+ return observer(FormItemComponent);
206
+ }
207
+ const COMPONENT_DICT = {};
208
+ for (const config of ALL_COMPONENTS) {
209
+ const Component = createFormItem(config);
210
+ COMPONENT_DICT[config.name] = Component;
211
+ }
212
+ const AnonymousFormItem = createFormItem({
213
+ name: "anonymous",
214
+ render({ $Component: Component, ...props }) {
215
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...props });
216
+ }
217
+ });
218
+ const NotFound = createFormItem({
219
+ name: "notFound",
220
+ isEmpty: () => false,
221
+ render({ $Component }) {
222
+ return /* @__PURE__ */ jsxRuntimeExports.jsxs(
223
+ "div",
224
+ {
225
+ style: {
226
+ border: "1px dashed red",
227
+ fontSize: 14,
228
+ padding: 4,
229
+ color: "red"
230
+ },
231
+ children: [
232
+ /* @__PURE__ */ jsxRuntimeExports.jsxs("code", { children: [
233
+ "<FormItem component='",
234
+ $Component,
235
+ "' />"
236
+ ] }),
237
+ " ",
238
+ "没有找到对应组件,请检查组件名称是否拼写正确"
239
+ ]
240
+ }
241
+ );
242
+ }
243
+ });
244
+ const Hidden = createFormItem({
245
+ name: "hidden",
246
+ hidden: true,
247
+ hasIntrinsicWidth: false,
248
+ defaultValue: void 0,
249
+ isEmpty() {
250
+ return false;
251
+ },
252
+ render({ id, value }) {
253
+ return /* @__PURE__ */ jsxRuntimeExports.jsx("input", { type: "hidden", value: stringifyObject(value), id });
254
+ }
255
+ });
256
+ function FormItem({ use, component, ...props }) {
257
+ if (use === false) {
258
+ return null;
259
+ }
260
+ if (component == null) {
261
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: { $Component: String(component) } });
262
+ } else if (typeof component === "string") {
263
+ if (component === "hidden") {
264
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(Hidden, { ...props });
265
+ }
266
+ const Comp = COMPONENT_DICT[component];
267
+ if (Comp == null) {
268
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: { $Component: component } });
269
+ }
270
+ return require$$0.createElement(Comp, props);
271
+ } else {
272
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(
273
+ AnonymousFormItem,
274
+ {
275
+ ...props,
276
+ componentProps: { ...props.componentProps, $Component: component }
277
+ }
278
+ );
279
+ }
280
+ }
281
+ export {
282
+ AnonymousFormItem,
283
+ FormItem,
284
+ FormItemView,
285
+ createFormItem
286
+ };
@@ -0,0 +1,22 @@
1
+ import { default as React } from 'react';
2
+ import { FormModel } from './model';
3
+ import { FormProps, FormArrayProps } from './type';
4
+ import { FormSubmit } from '../form-ui/default-button';
5
+ export declare function Form({ model: modelProp, defaultValue, children, className, style, layout, containerProps, htmlIdPrefix: htmlIdPrefixProp, ...restEnvProps }: FormProps): import("react/jsx-runtime").JSX.Element;
6
+ export declare namespace Form {
7
+ var Submit: typeof FormSubmit;
8
+ var ModelConsumer: (({ children }: React.ConsumerProps<FormModel<any>>) => React.ReactElement) & {
9
+ displayName: string;
10
+ };
11
+ var ModelProvider: React.Provider<FormModel<any>>;
12
+ var Array: (<T extends unknown>({ name, children, layout, arrayModel: arrayModelProp, use, }: FormArrayProps<T>) => import("react/jsx-runtime").JSX.Element) & {
13
+ displayName: string;
14
+ };
15
+ var Object: (({ name, children, use }: {
16
+ children: React.ReactNode;
17
+ name: string;
18
+ use?: boolean;
19
+ }) => import("react/jsx-runtime").JSX.Element) & {
20
+ displayName: string;
21
+ };
22
+ }
@@ -0,0 +1,73 @@
1
+ import { j as jsxRuntimeExports } from "../node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js";
2
+ import "../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";
3
+ import { useState } from "react";
4
+ import { FormModel } from "./model.js";
5
+ import { useHtmlIdPrefix, composeValue, range } from "./common-utils.js";
6
+ import { FormEnvProvider } from "./context/formEnvContext.js";
7
+ import { ModelProvider, useModel } from "./context/modelContext.js";
8
+ import { FormLayout } from "./layout.js";
9
+ import { FormSubmit } from "../form-ui/default-button.js";
10
+ import { arrayHelpers } from "./array-helper.js";
11
+ import { observer } from "../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";
12
+ function Form({
13
+ model: modelProp,
14
+ defaultValue,
15
+ children,
16
+ className,
17
+ style,
18
+ layout,
19
+ containerProps,
20
+ htmlIdPrefix: htmlIdPrefixProp,
21
+ ...restEnvProps
22
+ }) {
23
+ const [_model] = useState(() => new FormModel(defaultValue));
24
+ const model = composeValue(modelProp, _model);
25
+ const htmlIdPrefix = useHtmlIdPrefix(htmlIdPrefixProp);
26
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(FormEnvProvider, { htmlIdPrefix, ...restEnvProps, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: model, children: /* @__PURE__ */ jsxRuntimeExports.jsx(FormLayout, { style, className, containerProps, ...layout, children }) }) });
27
+ }
28
+ const FormModelConsumer = observer(({ children }) => {
29
+ const model = useModel();
30
+ return children(model);
31
+ });
32
+ const defaultArrayLayout = ({ arrayModel, itemContent, itemCount }) => {
33
+ return range(itemCount).map(
34
+ (itemIndex) => arrayHelpers.renderArrayItem(arrayModel, itemIndex, itemContent)
35
+ );
36
+ };
37
+ const FormArray = observer(
38
+ ({
39
+ name,
40
+ children,
41
+ layout,
42
+ arrayModel: arrayModelProp,
43
+ use
44
+ }) => {
45
+ var _a;
46
+ const parent = useModel();
47
+ if (use === false) {
48
+ return null;
49
+ }
50
+ const arrayModel = arrayModelProp ?? (name === "&" ? parent : parent.getSubModel(name));
51
+ const itemCount = ((_a = arrayModel.values) == null ? void 0 : _a.length) ?? 0;
52
+ const itemContent = typeof children === "function" ? children : () => children;
53
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: arrayModel, children: (layout ?? defaultArrayLayout)({ arrayModel, itemCount, itemContent }) });
54
+ }
55
+ );
56
+ const FormObject = observer(
57
+ ({ name, children, use }) => {
58
+ const parent = useModel();
59
+ if (use === false) {
60
+ return null;
61
+ }
62
+ const model = name === "&" ? parent : parent.getSubModel(name);
63
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: model, children });
64
+ }
65
+ );
66
+ Form.Submit = FormSubmit;
67
+ Form.ModelConsumer = FormModelConsumer;
68
+ Form.ModelProvider = ModelProvider;
69
+ Form.Array = FormArray;
70
+ Form.Object = FormObject;
71
+ export {
72
+ Form
73
+ };
@@ -0,0 +1,4 @@
1
+ import { FormLayoutProps, FormItemGroupProps } from './type';
2
+ export declare function FormLayout({ children, className, style, labelPosition, labelWidth, formItemGap, controlWidth, defaultLabelTopPosition, inlineError, containerProps, }: FormLayoutProps): import("react/jsx-runtime").JSX.Element;
3
+ /** @deprecated 请使用 Form.ItemView 代替 ItemGroup */
4
+ export declare const FormItemGroup: ({ label, asterisk, tip, children, labelWidth, controlWidth, className, style, inline, }: FormItemGroupProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,180 @@
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 styled from "styled-components";
4
+ import { asCSSLength } from "./common-utils.js";
5
+ const FormLayoutContainer = styled.div`
6
+ --label-width: auto;
7
+ --control-width: auto;
8
+ --form-item-gap: 0;
9
+ --form-item-label-top-position: 0;
10
+
11
+ .form-item {
12
+ margin-bottom: var(--form-item-gap);
13
+ line-height: 1.5;
14
+
15
+ &.auto-control-width {
16
+ --control-width: auto;
17
+ }
18
+ }
19
+
20
+ .form-item-label {
21
+ display: block;
22
+ }
23
+
24
+ .form-item-label-text {
25
+ color: #666;
26
+ }
27
+
28
+ .form-item.minimal {
29
+ margin-bottom: 0;
30
+
31
+ > .error-message,
32
+ > .form-item-label {
33
+ display: none;
34
+ }
35
+ }
36
+
37
+ &.horizontal {
38
+ .form-item {
39
+ display: flex;
40
+ align-items: flex-start;
41
+ }
42
+
43
+ .form-item-label {
44
+ flex: 0 0 var(--label-width);
45
+ padding-top: var(--form-item-label-top-position);
46
+ padding-right: 12px;
47
+ text-align: right;
48
+ }
49
+ }
50
+
51
+ .form-item.form-item-hidden {
52
+ display: none;
53
+ }
54
+
55
+ &.vertical {
56
+ .form-item-label {
57
+ font-size: 12px;
58
+ margin-bottom: 4px;
59
+ }
60
+ }
61
+
62
+ &.inline-error .error-message {
63
+ position: absolute;
64
+ right: 0;
65
+ top: -24px;
66
+ }
67
+
68
+ .form-item-preview {
69
+ > .form-item-label {
70
+ padding-top: 0;
71
+ }
72
+ }
73
+
74
+ .required-indicator {
75
+ margin-right: 4px;
76
+ color: #eb4141;
77
+ }
78
+
79
+ .required-indicator::before {
80
+ content: '*';
81
+ }
82
+
83
+ .error-message {
84
+ margin-top: 2px;
85
+ color: #eb4141;
86
+ }
87
+
88
+ .tip {
89
+ margin-left: 4px;
90
+ color: #999;
91
+ }
92
+
93
+ .help {
94
+ color: #999999;
95
+ margin-top: 4px;
96
+ }
97
+
98
+ .form-item-control {
99
+ position: relative;
100
+ width: var(--control-width);
101
+
102
+ > .next-input,
103
+ > .next-select {
104
+ width: 100%;
105
+ }
106
+
107
+ > .next-range {
108
+ margin: 8px 0;
109
+ }
110
+
111
+ > .next-rating {
112
+ margin-top: 4px;
113
+ margin-bottom: 2px;
114
+ }
115
+
116
+ > .next-checkbox-group,
117
+ > .next-radio-group,
118
+ > .next-checkbox-wrapper {
119
+ display: inline-block;
120
+ line-height: 18px;
121
+ }
122
+ }
123
+
124
+ &.horizontal .form-item-control {
125
+ > .next-checkbox-group,
126
+ > .next-radio-group,
127
+ > .next-checkbox-wrapper {
128
+ padding-top: 8px;
129
+ }
130
+ }
131
+ `;
132
+ function FormLayout({
133
+ children,
134
+ className,
135
+ style,
136
+ labelPosition = "left",
137
+ labelWidth = labelPosition === "left" ? 120 : "auto",
138
+ formItemGap = labelPosition === "left" ? 12 : 16,
139
+ controlWidth = 320,
140
+ defaultLabelTopPosition = 4,
141
+ inlineError,
142
+ containerProps
143
+ }) {
144
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(
145
+ FormLayoutContainer,
146
+ {
147
+ style: {
148
+ "--label-width": asCSSLength(labelWidth),
149
+ "--control-width": asCSSLength(controlWidth),
150
+ "--form-item-gap": asCSSLength(formItemGap),
151
+ "--form-item-label-top-position": asCSSLength(defaultLabelTopPosition),
152
+ ...style
153
+ },
154
+ className: cx(
155
+ "xform-container",
156
+ {
157
+ horizontal: labelPosition === "left",
158
+ vertical: labelPosition === "top",
159
+ "inline-error": labelPosition === "top" && inlineError
160
+ },
161
+ className
162
+ ),
163
+ ...containerProps,
164
+ children
165
+ }
166
+ );
167
+ }
168
+ styled.div`
169
+ ${FormLayoutContainer}.horizontal & {
170
+ display: flex;
171
+ }
172
+ &.inline {
173
+ .form-item-group-content {
174
+ display: flex;
175
+ }
176
+ }
177
+ `;
178
+ export {
179
+ FormLayout
180
+ };
@@ -0,0 +1,23 @@
1
+ import { Field, FormModel } from './model';
2
+ import { FieldValidateTrigger, FormEnvContextType, SubmitOptions } from './type';
3
+ export declare const modelUtils: {
4
+ clearError: <T>(model: FormModel<T>) => void;
5
+ scrollToFirstError(errorFields: Field[]): void;
6
+ animateErrorFields(errorFields: Field[]): void;
7
+ validateAll: <T>(model: FormModel<T>, trigger?: FieldValidateTrigger) => Promise<{
8
+ hasError: boolean;
9
+ errors: any;
10
+ errorFields: Field<unknown>[];
11
+ }>;
12
+ submit: <T>(model: FormModel<T>, options?: SubmitOptions) => Promise<void>;
13
+ reset: <T>(model: FormModel<T>, { onReset }?: Pick<FormEnvContextType, "onReset">) => void;
14
+ acceptValuesFormView: (model: FormModel, opts?: {
15
+ mergeDefaultValue?: boolean;
16
+ }) => void;
17
+ mergeValuesFromViewToView: typeof _mergeValuesFromViewToTarget;
18
+ };
19
+ /** 遍历 model 下当前挂载的 field 对象,将字段值或字段默认值写入到 target 中 */
20
+ declare function _mergeValuesFromViewToTarget(target: any, model: FormModel, { mergeDefaultValue }?: {
21
+ mergeDefaultValue?: boolean;
22
+ }): void;
23
+ export {};