diff --git a/packages/vant/src/date-picker/test/index.spec.ts b/packages/vant/src/date-picker/test/index.spec.ts index 3f1b90939b6..7d6fc9ef1c1 100644 --- a/packages/vant/src/date-picker/test/index.spec.ts +++ b/packages/vant/src/date-picker/test/index.spec.ts @@ -227,3 +227,38 @@ test('should be displayed correctly when modelValue updated by external sources' .selectedValues, ).toEqual(['2024', '01']); }); + +test('update modelValue to undefined', async () => { + const wrapper = mount(DatePicker, { + props: { + modelValue: ['2024', '10', '10'], + }, + }); + + await wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')?.[0]).toEqual([ + { + selectedOptions: [ + { text: '2024', value: '2024' }, + { text: '10', value: '10' }, + { text: '10', value: '10' }, + ], + selectedValues: ['2024', '10', '10'], + selectedIndexes: [10, 9, 9], + }, + ]); + + await wrapper.setProps({ modelValue: undefined }); + await wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')?.[1]).toEqual([ + { + selectedOptions: [ + { text: '2014', value: '2014' }, + { text: '01', value: '01' }, + { text: '01', value: '01' }, + ], + selectedValues: ['2014', '01', '01'], + selectedIndexes: [0, 0, 0], + }, + ]); +}); diff --git a/packages/vant/src/date-picker/utils.ts b/packages/vant/src/date-picker/utils.ts index f25337d386e..c653175c08b 100644 --- a/packages/vant/src/date-picker/utils.ts +++ b/packages/vant/src/date-picker/utils.ts @@ -66,8 +66,14 @@ export const genOptions = ( return filter ? filter(type, options, values!) : options; }; -export const formatValueRange = (values: string[], columns: PickerOption[][]) => - values.map((value, index) => { +export const formatValueRange = ( + values: string[], + columns: PickerOption[][], +) => { + if (values.length === 0) { + return columns.map((column) => column[0]?.value?.toString() ?? ''); + } + return values.map((value, index) => { const column = columns[index]; if (column.length) { const minValue = +column[0].value!; @@ -76,3 +82,4 @@ export const formatValueRange = (values: string[], columns: PickerOption[][]) => } return value; }); +}; diff --git a/packages/vant/src/time-picker/test/__snapshots__/demo-ssr.spec.ts.snap b/packages/vant/src/time-picker/test/__snapshots__/demo-ssr.spec.ts.snap index 76765e2c770..932e19eea98 100644 --- a/packages/vant/src/time-picker/test/__snapshots__/demo-ssr.spec.ts.snap +++ b/packages/vant/src/time-picker/test/__snapshots__/demo-ssr.spec.ts.snap @@ -5469,46 +5469,6 @@ exports[`should render demo and match snapshot 1`] = ` style="height:44px;" tabindex="0" class="van-picker-column__item van-picker-column__item--selected" - > -
- 00 -
- -
  • -
    - 10 -
    -
  • -
  • -
    - 20 -
    -
  • -
  • -
    - 30 -
    -
  • -
  • 40 diff --git a/packages/vant/src/time-picker/test/index.spec.tsx b/packages/vant/src/time-picker/test/index.spec.tsx index bbff4c48f65..fba0c8a01e8 100644 --- a/packages/vant/src/time-picker/test/index.spec.tsx +++ b/packages/vant/src/time-picker/test/index.spec.tsx @@ -317,3 +317,36 @@ test('should time range when set props max-time', async () => { }, ]); }); + +test('update modelValue to undefined', async () => { + const wrapper = mount(TimePicker, { + props: { + modelValue: ['12', '00'], + }, + }); + + await wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')?.[0]).toEqual([ + { + selectedOptions: [ + { text: '12', value: '12' }, + { text: '00', value: '00' }, + ], + selectedValues: ['12', '00'], + selectedIndexes: [12, 0], + }, + ]); + + await wrapper.setProps({ modelValue: undefined }); + await wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')?.[1]).toEqual([ + { + selectedOptions: [ + { text: '00', value: '00' }, + { text: '00', value: '00' }, + ], + selectedValues: ['00', '00'], + selectedIndexes: [0, 0], + }, + ]); +});