@peng_kai/kit 0.2.41 → 0.2.42
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/antd/hooks/useAntdForm.ts +19 -9
- package/package.json +1 -1
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import type { FormInstance, FormItemProps, RuleObject } from 'ant-design-vue/es/form';
|
|
7
7
|
import type { FormProps } from 'ant-design-vue';
|
|
8
|
-
import { type MaybeRefOrGetter, computed, reactive, ref,
|
|
9
|
-
import {
|
|
10
|
-
import { reactiveComputed } from '@vueuse/core';
|
|
8
|
+
import { type MaybeRefOrGetter, computed, reactive, ref, toRef, watch, watchEffect } from 'vue';
|
|
9
|
+
import { mapValues } from 'lodash-es';
|
|
11
10
|
import { GROUP_SEP, buildGroupField, getGroupIndex } from './useAntdForm.helpers';
|
|
12
11
|
|
|
13
12
|
export { useAntdForm };
|
|
@@ -29,7 +28,7 @@ interface Options<S, TS> {
|
|
|
29
28
|
function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRefOrGetter<SchemaConfig<S>>, options?: Options<S, TS>) {
|
|
30
29
|
const schemasR = toRef(schemas);
|
|
31
30
|
|
|
32
|
-
const state =
|
|
31
|
+
const state = ref({} as S);
|
|
33
32
|
const stateTF = computed<TS>(() => options?.transform?.(<any>state.value) ?? <any>state.value);
|
|
34
33
|
const show = computed(() => mapValues(state.value, (_, k): boolean => schemasR.value[k].show?.(state.value) ?? true));
|
|
35
34
|
const props = computed<FormProps>(() => ({ model: state.value, ref: (c: any) => (formRef.value = c) }));
|
|
@@ -50,16 +49,15 @@ function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRe
|
|
|
50
49
|
get scrollToField() {
|
|
51
50
|
return formRef.value?.scrollToField;
|
|
52
51
|
},
|
|
52
|
+
get validate() {
|
|
53
|
+
return formRef.value?.validate;
|
|
54
|
+
},
|
|
53
55
|
get validateFields() {
|
|
54
56
|
return formRef.value?.validateFields;
|
|
55
57
|
},
|
|
56
58
|
get clearValidate() {
|
|
57
59
|
return formRef.value?.clearValidate;
|
|
58
60
|
},
|
|
59
|
-
async validate(...args: Parameters<FormInstance['validate']>) {
|
|
60
|
-
const res = await formRef.value?.validate(...args);
|
|
61
|
-
return cloneDeep(reactive(res ?? {}));
|
|
62
|
-
},
|
|
63
61
|
addGroup(groupName: string, groupSchemas: Record<string, ItemSchema>) {
|
|
64
62
|
const i = getGroupIndex(groupName, schemasR.value) + 1;
|
|
65
63
|
|
|
@@ -98,5 +96,17 @@ function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRe
|
|
|
98
96
|
state.value = newState;
|
|
99
97
|
}, { flush: 'pre' });
|
|
100
98
|
|
|
101
|
-
|
|
99
|
+
// 为什么之前用 watchEffect?
|
|
100
|
+
// watch(schemasR, () => {
|
|
101
|
+
// const _state: any = {};
|
|
102
|
+
|
|
103
|
+
// for (const k in schemasR.value) {
|
|
104
|
+
// const item = schemasR.value[k];
|
|
105
|
+
// _state[k] = (k in state.value) ? toRef(state.value, k) : toRef(item, 'value');
|
|
106
|
+
// }
|
|
107
|
+
|
|
108
|
+
// state.value = _state;
|
|
109
|
+
// }, { immediate: true });
|
|
110
|
+
|
|
111
|
+
return reactive({ state, stateTF, show, props, itemProps, $form });
|
|
102
112
|
}
|