fix(form): date相关的field,在没有配置valueFormat的情况下与默认的valueFormat不符

This commit is contained in:
roymondchen 2024-08-30 17:35:04 +08:00
parent 45d246abce
commit 883e227d54
7 changed files with 24 additions and 11 deletions

View File

@ -5,8 +5,8 @@
:size="size"
:placeholder="config.placeholder"
:disabled="disabled"
:format="config.format"
:value-format="config.valueFormat || 'YYYY/MM/DD HH:mm:ss'"
:format="config.format || 'YYYY/MM/DD'"
:value-format="config.valueFormat || 'YYYY/MM/DD'"
@change="changeHandler"
></TMagicDatePicker>
</template>
@ -30,7 +30,7 @@ const emit = defineEmits<{
useAddField(props.prop);
props.model[props.name] = datetimeFormatter(props.model[props.name], '');
props.model[props.name] = datetimeFormatter(props.model[props.name], '', 'YYYY/MM/DD');
const changeHandler = (v: string) => {
emit('change', v);

View File

@ -37,7 +37,11 @@ if (props.model) {
if (value === 'Invalid Date') {
props.model[props.name] = '';
} else {
props.model[props.name] = datetimeFormatter(props.model[props.name], '', props.config.valueFormat);
props.model[props.name] = datetimeFormatter(
props.model[props.name],
'',
props.config.valueFormat || 'YYYY/MM/DD HH:mm:ss',
);
}
}

View File

@ -9,6 +9,9 @@
:unlink-panels="true"
:disabled="disabled"
:default-time="config.defaultTime"
:value-format="config.valueFormat || 'YYYY/MM/DD HH:mm:ss'"
:date-format="config.dateFormat || 'YYYY/MM/DD'"
:time-format="config.timeFormat || 'HH:mm:ss'"
@change="changeHandler"
></TMagicDatePicker>
</template>
@ -71,7 +74,11 @@ const setValue = (v: Date[] | Date) => {
return;
}
if (Array.isArray(v)) {
props.model[item] = datetimeFormatter(v[index]?.toString(), '');
props.model[item] = datetimeFormatter(
v[index]?.toString(),
'',
props.config.valueFormat || 'YYYY/MM/DD HH:mm:ss',
);
} else {
props.model[item] = undefined;
}
@ -85,7 +92,7 @@ const changeHandler = (v: Date[]) => {
emit(
'change',
value.map((item?: Date) => {
if (item) return datetimeFormatter(item, '');
if (item) return datetimeFormatter(item, '', props.config.valueFormat || 'YYYY/MM/DD HH:mm:ss');
return undefined;
}),
);

View File

@ -17,7 +17,6 @@
import { ref, watch } from 'vue';
import { TMagicTimePicker } from '@tmagic/design';
import { datetimeFormatter } from '@tmagic/utils';
import type { DaterangeConfig, FieldProps } from '../schema';
import { useAddField } from '../utils/useAddField';
@ -59,7 +58,7 @@ const setValue = (v: Date[] | Date) => {
return;
}
if (Array.isArray(v)) {
props.model[item] = datetimeFormatter(v[index]?.toString(), '');
props.model[item] = v[index];
} else {
props.model[item] = undefined;
}

View File

@ -296,6 +296,9 @@ export interface DaterangeConfig extends FormItem {
type: 'daterange';
defaultTime?: Date[];
names?: string[];
valueFormat?: string;
dateFormat?: string;
timeFormat?: string;
}
/**

View File

@ -53,7 +53,7 @@ describe('Date', () => {
expect(date.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm();
expect(value.date).toMatch('2021-01-01 00:00:00');
expect(value.date).toMatch('2021/01/01');
});
test('输入', async () => {
@ -74,7 +74,7 @@ describe('Date', () => {
await input.trigger('blur');
const value = await (wrapper.vm as any).submitForm();
expect(input.element.value).toMatch('2021-07-28');
expect(input.element.value).toMatch('2021/07/28');
expect(value.date).toMatch('2021-07-28');
});

View File

@ -53,7 +53,7 @@ describe('DateTime', () => {
expect(datetime.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm();
expect(value.datetime).toMatch('2021-01-01 12:00:00');
expect(value.datetime).toMatch('2021/01/01 12:00:00');
});
test('错误类型初始化初始化', async () => {