fix(form): daterange 配置names后配置失效

This commit is contained in:
roymondchen 2025-10-28 15:29:50 +08:00
parent 12e6dd18b4
commit 0f3dfcf511
4 changed files with 41 additions and 26 deletions

View File

@ -208,10 +208,7 @@ const items = computed(() => (props.config as ContainerCommonConfig).items);
const itemProp = computed(() => {
let n: string | number = '';
const { names } = props.config as any;
if (names?.[0]) {
[n] = names;
} else if (name.value) {
if (name.value) {
n = name.value;
} else {
return props.prop;

View File

@ -9,6 +9,7 @@
:unlink-panels="true"
:disabled="disabled"
:default-time="config.defaultTime"
:format="`${config.dateFormat || 'YYYY/MM/DD'} ${config.timeFormat || 'HH:mm:ss'}`"
:value-format="config.valueFormat || 'YYYY/MM/DD HH:mm:ss'"
:date-format="config.dateFormat || 'YYYY/MM/DD'"
:time-format="config.timeFormat || 'HH:mm:ss'"
@ -17,11 +18,11 @@
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import { onUnmounted, ref, watch } from 'vue';
import { TMagicDatePicker } from '@tmagic/design';
import type { DaterangeConfig, FieldProps } from '../schema';
import type { ChangeRecord, DaterangeConfig, FieldProps } from '../schema';
import { datetimeFormatter } from '../utils/form';
import { useAddField } from '../utils/useAddField';
@ -40,7 +41,7 @@ const value = ref<(Date | string | undefined)[] | null>([]);
if (props.model !== undefined) {
if (names?.length) {
watch(
const unWatch = watch(
[() => props.model[names[0]], () => props.model[names[1]]],
([start, end], [preStart, preEnd]) => {
if (!value.value) {
@ -56,8 +57,12 @@ if (props.model !== undefined) {
immediate: true,
},
);
onUnmounted(() => {
unWatch();
});
} else if (props.name && props.model[props.name]) {
watch(
const unWatch = watch(
() => props.model[props.name],
(start, preStart) => {
const format = `${props.config.dateFormat || 'YYYY/MM/DD'} ${props.config.timeFormat || 'HH:mm:ss'}`;
@ -71,32 +76,41 @@ if (props.model !== undefined) {
immediate: true,
},
);
onUnmounted(() => {
unWatch();
});
}
}
const setValue = (v: Date[] | Date) => {
names?.forEach((item, index) => {
if (!props.model) {
return;
}
if (Array.isArray(v)) {
props.model[item] = v[index];
} else {
props.model[item] = undefined;
}
});
};
const changeHandler = (v: Date[]) => {
const value = v || [];
if (props.name) {
emit('change', value);
} else {
if (names?.length) {
setValue(value);
if (props.config.names?.length) {
const newChangeRecords: ChangeRecord[] = [];
props.config.names.forEach((item, index) => {
if (!props.model) {
return;
}
if (Array.isArray(v)) {
newChangeRecords.push({
propPath: props.prop ? `${props.prop}.${item}` : item,
value: v[index],
});
} else {
newChangeRecords.push({
propPath: props.prop ? `${props.prop}.${item}` : item,
value: undefined,
});
}
});
emit('change', props.model, {
changeRecords: newChangeRecords,
});
}
emit('change', props.model);
}
};
</script>

View File

@ -1,6 +1,9 @@
<template>
<TDateRangePicker
v-if="type.endsWith('range')"
allow-input
clearable
enable-time-picker
:modelValue="modelValue"
:mode="mode"
:placeholder="[startPlaceholder || '', endPlaceholder || '']"
@ -14,6 +17,7 @@
/>
<TDatePicker
v-else
clearable
:modelValue="modelValue"
:mode="mode"
:placeholder="placeholder"

View File

@ -56,8 +56,8 @@ export default createForm([
{
type: 'daterange',
text: '日期范围',
name: 'daterange',
// names: ['one', 'two'],
// name: 'daterange',
names: ['one', 'two'],
},
{
type: 'time',