index.vue
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script lang="ts" setup>
import { BasicForm, FormSchema, useForm } from '/@/components/Form';
import { ComponentType, ColEx } from '/@/components/Form/src/types/index';
import { computed } from '@vue/reactivity';
import { isFunction } from '/@/utils/is';
import { unref } from 'vue';
import { watch } from 'vue';
import { nextTick } from 'vue';
import { ref } from 'vue';
import { onMounted } from 'vue';
interface ValueItemType {
value: any;
}
enum FormFieldsEnum {
TOTAL_CONTROL = 'totalControl',
}
enum EmitEventEnum {
UPDATE_VALUE = 'update:value',
}
const emit = defineEmits<{
(event: EmitEventEnum.UPDATE_VALUE, value: ValueItemType[]): void;
}>();
const props = withDefaults(
defineProps<{
value: ValueItemType[];
length?: number;
component?: ComponentType;
itemColProps?: Partial<ColEx>;
itemLabel?: (index: number) => string;
itemProps?: (index: number) => FormSchema;
showTotalControl?: boolean;
totalControlProps?: FormSchema;
}>(),
{
value: () => [],
length: 0,
component: 'Switch',
itemLabel: (index: number) => `#${index}`,
itemProps: () => ({} as unknown as FormSchema),
itemColProps: () => ({ span: 12 } as Partial<ColEx>),
showTotalControl: true,
totalControlProps: () => ({} as unknown as FormSchema),
}
);
const getProps = computed(() => {
return props;
});
const batchSetValue = (value: any): ValueItemType[] => {
const { length } = unref(getProps);
return Array.from({ length }, () => ({ value }));
};
const getTotalControlItem = computed(() => {
const { totalControlProps, component, showTotalControl } = unref(getProps);
return {
...totalControlProps,
field: FormFieldsEnum.TOTAL_CONTROL,
component,
ifShow: showTotalControl,
componentProps: {
onChange(value: any) {
handleUpdateValue(batchSetValue(value));
},
},
} as FormSchema;
});
const getSchemas = computed(() => {
const { itemProps, itemLabel, length, component } = unref(getProps);
let label = isFunction(itemLabel) ? itemLabel : (index: number) => `#${index}`;
let _itemProps = isFunction(itemProps) ? itemProps : () => ({});
const schemas = Array.from(
{ length },
(_item, index) =>
({
..._itemProps(index),
label: label(index),
field: index.toString(),
component,
componentProps: {
onChange: async () => {
await nextTick();
handleUpdateValue();
},
},
} as FormSchema)
);
length && schemas.unshift(unref(getTotalControlItem));
return schemas;
});
const [registerForm, { getFieldsValue, setProps, setFieldsValue }] = useForm({
showActionButtonGroup: false,
schemas: unref(getSchemas),
// baseColProps,
baseColProps: props.itemColProps,
});
const handleUpdateValue = (value?: ValueItemType[]) => {
if (value) {
emit(EmitEventEnum.UPDATE_VALUE, value);
return;
}
const allValue = getFieldsValue();
const sortKeyList = Array.from({ length: unref(getProps).length }, (_v, key) => key);
const res = sortKeyList.map((item) => ({ value: allValue[item] } as ValueItemType));
emit(EmitEventEnum.UPDATE_VALUE, res);
};
const transformValue = (value: ValueItemType[]) => {
const { length } = unref(getProps);
if (value.length !== length) {
value = Array.from(
{ length: unref(getProps).length },
() => ({ value: null } as ValueItemType)
);
}
return value.reduce((prev, next, index) => ({ ...prev, [index]: next.value }), {});
};
const initialized = ref(false);
watch(
() => props.value,
async (target) => {
if (target) {
let flag = unref(initialized);
if (!flag) {
await nextTick();
}
const value = transformValue(target);
setFieldsValue(value);
if (!flag) {
handleUpdateValue();
}
}
},
{
immediate: true,
}
);
watch(
() => [props.length, props.component],
(target) => {
if (target !== undefined || target !== null) {
setProps({
schemas: unref(getSchemas),
});
handleUpdateValue();
}
}
);
onMounted(() => {
initialized.value = true;
});
</script>
<template>
<BasicForm class="control-group-form" @register="registerForm" />
</template>
<style lang="less" scoped>
.control-group-form {
:deep(.ant-form-item-label) {
font-weight: 700;
}
}
</style>