react-antd-xform 1.0.7 → 1.0.8
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.
- package/dist/form/array-helper.js +11 -19
- package/dist/form/common-utils.js +1 -3
- package/dist/form/context/formEnvContext.js +4 -1
- package/dist/form/extend/form-array.js +26 -22
- package/dist/form/extend/form-check.js +5 -7
- package/dist/form/extend/form-effect.js +10 -7
- package/dist/form/extend/form-layout.js +11 -23
- package/dist/form/extend/form-model-consumer.js +3 -1
- package/dist/form/extend/form-object.js +11 -9
- package/dist/form/extend/form-reset.js +1 -9
- package/dist/form/extend/form-submit.js +5 -10
- package/dist/form/form-item.js +77 -97
- package/dist/form/helpers/AsyncValue.js +17 -19
- package/dist/form/model-utils.js +54 -46
- package/dist/form/model.js +42 -69
- package/dist/form-ui/common-utils.js +101 -102
- package/dist/form-ui/default-component.js +3 -1
- package/dist/form-ui/render-preview.js +8 -13
- package/package.json +3 -1
|
@@ -4,10 +4,7 @@ import invariant from "../node_modules/.pnpm/invariant@2.2.4/node_modules/invari
|
|
|
4
4
|
import { Form } from "./index.js";
|
|
5
5
|
import { composeValue } from "./common-utils.js";
|
|
6
6
|
function invariantArrayShapeIsAutoOrArray(arrayModel) {
|
|
7
|
-
invariant(
|
|
8
|
-
arrayModel._valueShape === "auto" || arrayModel._valueShape === "array",
|
|
9
|
-
'arrayModel._valueShape should be "auto" or "array"'
|
|
10
|
-
);
|
|
7
|
+
invariant(arrayModel._valueShape === "auto" || arrayModel._valueShape === "array", 'arrayModel._valueShape should be "auto" or "array"');
|
|
11
8
|
}
|
|
12
9
|
function updateSubModelsNames(arrayModel) {
|
|
13
10
|
arrayModel._subModels.forEach((mod, index) => {
|
|
@@ -34,10 +31,7 @@ const arrayHelpers = {
|
|
|
34
31
|
if (arrayModel.values == null) {
|
|
35
32
|
arrayModel.values = [];
|
|
36
33
|
}
|
|
37
|
-
const value = composeValue(
|
|
38
|
-
typeof itemFactory === "function" ? itemFactory(arrayModel) : itemFactory,
|
|
39
|
-
{}
|
|
40
|
-
);
|
|
34
|
+
const value = composeValue(typeof itemFactory === "function" ? itemFactory(arrayModel) : itemFactory, {});
|
|
41
35
|
arrayModel.values.push(value);
|
|
42
36
|
}),
|
|
43
37
|
delete: action((arrayModel, itemIndex) => {
|
|
@@ -90,18 +84,16 @@ const arrayHelpers = {
|
|
|
90
84
|
arrayModel._subModels.length = 0;
|
|
91
85
|
}
|
|
92
86
|
}),
|
|
93
|
-
move: action(
|
|
94
|
-
(arrayModel
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
reorderInPlace(arrayModel.values, fromIndex, toIndex);
|
|
98
|
-
}
|
|
99
|
-
if (Array.isArray(arrayModel._subModels)) {
|
|
100
|
-
reorderInPlace(arrayModel._subModels, fromIndex, toIndex);
|
|
101
|
-
updateSubModelsNames(arrayModel);
|
|
102
|
-
}
|
|
87
|
+
move: action((arrayModel, fromIndex, toIndex) => {
|
|
88
|
+
invariantArrayShapeIsAutoOrArray(arrayModel);
|
|
89
|
+
if (Array.isArray(arrayModel.values)) {
|
|
90
|
+
reorderInPlace(arrayModel.values, fromIndex, toIndex);
|
|
103
91
|
}
|
|
104
|
-
|
|
92
|
+
if (Array.isArray(arrayModel._subModels)) {
|
|
93
|
+
reorderInPlace(arrayModel._subModels, fromIndex, toIndex);
|
|
94
|
+
updateSubModelsNames(arrayModel);
|
|
95
|
+
}
|
|
96
|
+
}),
|
|
105
97
|
renderArrayItem(arrayModel, itemIndex, itemContent) {
|
|
106
98
|
const itemModel = arrayModel.getSubModel(itemIndex);
|
|
107
99
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Form.ModelProvider, { value: itemModel, children: itemContent(itemIndex, itemModel) }, itemModel.id);
|
|
@@ -16,7 +16,10 @@ function FormEnvProvider({
|
|
|
16
16
|
...override
|
|
17
17
|
}) {
|
|
18
18
|
const parent = useFormEnv();
|
|
19
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(FormEnvContext.Provider, { value: {
|
|
19
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(FormEnvContext.Provider, { value: {
|
|
20
|
+
...parent,
|
|
21
|
+
...override
|
|
22
|
+
}, children });
|
|
20
23
|
}
|
|
21
24
|
export {
|
|
22
25
|
FormEnvProvider,
|
|
@@ -4,30 +4,34 @@ import { useModel, ModelProvider } from "../context/modelContext.js";
|
|
|
4
4
|
import { range } from "../common-utils.js";
|
|
5
5
|
import { arrayHelpers } from "../array-helper.js";
|
|
6
6
|
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";
|
|
7
|
-
const defaultArrayLayout = ({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const defaultArrayLayout = ({
|
|
8
|
+
arrayModel,
|
|
9
|
+
itemContent,
|
|
10
|
+
itemCount
|
|
11
|
+
}) => {
|
|
12
|
+
return range(itemCount).map((itemIndex) => arrayHelpers.renderArrayItem(arrayModel, itemIndex, itemContent));
|
|
11
13
|
};
|
|
12
|
-
const FormArray = observer(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
const arrayModel = arrayModelProp ?? (name === "&" ? parent : parent.getSubModel(name));
|
|
26
|
-
const itemCount = ((_a = arrayModel.values) == null ? void 0 : _a.length) ?? 0;
|
|
27
|
-
const itemContent = typeof children === "function" ? children : () => children;
|
|
28
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: arrayModel, children: (layout ?? defaultArrayLayout)({ arrayModel, itemCount, itemContent }) });
|
|
14
|
+
const FormArray = observer(({
|
|
15
|
+
name,
|
|
16
|
+
children,
|
|
17
|
+
layout,
|
|
18
|
+
arrayModel: arrayModelProp,
|
|
19
|
+
use
|
|
20
|
+
}) => {
|
|
21
|
+
var _a;
|
|
22
|
+
const parent = useModel();
|
|
23
|
+
if (use === false) {
|
|
24
|
+
return null;
|
|
29
25
|
}
|
|
30
|
-
);
|
|
26
|
+
const arrayModel = arrayModelProp ?? (name === "&" ? parent : parent.getSubModel(name));
|
|
27
|
+
const itemCount = ((_a = arrayModel.values) == null ? void 0 : _a.length) ?? 0;
|
|
28
|
+
const itemContent = typeof children === "function" ? children : () => children;
|
|
29
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: arrayModel, children: (layout ?? defaultArrayLayout)({
|
|
30
|
+
arrayModel,
|
|
31
|
+
itemCount,
|
|
32
|
+
itemContent
|
|
33
|
+
}) });
|
|
34
|
+
});
|
|
31
35
|
export {
|
|
32
36
|
FormArray
|
|
33
37
|
};
|
|
@@ -23,13 +23,11 @@ const FormCheck = observer(function FormCheck2({
|
|
|
23
23
|
};
|
|
24
24
|
useLayoutEffect(() => check._track(checkConfig));
|
|
25
25
|
useEffect(() => {
|
|
26
|
-
return reaction(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
{ fireImmediately: checkConfig.validateOnMount }
|
|
32
|
-
);
|
|
26
|
+
return reaction(convertWatchableToExpression(watch, model), () => {
|
|
27
|
+
check.validate();
|
|
28
|
+
}, {
|
|
29
|
+
fireImmediately: checkConfig.validateOnMount
|
|
30
|
+
});
|
|
33
31
|
}, [check, model, ...deps]);
|
|
34
32
|
if (renderError === true) {
|
|
35
33
|
return check.error;
|
|
@@ -17,13 +17,16 @@ const FormEffect = observer(function FormEffect2({
|
|
|
17
17
|
effectRef.current = effect;
|
|
18
18
|
});
|
|
19
19
|
useEffect(() => {
|
|
20
|
-
return reaction(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
return reaction(convertWatchableToExpression(watch, model), (next, prev) => {
|
|
21
|
+
effectRef.current(next, {
|
|
22
|
+
model,
|
|
23
|
+
prev,
|
|
24
|
+
next
|
|
25
|
+
});
|
|
26
|
+
}, {
|
|
27
|
+
fireImmediately,
|
|
28
|
+
equals
|
|
29
|
+
});
|
|
27
30
|
}, deps);
|
|
28
31
|
return null;
|
|
29
32
|
});
|
|
@@ -141,29 +141,17 @@ function FormLayout({
|
|
|
141
141
|
inlineError,
|
|
142
142
|
containerProps
|
|
143
143
|
}) {
|
|
144
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
);
|
|
144
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(FormLayoutContainer, { style: {
|
|
145
|
+
"--label-width": asCSSLength(labelWidth),
|
|
146
|
+
"--control-width": asCSSLength(controlWidth),
|
|
147
|
+
"--form-item-gap": asCSSLength(formItemGap),
|
|
148
|
+
"--form-item-label-top-position": asCSSLength(defaultLabelTopPosition),
|
|
149
|
+
...style
|
|
150
|
+
}, className: cx("xform-container", {
|
|
151
|
+
horizontal: labelPosition === "left",
|
|
152
|
+
vertical: labelPosition === "top",
|
|
153
|
+
"inline-error": labelPosition === "top" && inlineError
|
|
154
|
+
}, className), ...containerProps, children });
|
|
167
155
|
}
|
|
168
156
|
export {
|
|
169
157
|
FormLayout
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
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";
|
|
2
2
|
import { useModel } from "../context/modelContext.js";
|
|
3
3
|
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";
|
|
4
|
-
const FormModelConsumer = observer(({
|
|
4
|
+
const FormModelConsumer = observer(({
|
|
5
|
+
children
|
|
6
|
+
}) => {
|
|
5
7
|
const model = useModel();
|
|
6
8
|
return children(model);
|
|
7
9
|
});
|
|
@@ -2,16 +2,18 @@ import { j as jsxRuntimeExports } from "../../node_modules/.pnpm/react@18.3.1/no
|
|
|
2
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
3
|
import { useModel, ModelProvider } from "../context/modelContext.js";
|
|
4
4
|
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";
|
|
5
|
-
const FormObject = observer(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return
|
|
5
|
+
const FormObject = observer(({
|
|
6
|
+
name,
|
|
7
|
+
children,
|
|
8
|
+
use
|
|
9
|
+
}) => {
|
|
10
|
+
const parent = useModel();
|
|
11
|
+
if (use === false) {
|
|
12
|
+
return null;
|
|
13
13
|
}
|
|
14
|
-
);
|
|
14
|
+
const model = name === "&" ? parent : parent.getSubModel(name);
|
|
15
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: model, children });
|
|
16
|
+
});
|
|
15
17
|
export {
|
|
16
18
|
FormObject
|
|
17
19
|
};
|
|
@@ -10,15 +10,7 @@ function FormReset({
|
|
|
10
10
|
}) {
|
|
11
11
|
const model = useModel();
|
|
12
12
|
const formEnv = useFormEnv();
|
|
13
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
14
|
-
ButtonComponent,
|
|
15
|
-
{
|
|
16
|
-
...props,
|
|
17
|
-
onClick: action(() => modelUtils.reset(model, formEnv)),
|
|
18
|
-
children,
|
|
19
|
-
...props
|
|
20
|
-
}
|
|
21
|
-
);
|
|
13
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(ButtonComponent, { ...props, onClick: action(() => modelUtils.reset(model, formEnv)), children, ...props });
|
|
22
14
|
}
|
|
23
15
|
export {
|
|
24
16
|
FormReset
|
|
@@ -8,20 +8,15 @@ function FormSubmit({
|
|
|
8
8
|
...props
|
|
9
9
|
}) {
|
|
10
10
|
const model = useModel();
|
|
11
|
-
const {
|
|
11
|
+
const {
|
|
12
|
+
onSubmit,
|
|
13
|
+
onError
|
|
14
|
+
} = useFormEnv();
|
|
12
15
|
const submitOptions = {
|
|
13
16
|
onSubmit,
|
|
14
17
|
onError
|
|
15
18
|
};
|
|
16
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
17
|
-
ButtonComponent,
|
|
18
|
-
{
|
|
19
|
-
...props,
|
|
20
|
-
onClick: () => modelUtils.submit(model, submitOptions),
|
|
21
|
-
children,
|
|
22
|
-
...props
|
|
23
|
-
}
|
|
24
|
-
);
|
|
19
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(ButtonComponent, { ...props, onClick: () => modelUtils.submit(model, submitOptions), children, ...props });
|
|
25
20
|
}
|
|
26
21
|
export {
|
|
27
22
|
FormSubmit
|
package/dist/form/form-item.js
CHANGED
|
@@ -61,34 +61,25 @@ function FormItemView({
|
|
|
61
61
|
labelStyle,
|
|
62
62
|
controlStyle
|
|
63
63
|
}) {
|
|
64
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsxs(
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
children
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
children,
|
|
81
|
-
help && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "help", children: help }),
|
|
82
|
-
error && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "error-message", children: error })
|
|
83
|
-
] }),
|
|
84
|
-
rightNode
|
|
85
|
-
]
|
|
86
|
-
}
|
|
87
|
-
);
|
|
64
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { "data-xform-id": htmlId, className: cx("form-item", className), style: {
|
|
65
|
+
"--label-width": asCSSLength(labelWidth),
|
|
66
|
+
"--control-width": asCSSLength(controlWidth),
|
|
67
|
+
...style
|
|
68
|
+
}, children: [
|
|
69
|
+
label == null && tip == null ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs("label", { className: "form-item-label", htmlFor: htmlId, style: labelStyle, children: [
|
|
70
|
+
asterisk && /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "required-indicator" }),
|
|
71
|
+
label && /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "form-item-label-text", children: label })
|
|
72
|
+
] }),
|
|
73
|
+
/* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "form-item-control", style: controlStyle, children: [
|
|
74
|
+
children,
|
|
75
|
+
help && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "help", children: help }),
|
|
76
|
+
error && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "error-message", children: error })
|
|
77
|
+
] }),
|
|
78
|
+
rightNode
|
|
79
|
+
] });
|
|
88
80
|
}
|
|
89
81
|
function createFormItem(inputOptions) {
|
|
90
82
|
const options = processCreationOptions(inputOptions);
|
|
91
|
-
console.log(options, "options");
|
|
92
83
|
function FormItemComponent({
|
|
93
84
|
defaultValue: defaultValueProp,
|
|
94
85
|
isEmpty = options.isEmpty,
|
|
@@ -110,15 +101,14 @@ function createFormItem(inputOptions) {
|
|
|
110
101
|
const componentProps = {
|
|
111
102
|
id: htmlId,
|
|
112
103
|
variant,
|
|
113
|
-
...isPreview ? {
|
|
104
|
+
...isPreview ? {
|
|
105
|
+
isPreview: true
|
|
106
|
+
} : null,
|
|
114
107
|
// dataSource, readOnly, disabled,options 允许直接透传
|
|
115
108
|
...pick(props, ["dataSource", "readOnly", "disabled", "options"]),
|
|
116
109
|
...componentPropsProp,
|
|
117
110
|
// status 优先用 prop 中的值,然后再根据 error 自动判断
|
|
118
|
-
[options.statusPropName]: composeValue(
|
|
119
|
-
componentPropsProp == null ? void 0 : componentPropsProp[options.statusPropName],
|
|
120
|
-
composeValue(props[options.statusPropName], error ? "error" : void 0)
|
|
121
|
-
),
|
|
111
|
+
[options.statusPropName]: composeValue(componentPropsProp == null ? void 0 : componentPropsProp[options.statusPropName], composeValue(props[options.statusPropName], error ? "error" : void 0)),
|
|
122
112
|
[options.valuePropName]: composeValue(props[options.valuePropName], value),
|
|
123
113
|
onChange: composeValue(props.onChange, field.handleChange),
|
|
124
114
|
onFocus: composeValue(props.onFocus, field.handleFocus),
|
|
@@ -143,15 +133,13 @@ function createFormItem(inputOptions) {
|
|
|
143
133
|
useLayoutEffect(() => field._track(fieldConfig));
|
|
144
134
|
useLayoutEffect(() => {
|
|
145
135
|
if (fieldConfig.writeDefaultValueToModel === "force") {
|
|
146
|
-
return reaction(
|
|
147
|
-
(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
{ fireImmediately: true }
|
|
154
|
-
);
|
|
136
|
+
return reaction(() => field.value, () => {
|
|
137
|
+
if (field.value === void 0 && fieldConfig.defaultValueProp !== void 0) {
|
|
138
|
+
field.value = fieldConfig.defaultValueProp;
|
|
139
|
+
}
|
|
140
|
+
}, {
|
|
141
|
+
fireImmediately: true
|
|
142
|
+
});
|
|
155
143
|
} else if (fieldConfig.writeDefaultValueToModel) {
|
|
156
144
|
if (field.value === void 0 && fieldConfig.defaultValueProp !== void 0) {
|
|
157
145
|
runInAction(() => {
|
|
@@ -179,30 +167,11 @@ function createFormItem(inputOptions) {
|
|
|
179
167
|
return null;
|
|
180
168
|
}
|
|
181
169
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
label: props.label,
|
|
188
|
-
help: props.help,
|
|
189
|
-
asterisk: props.asterisk ?? props.required,
|
|
190
|
-
error,
|
|
191
|
-
tip: props.tip,
|
|
192
|
-
style: props.style,
|
|
193
|
-
className: cx(props.className, {
|
|
194
|
-
"form-item-hidden": options.hidden,
|
|
195
|
-
"form-item-preview": isPreview,
|
|
196
|
-
"auto-control-width": options.hasIntrinsicWidth
|
|
197
|
-
}),
|
|
198
|
-
labelWidth: props.labelWidth,
|
|
199
|
-
labelStyle: props.labelStyle,
|
|
200
|
-
controlWidth: props.controlWidth,
|
|
201
|
-
controlStyle: props.controlStyle,
|
|
202
|
-
rightNode: props.rightNode,
|
|
203
|
-
children: isPreview ? renderPreview(componentProps) : options.render(componentProps)
|
|
204
|
-
}
|
|
205
|
-
);
|
|
170
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(FormItemView, { htmlId, label: props.label, help: props.help, asterisk: props.asterisk ?? props.required, error, tip: props.tip, style: props.style, className: cx(props.className, {
|
|
171
|
+
"form-item-hidden": options.hidden,
|
|
172
|
+
"form-item-preview": isPreview,
|
|
173
|
+
"auto-control-width": options.hasIntrinsicWidth
|
|
174
|
+
}), labelWidth: props.labelWidth, labelStyle: props.labelStyle, controlWidth: props.controlWidth, controlStyle: props.controlStyle, rightNode: props.rightNode, children: isPreview ? renderPreview(componentProps) : options.render(componentProps) });
|
|
206
175
|
}
|
|
207
176
|
FormItemComponent.displayName = `FormItem__${options.name}`;
|
|
208
177
|
return observer(FormItemComponent);
|
|
@@ -214,34 +183,33 @@ for (const config of ALL_COMPONENTS) {
|
|
|
214
183
|
}
|
|
215
184
|
const AnonymousFormItem = createFormItem({
|
|
216
185
|
name: "anonymous",
|
|
217
|
-
render({
|
|
186
|
+
render({
|
|
187
|
+
$Component: Component,
|
|
188
|
+
...props
|
|
189
|
+
}) {
|
|
218
190
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...props });
|
|
219
191
|
}
|
|
220
192
|
});
|
|
221
193
|
const NotFound = createFormItem({
|
|
222
194
|
name: "notFound",
|
|
223
195
|
isEmpty: () => false,
|
|
224
|
-
render({
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
"没有找到对应组件,请检查组件名称是否拼写正确"
|
|
242
|
-
]
|
|
243
|
-
}
|
|
244
|
-
);
|
|
196
|
+
render({
|
|
197
|
+
$Component
|
|
198
|
+
}) {
|
|
199
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { style: {
|
|
200
|
+
border: "1px dashed red",
|
|
201
|
+
fontSize: 14,
|
|
202
|
+
padding: 4,
|
|
203
|
+
color: "red"
|
|
204
|
+
}, children: [
|
|
205
|
+
/* @__PURE__ */ jsxRuntimeExports.jsxs("code", { children: [
|
|
206
|
+
"<FormItem component='",
|
|
207
|
+
$Component,
|
|
208
|
+
"' />"
|
|
209
|
+
] }),
|
|
210
|
+
" ",
|
|
211
|
+
"没有找到对应组件,请检查组件名称是否拼写正确"
|
|
212
|
+
] });
|
|
245
213
|
}
|
|
246
214
|
});
|
|
247
215
|
const Hidden = createFormItem({
|
|
@@ -252,38 +220,50 @@ const Hidden = createFormItem({
|
|
|
252
220
|
isEmpty() {
|
|
253
221
|
return false;
|
|
254
222
|
},
|
|
255
|
-
render({
|
|
223
|
+
render({
|
|
224
|
+
id,
|
|
225
|
+
value
|
|
226
|
+
}) {
|
|
256
227
|
return /* @__PURE__ */ jsxRuntimeExports.jsx("input", { type: "hidden", value: stringifyObject(value), id });
|
|
257
228
|
}
|
|
258
229
|
});
|
|
259
|
-
function FormItem({
|
|
230
|
+
function FormItem({
|
|
231
|
+
use,
|
|
232
|
+
component,
|
|
233
|
+
...props
|
|
234
|
+
}) {
|
|
260
235
|
const formEnv = useFormEnv();
|
|
261
236
|
const variant = formEnv.variant;
|
|
262
237
|
if (use === false) {
|
|
263
238
|
return null;
|
|
264
239
|
}
|
|
265
240
|
if (component == null) {
|
|
266
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: {
|
|
241
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: {
|
|
242
|
+
$Component: String(component)
|
|
243
|
+
} });
|
|
267
244
|
} else if (typeof component === "string") {
|
|
268
245
|
if (component === "hidden") {
|
|
269
246
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Hidden, { ...props });
|
|
270
247
|
}
|
|
271
248
|
const Comp = COMPONENT_DICT[component];
|
|
272
249
|
if (Comp == null) {
|
|
273
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: {
|
|
250
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: {
|
|
251
|
+
$Component: component
|
|
252
|
+
} });
|
|
274
253
|
}
|
|
275
254
|
return React.createElement(Comp, {
|
|
276
255
|
...props,
|
|
277
|
-
componentProps: {
|
|
256
|
+
componentProps: {
|
|
257
|
+
...props.componentProps,
|
|
258
|
+
variant
|
|
259
|
+
}
|
|
278
260
|
});
|
|
279
261
|
} else {
|
|
280
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
);
|
|
262
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(AnonymousFormItem, { ...props, componentProps: {
|
|
263
|
+
...props.componentProps,
|
|
264
|
+
variant,
|
|
265
|
+
$Component: component
|
|
266
|
+
} });
|
|
287
267
|
}
|
|
288
268
|
}
|
|
289
269
|
export {
|
|
@@ -71,27 +71,25 @@ const _AsyncValue = class _AsyncValue {
|
|
|
71
71
|
this.name = typeof options.name === "string" ? options.name : `AsyncValue_${_AsyncValue._globalCount++}`;
|
|
72
72
|
this._atom = createAtom(`${this.name}#state`);
|
|
73
73
|
this._refreshAtom = createAtom(`${this.name}#refresh`);
|
|
74
|
-
makeObservable(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
);
|
|
74
|
+
makeObservable(this, {
|
|
75
|
+
status: computed,
|
|
76
|
+
error: computed,
|
|
77
|
+
_inner_current: computed({
|
|
78
|
+
name: `${this.name}.inner_current`
|
|
79
|
+
}),
|
|
80
|
+
isLoading: computed,
|
|
81
|
+
isError: computed,
|
|
82
|
+
isReady: computed
|
|
83
|
+
}, {
|
|
84
|
+
name: this.name
|
|
85
|
+
});
|
|
86
86
|
this._disposers.push(this._stop);
|
|
87
87
|
this._disposers.push(onBecomeObserved(this._atom, this._start));
|
|
88
|
-
this._disposers.push(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
})
|
|
94
|
-
);
|
|
88
|
+
this._disposers.push(onBecomeUnobserved(this._atom, () => {
|
|
89
|
+
if (!this.options.keepAlive) {
|
|
90
|
+
this._stop();
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
95
93
|
this._disposers.push(() => {
|
|
96
94
|
this._status = "ready";
|
|
97
95
|
this._current = null;
|
package/dist/form/model-utils.js
CHANGED
|
@@ -16,16 +16,16 @@ const modelUtils = {
|
|
|
16
16
|
if (!((_a = field.config) == null ? void 0 : _a.htmlId)) {
|
|
17
17
|
continue;
|
|
18
18
|
}
|
|
19
|
-
const div = document.querySelector(
|
|
20
|
-
`*[data-xform-id="${field.config.htmlId}"]`
|
|
21
|
-
);
|
|
19
|
+
const div = document.querySelector(`*[data-xform-id="${field.config.htmlId}"]`);
|
|
22
20
|
if (!div) {
|
|
23
21
|
continue;
|
|
24
22
|
}
|
|
25
23
|
if (typeof div.scrollIntoViewIfNeeded === "function") {
|
|
26
24
|
div.scrollIntoViewIfNeeded();
|
|
27
25
|
} else {
|
|
28
|
-
div.scrollIntoView({
|
|
26
|
+
div.scrollIntoView({
|
|
27
|
+
block: "nearest"
|
|
28
|
+
});
|
|
29
29
|
}
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
@@ -36,23 +36,31 @@ const modelUtils = {
|
|
|
36
36
|
if (!((_a = field.config) == null ? void 0 : _a.htmlId)) {
|
|
37
37
|
continue;
|
|
38
38
|
}
|
|
39
|
-
const div = document.querySelector(
|
|
40
|
-
`*[data-xform-id="${field.config.htmlId}"]`
|
|
41
|
-
);
|
|
39
|
+
const div = document.querySelector(`*[data-xform-id="${field.config.htmlId}"]`);
|
|
42
40
|
if (!div) {
|
|
43
41
|
continue;
|
|
44
42
|
}
|
|
45
|
-
div.animate(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
div.animate([{
|
|
44
|
+
offset: 0,
|
|
45
|
+
transform: "translateX(0)"
|
|
46
|
+
}, {
|
|
47
|
+
offset: 0.065,
|
|
48
|
+
transform: "translateX(-6px) rotateY(-9deg)"
|
|
49
|
+
}, {
|
|
50
|
+
offset: 0.185,
|
|
51
|
+
transform: "translateX(5px) rotateY(7deg)"
|
|
52
|
+
}, {
|
|
53
|
+
offset: 0.315,
|
|
54
|
+
transform: "translateX(-3px) rotateY(-5deg)"
|
|
55
|
+
}, {
|
|
56
|
+
offset: 0.435,
|
|
57
|
+
transform: "translateX(2px) rotateY(3deg)"
|
|
58
|
+
}, {
|
|
59
|
+
offset: 0.5,
|
|
60
|
+
transform: "translateX(0)"
|
|
61
|
+
}], {
|
|
62
|
+
duration: 750
|
|
63
|
+
});
|
|
56
64
|
}
|
|
57
65
|
},
|
|
58
66
|
validateAll: action(function(model, trigger = "*") {
|
|
@@ -64,29 +72,21 @@ const modelUtils = {
|
|
|
64
72
|
if (!field.isMounted) {
|
|
65
73
|
return;
|
|
66
74
|
}
|
|
67
|
-
promises.push(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
)
|
|
77
|
-
);
|
|
75
|
+
promises.push(field.validate(trigger).then(action((error) => {
|
|
76
|
+
if (error) {
|
|
77
|
+
hasError = true;
|
|
78
|
+
observableSetIn(errors, field.path, error);
|
|
79
|
+
errorFields.push(field);
|
|
80
|
+
}
|
|
81
|
+
})));
|
|
78
82
|
});
|
|
79
83
|
model.iterateChecks((check) => {
|
|
80
|
-
promises.push(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
)
|
|
89
|
-
);
|
|
84
|
+
promises.push(check.validate().then(action((error) => {
|
|
85
|
+
if (error) {
|
|
86
|
+
hasError = true;
|
|
87
|
+
observableSetIn(errors, check.path, error);
|
|
88
|
+
}
|
|
89
|
+
})));
|
|
90
90
|
});
|
|
91
91
|
return Promise.all(promises).then(() => ({
|
|
92
92
|
hasError,
|
|
@@ -103,7 +103,11 @@ const modelUtils = {
|
|
|
103
103
|
animateErrorFields = false,
|
|
104
104
|
scrollToFirstError = true
|
|
105
105
|
} = options;
|
|
106
|
-
const {
|
|
106
|
+
const {
|
|
107
|
+
hasError,
|
|
108
|
+
errors,
|
|
109
|
+
errorFields
|
|
110
|
+
} = await modelUtils.validateAll(model);
|
|
107
111
|
if (hasError) {
|
|
108
112
|
if (scrollToFirstError) {
|
|
109
113
|
modelUtils.scrollToFirstError(errorFields);
|
|
@@ -114,15 +118,17 @@ const modelUtils = {
|
|
|
114
118
|
onError == null ? void 0 : onError(errors, model);
|
|
115
119
|
} else if (typeof onSubmit === "function") {
|
|
116
120
|
runInAction(() => {
|
|
117
|
-
const result = observable(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
+
const result = observable(valueFilter === "all" ? toJS(model.values) : model._valueShape === "array" ? [] : {});
|
|
122
|
+
_mergeValuesFromViewToTarget(result, model, {
|
|
123
|
+
mergeDefaultValue
|
|
124
|
+
});
|
|
121
125
|
onSubmit(toJS(result), model);
|
|
122
126
|
});
|
|
123
127
|
}
|
|
124
128
|
}),
|
|
125
|
-
reset: action(function(model, {
|
|
129
|
+
reset: action(function(model, {
|
|
130
|
+
onReset
|
|
131
|
+
} = {}) {
|
|
126
132
|
model.values = {};
|
|
127
133
|
modelUtils.clearError(model);
|
|
128
134
|
onReset == null ? void 0 : onReset(model);
|
|
@@ -132,7 +138,9 @@ const modelUtils = {
|
|
|
132
138
|
}),
|
|
133
139
|
mergeValuesFromViewToView: _mergeValuesFromViewToTarget
|
|
134
140
|
};
|
|
135
|
-
function _mergeValuesFromViewToTarget(target, model, {
|
|
141
|
+
function _mergeValuesFromViewToTarget(target, model, {
|
|
142
|
+
mergeDefaultValue = true
|
|
143
|
+
} = {}) {
|
|
136
144
|
model.iterateFields((field) => {
|
|
137
145
|
if (!field.isMounted) {
|
|
138
146
|
return;
|
package/dist/form/model.js
CHANGED
|
@@ -37,27 +37,23 @@ class FormModel {
|
|
|
37
37
|
this._fieldIdGenerator = new IdGenerator("Field");
|
|
38
38
|
this.id = this._modelIdGenerator.getNextId();
|
|
39
39
|
this._values = composeValue(initValues, {});
|
|
40
|
-
if (this._values == null)
|
|
41
|
-
console.warn("[xform] FormModel 根节点的初始 values 不能为 null");
|
|
42
|
-
}
|
|
40
|
+
if (this._values == null) ;
|
|
43
41
|
}
|
|
44
|
-
makeObservable(
|
|
45
|
-
this
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
{ name: `${this.id}(${this.name})` }
|
|
60
|
-
);
|
|
42
|
+
makeObservable(this, {
|
|
43
|
+
// root model 才会使用 this._values
|
|
44
|
+
_values: this._modelType === ModelType.rootModel ? observable : false,
|
|
45
|
+
values: computed,
|
|
46
|
+
state: observable,
|
|
47
|
+
setValue: action,
|
|
48
|
+
// 注意 name 是可以变化的;在数组元素调换位置的情况下 name 会进行变更
|
|
49
|
+
name: observable.ref,
|
|
50
|
+
path: computed,
|
|
51
|
+
_selfDeleted: observable.ref,
|
|
52
|
+
isDeleted: computed,
|
|
53
|
+
_markDeleted: action
|
|
54
|
+
}, {
|
|
55
|
+
name: `${this.id}(${this.name})`
|
|
56
|
+
});
|
|
61
57
|
}
|
|
62
58
|
get values() {
|
|
63
59
|
if (this._modelType === ModelType.rootModel) {
|
|
@@ -68,15 +64,9 @@ class FormModel {
|
|
|
68
64
|
}
|
|
69
65
|
set values(nextValues) {
|
|
70
66
|
if (this.isDeleted) {
|
|
71
|
-
console.warn(
|
|
72
|
-
"[xform] 对已删除的 Model 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
73
|
-
);
|
|
74
67
|
return;
|
|
75
68
|
}
|
|
76
69
|
if (this._modelType === ModelType.rootModel) {
|
|
77
|
-
if (nextValues == null) {
|
|
78
|
-
console.warn("[xform] FormModel 根节点的 values 不能设置为 null/undefined");
|
|
79
|
-
}
|
|
80
70
|
this._values = nextValues;
|
|
81
71
|
} else {
|
|
82
72
|
this.parent.setValue(this.name, nextValues);
|
|
@@ -94,9 +84,6 @@ class FormModel {
|
|
|
94
84
|
}
|
|
95
85
|
setValue(name, value) {
|
|
96
86
|
if (this.isDeleted) {
|
|
97
|
-
console.warn(
|
|
98
|
-
"[xform] 对已删除的 Model 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
99
|
-
);
|
|
100
87
|
return;
|
|
101
88
|
}
|
|
102
89
|
if (this._modelType === ModelType.subModel && this.values == null) {
|
|
@@ -159,10 +146,7 @@ class FormModel {
|
|
|
159
146
|
this._valueShape = valueShape;
|
|
160
147
|
this._subModels = valueShape === "object" ? {} : [];
|
|
161
148
|
} else {
|
|
162
|
-
invariant(
|
|
163
|
-
this._valueShape === valueShape,
|
|
164
|
-
"[xform] FormModel 的结构需要在使用过程中保持一致,一个数据索引对应的结构不能从数组变为对象,也不能从对象变为数组"
|
|
165
|
-
);
|
|
149
|
+
invariant(this._valueShape === valueShape, "[xform] FormModel 的结构需要在使用过程中保持一致,一个数据索引对应的结构不能从数组变为对象,也不能从对象变为数组");
|
|
166
150
|
}
|
|
167
151
|
}
|
|
168
152
|
/** 递归前序遍历该 model 下所有的 model 对象(包含 model 本身) */
|
|
@@ -244,7 +228,6 @@ class Check {
|
|
|
244
228
|
}
|
|
245
229
|
_track(config) {
|
|
246
230
|
if (this.isMounted) {
|
|
247
|
-
console.warn(`[xform] check \`${this.path.join(".")}\` 已在视图中被加载。`);
|
|
248
231
|
return;
|
|
249
232
|
}
|
|
250
233
|
this.config = config;
|
|
@@ -259,7 +242,9 @@ class Check {
|
|
|
259
242
|
if (!this.isMounted) {
|
|
260
243
|
return;
|
|
261
244
|
}
|
|
262
|
-
const {
|
|
245
|
+
const {
|
|
246
|
+
validate
|
|
247
|
+
} = this.config;
|
|
263
248
|
let cancelled = false;
|
|
264
249
|
(_a = this.cancelValidation) == null ? void 0 : _a.call(this);
|
|
265
250
|
this.validating = true;
|
|
@@ -299,9 +284,6 @@ const _Field = class _Field {
|
|
|
299
284
|
this.handleChange = (nextValue, ...rest) => {
|
|
300
285
|
var _a, _b, _c;
|
|
301
286
|
if (nextValue === void 0 && ((_a = this.config) == null ? void 0 : _a.defaultValue) !== void 0) {
|
|
302
|
-
console.warn(
|
|
303
|
-
"[xform] xform 中所有组件均为受控用法,不支持 onChange(undefined),该调用将自动变为 onChange(null)"
|
|
304
|
-
);
|
|
305
287
|
nextValue = null;
|
|
306
288
|
}
|
|
307
289
|
this.value = nextValue;
|
|
@@ -339,22 +321,18 @@ const _Field = class _Field {
|
|
|
339
321
|
}
|
|
340
322
|
const name = this.name;
|
|
341
323
|
const forkName = this._forkName;
|
|
342
|
-
makeObservable(
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
{
|
|
355
|
-
name: `${this.id}(${name}${forkName === _Field.ORIGINAL ? "" : "#" + forkName})`
|
|
356
|
-
}
|
|
357
|
-
);
|
|
324
|
+
makeObservable(this, {
|
|
325
|
+
state: observable,
|
|
326
|
+
value: computed,
|
|
327
|
+
path: computed,
|
|
328
|
+
validate: action,
|
|
329
|
+
handleBlur: action,
|
|
330
|
+
handleChange: action,
|
|
331
|
+
clear: action,
|
|
332
|
+
isDeleted: computed
|
|
333
|
+
}, {
|
|
334
|
+
name: `${this.id}(${name}${forkName === _Field.ORIGINAL ? "" : "#" + forkName})`
|
|
335
|
+
});
|
|
358
336
|
if (forkName === _Field.ORIGINAL) {
|
|
359
337
|
this._forkMap = /* @__PURE__ */ new Map();
|
|
360
338
|
} else {
|
|
@@ -382,9 +360,6 @@ const _Field = class _Field {
|
|
|
382
360
|
}
|
|
383
361
|
set value(value) {
|
|
384
362
|
if (this.isDeleted) {
|
|
385
|
-
console.warn(
|
|
386
|
-
"[xform] 对已删除的 Field 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
387
|
-
);
|
|
388
363
|
return;
|
|
389
364
|
}
|
|
390
365
|
if (this.fieldType === FieldType.normal) {
|
|
@@ -393,22 +368,13 @@ const _Field = class _Field {
|
|
|
393
368
|
this._tupleParts.forEach((part, index) => {
|
|
394
369
|
this.parent.setValue(part, value == null ? value : value[index]);
|
|
395
370
|
});
|
|
396
|
-
} else if (this.fieldType === FieldType.readonly)
|
|
397
|
-
console.warn(
|
|
398
|
-
"[xform] 对只读 Field 进行赋值将被忽略,请检查是否为 FormItem 设置了 props.name 或 props.field."
|
|
399
|
-
);
|
|
400
|
-
}
|
|
371
|
+
} else if (this.fieldType === FieldType.readonly) ;
|
|
401
372
|
}
|
|
402
373
|
get path() {
|
|
403
374
|
return this.parent.path.concat([this.name]);
|
|
404
375
|
}
|
|
405
376
|
_track(config) {
|
|
406
377
|
if (this.isMounted) {
|
|
407
|
-
console.warn(
|
|
408
|
-
`[xform] field \`${this.path.join(
|
|
409
|
-
"."
|
|
410
|
-
)}\` 已在视图中被加载,你需要 fork 该字段来进行多次加载.`
|
|
411
|
-
);
|
|
412
378
|
return;
|
|
413
379
|
}
|
|
414
380
|
this.config = config;
|
|
@@ -422,9 +388,16 @@ const _Field = class _Field {
|
|
|
422
388
|
if (this._forkMap.has(forkName)) {
|
|
423
389
|
return this._forkMap.get(forkName);
|
|
424
390
|
} else {
|
|
425
|
-
const common = {
|
|
391
|
+
const common = {
|
|
392
|
+
parent: this.parent,
|
|
393
|
+
name: this.name,
|
|
394
|
+
forkName
|
|
395
|
+
};
|
|
426
396
|
if (this.fieldType === FieldType.normal) {
|
|
427
|
-
return new _Field({
|
|
397
|
+
return new _Field({
|
|
398
|
+
fieldType: FieldType.normal,
|
|
399
|
+
...common
|
|
400
|
+
});
|
|
428
401
|
} else if (this.fieldType === FieldType.tuple) {
|
|
429
402
|
return new _Field({
|
|
430
403
|
fieldType: FieldType.tuple,
|
|
@@ -32,7 +32,12 @@ const withValueChangeHandler = (Component) => {
|
|
|
32
32
|
};
|
|
33
33
|
const withDayjsTransformAntdDate = (Component) => {
|
|
34
34
|
return (props) => {
|
|
35
|
-
const {
|
|
35
|
+
const {
|
|
36
|
+
value,
|
|
37
|
+
onChange,
|
|
38
|
+
picker,
|
|
39
|
+
...restProps
|
|
40
|
+
} = props;
|
|
36
41
|
const parsedValue = useMemo(() => {
|
|
37
42
|
if (!value) return void 0;
|
|
38
43
|
if (picker === "week") {
|
|
@@ -52,36 +57,39 @@ const withDayjsTransformAntdDate = (Component) => {
|
|
|
52
57
|
}
|
|
53
58
|
return void 0;
|
|
54
59
|
}, [value, picker]);
|
|
55
|
-
const handleChange = useCallback(
|
|
56
|
-
(dayjsDate
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
},
|
|
77
|
-
[onChange, picker]
|
|
78
|
-
);
|
|
60
|
+
const handleChange = useCallback((dayjsDate, dateString) => {
|
|
61
|
+
if (!dayjsDate || !dayjsDate.isValid()) {
|
|
62
|
+
onChange == null ? void 0 : onChange(null);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
let formattedValue;
|
|
66
|
+
if (picker === "week") {
|
|
67
|
+
const year = dayjsDate.year();
|
|
68
|
+
const week = dayjsDate.week();
|
|
69
|
+
formattedValue = `${year}-${week}`;
|
|
70
|
+
} else if (picker === "month") {
|
|
71
|
+
formattedValue = dayjsDate.format("YYYY-MM");
|
|
72
|
+
} else if (picker === "quarter") {
|
|
73
|
+
formattedValue = dayjsDate.format("YYYY-Q");
|
|
74
|
+
} else if (picker === "year") {
|
|
75
|
+
formattedValue = dayjsDate.format("YYYY");
|
|
76
|
+
} else {
|
|
77
|
+
formattedValue = dayjsDate.format("YYYY-MM-DD");
|
|
78
|
+
}
|
|
79
|
+
onChange == null ? void 0 : onChange(formattedValue);
|
|
80
|
+
}, [onChange, picker]);
|
|
79
81
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...restProps, picker, value: parsedValue, onChange: handleChange });
|
|
80
82
|
};
|
|
81
83
|
};
|
|
82
84
|
const withDayjsTransformAntdDateRangePicker = (Component) => {
|
|
83
85
|
return (props) => {
|
|
84
|
-
const {
|
|
86
|
+
const {
|
|
87
|
+
value,
|
|
88
|
+
onChange,
|
|
89
|
+
format,
|
|
90
|
+
picker = "date",
|
|
91
|
+
...restProps
|
|
92
|
+
} = props;
|
|
85
93
|
const resolvedFormat = format || DEFAULT_FORMATS[picker] || DEFAULT_FORMATS.date;
|
|
86
94
|
const parsedValue = useMemo(() => {
|
|
87
95
|
if (isEmptyDateValue(value)) {
|
|
@@ -130,57 +138,49 @@ const withDayjsTransformAntdDateRangePicker = (Component) => {
|
|
|
130
138
|
}
|
|
131
139
|
return [isValidStart ? start : null, isValidEnd ? end : null];
|
|
132
140
|
}, [value, picker, resolvedFormat]);
|
|
133
|
-
const handleChange = useCallback(
|
|
134
|
-
(dates
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return `${year}-Q${quarter}`;
|
|
151
|
-
}
|
|
152
|
-
case "year":
|
|
153
|
-
return dayjsObj.format("YYYY");
|
|
154
|
-
case "date":
|
|
155
|
-
default:
|
|
156
|
-
return dayjsObj.format(resolvedFormat);
|
|
141
|
+
const handleChange = useCallback((dates, dateStrings) => {
|
|
142
|
+
if (!dates || !dates[0] && !dates[1]) {
|
|
143
|
+
onChange == null ? void 0 : onChange(null);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const [start, end] = dates;
|
|
147
|
+
const formatValue = (dayjsObj, type) => {
|
|
148
|
+
if (!dayjsObj || !dayjsObj.isValid()) return "";
|
|
149
|
+
switch (type) {
|
|
150
|
+
case "week":
|
|
151
|
+
return `${dayjsObj.year()}-W${dayjsObj.week()}`;
|
|
152
|
+
case "month":
|
|
153
|
+
return dayjsObj.format("YYYY-MM");
|
|
154
|
+
case "quarter": {
|
|
155
|
+
const year = dayjsObj.year();
|
|
156
|
+
const quarter = Math.floor(dayjsObj.month() / 3) + 1;
|
|
157
|
+
return `${year}-Q${quarter}`;
|
|
157
158
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
onChange == null ? void 0 : onChange(formatted);
|
|
159
|
+
case "year":
|
|
160
|
+
return dayjsObj.format("YYYY");
|
|
161
|
+
case "date":
|
|
162
|
+
default:
|
|
163
|
+
return dayjsObj.format(resolvedFormat);
|
|
164
164
|
}
|
|
165
|
-
}
|
|
166
|
-
[
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
...restProps,
|
|
172
|
-
picker,
|
|
173
|
-
format: resolvedFormat,
|
|
174
|
-
value: parsedValue,
|
|
175
|
-
onChange: handleChange,
|
|
176
|
-
allowClear: restProps.allowClear ?? true
|
|
165
|
+
};
|
|
166
|
+
const formatted = [formatValue(start, picker), formatValue(end, picker)];
|
|
167
|
+
if (!formatted[0] && !formatted[1]) {
|
|
168
|
+
onChange == null ? void 0 : onChange(null);
|
|
169
|
+
} else {
|
|
170
|
+
onChange == null ? void 0 : onChange(formatted);
|
|
177
171
|
}
|
|
178
|
-
);
|
|
172
|
+
}, [onChange, picker, resolvedFormat]);
|
|
173
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...restProps, picker, format: resolvedFormat, value: parsedValue, onChange: handleChange, allowClear: restProps.allowClear ?? true });
|
|
179
174
|
};
|
|
180
175
|
};
|
|
181
176
|
const withDayjsTransformAntdTime = (Component) => {
|
|
182
177
|
return (props) => {
|
|
183
|
-
const {
|
|
178
|
+
const {
|
|
179
|
+
value,
|
|
180
|
+
onChange,
|
|
181
|
+
format = DEFAULT_FORMAT_TIME,
|
|
182
|
+
...restProps
|
|
183
|
+
} = props;
|
|
184
184
|
const parsedValue = useMemo(() => {
|
|
185
185
|
if (!value) return void 0;
|
|
186
186
|
const stringValue = String(value).trim();
|
|
@@ -188,23 +188,25 @@ const withDayjsTransformAntdTime = (Component) => {
|
|
|
188
188
|
const parsed = dayjs(stringValue, format, true);
|
|
189
189
|
return parsed.isValid() ? parsed : void 0;
|
|
190
190
|
}, [value, format]);
|
|
191
|
-
const handleChange = useCallback(
|
|
192
|
-
(time
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
},
|
|
200
|
-
[onChange, format]
|
|
201
|
-
);
|
|
191
|
+
const handleChange = useCallback((time, timeString) => {
|
|
192
|
+
if (!time || !time.isValid()) {
|
|
193
|
+
onChange == null ? void 0 : onChange(null);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const formatted = time.format(format);
|
|
197
|
+
onChange == null ? void 0 : onChange(formatted);
|
|
198
|
+
}, [onChange, format]);
|
|
202
199
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...restProps, format, value: parsedValue, onChange: handleChange });
|
|
203
200
|
};
|
|
204
201
|
};
|
|
205
202
|
const withDayjsTransformAntdTimeRange = (Component) => {
|
|
206
203
|
return (props) => {
|
|
207
|
-
const {
|
|
204
|
+
const {
|
|
205
|
+
value,
|
|
206
|
+
onChange,
|
|
207
|
+
format = DEFAULT_FORMAT_TIME,
|
|
208
|
+
...restProps
|
|
209
|
+
} = props;
|
|
208
210
|
const parsedValue = useMemo(() => {
|
|
209
211
|
if (!value || !Array.isArray(value)) return void 0;
|
|
210
212
|
const [startStr, endStr] = value;
|
|
@@ -216,32 +218,29 @@ const withDayjsTransformAntdTimeRange = (Component) => {
|
|
|
216
218
|
if (!isValidStart && !isValidEnd) return void 0;
|
|
217
219
|
return [isValidStart ? start : null, isValidEnd ? end : null];
|
|
218
220
|
}, [value, format]);
|
|
219
|
-
const handleChange = useCallback(
|
|
220
|
-
(times
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
} else {
|
|
233
|
-
onChange == null ? void 0 : onChange(formatted);
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
[onChange, format]
|
|
237
|
-
);
|
|
221
|
+
const handleChange = useCallback((times, timeStrings) => {
|
|
222
|
+
if (!times) {
|
|
223
|
+
onChange == null ? void 0 : onChange(null);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const [start, end] = times;
|
|
227
|
+
const formatted = [(start == null ? void 0 : start.isValid()) ? start.format(format) : "", (end == null ? void 0 : end.isValid()) ? end.format(format) : ""];
|
|
228
|
+
if (!formatted[0] && !formatted[1]) {
|
|
229
|
+
onChange == null ? void 0 : onChange(null);
|
|
230
|
+
} else {
|
|
231
|
+
onChange == null ? void 0 : onChange(formatted);
|
|
232
|
+
}
|
|
233
|
+
}, [onChange, format]);
|
|
238
234
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...restProps, format, value: parsedValue, onChange: handleChange });
|
|
239
235
|
};
|
|
240
236
|
};
|
|
241
237
|
const withInjectedProps = (injectedProps) => {
|
|
242
238
|
return (Component) => {
|
|
243
239
|
const WrappedComponent = React.forwardRef((props, ref) => {
|
|
244
|
-
const mergedProps = {
|
|
240
|
+
const mergedProps = {
|
|
241
|
+
...injectedProps,
|
|
242
|
+
...props
|
|
243
|
+
};
|
|
245
244
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ref, ...mergedProps });
|
|
246
245
|
});
|
|
247
246
|
WrappedComponent.displayName = `WithInjectedProps(${Component.displayName || Component.name})`;
|
|
@@ -140,7 +140,9 @@ const ALL_COMPONENTS = Object.keys(COMPONENT_MAP).map((name) => {
|
|
|
140
140
|
component = withDayjsTransformAntdTimeRange(component);
|
|
141
141
|
break;
|
|
142
142
|
case "multiSelect":
|
|
143
|
-
component = withInjectedProps({
|
|
143
|
+
component = withInjectedProps({
|
|
144
|
+
mode: "multiple"
|
|
145
|
+
})(component);
|
|
144
146
|
break;
|
|
145
147
|
case "colorPicker":
|
|
146
148
|
component = withColorPickerHandler(component);
|
|
@@ -28,19 +28,14 @@ const useRatePreview = (value) => {
|
|
|
28
28
|
};
|
|
29
29
|
const useColorPreview = (value) => {
|
|
30
30
|
if (!value) return "--";
|
|
31
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
backgroundColor: value,
|
|
40
|
-
borderRadius: 3
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
);
|
|
31
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx("span", { style: {
|
|
32
|
+
display: "inline-block",
|
|
33
|
+
width: 16,
|
|
34
|
+
height: 16,
|
|
35
|
+
border: "1px solid #ddd",
|
|
36
|
+
backgroundColor: value,
|
|
37
|
+
borderRadius: 3
|
|
38
|
+
} });
|
|
44
39
|
};
|
|
45
40
|
export {
|
|
46
41
|
useBooleanPreview,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-antd-xform",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "基于mobx的form解决方案",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -45,7 +45,9 @@
|
|
|
45
45
|
"@types/react-dom": "^18.2.22",
|
|
46
46
|
"@types/react-syntax-highlighter": "^15.5.13",
|
|
47
47
|
"@vitejs/plugin-react": "^4.6.0",
|
|
48
|
+
"babel-plugin-transform-remove-console": "^6.9.4",
|
|
48
49
|
"dumi": "^2.4.21",
|
|
50
|
+
"dumi-theme-antd": "^0.4.4",
|
|
49
51
|
"react": "^18.2.0",
|
|
50
52
|
"react-dom": "^18.2.0",
|
|
51
53
|
"react-json-view": "^1.21.3",
|