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

View File

@ -37,7 +37,11 @@ if (props.model) {
if (value === 'Invalid Date') { if (value === 'Invalid Date') {
props.model[props.name] = ''; props.model[props.name] = '';
} else { } 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" :unlink-panels="true"
:disabled="disabled" :disabled="disabled"
:default-time="config.defaultTime" :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" @change="changeHandler"
></TMagicDatePicker> ></TMagicDatePicker>
</template> </template>
@ -71,7 +74,11 @@ const setValue = (v: Date[] | Date) => {
return; return;
} }
if (Array.isArray(v)) { 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 { } else {
props.model[item] = undefined; props.model[item] = undefined;
} }
@ -85,7 +92,7 @@ const changeHandler = (v: Date[]) => {
emit( emit(
'change', 'change',
value.map((item?: Date) => { 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; return undefined;
}), }),
); );

View File

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

View File

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

View File

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

View File

@ -53,7 +53,7 @@ describe('DateTime', () => {
expect(datetime.exists()).toBe(true); expect(datetime.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm(); 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 () => { test('错误类型初始化初始化', async () => {