Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TimePicker & DatePicker): fix the 'formatValueRange' function defect #13094

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/vant/src/date-picker/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
},
]);
});
11 changes: 9 additions & 2 deletions packages/vant/src/date-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ export const genOptions = <T extends string>(
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!;
Expand All @@ -76,3 +82,4 @@ export const formatValueRange = (values: string[], columns: PickerOption[][]) =>
}
return value;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
<div class="van-ellipsis">
00
</div>
</li>
<li
role="button"
style="height:44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
10
</div>
</li>
<li
role="button"
style="height:44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
20
</div>
</li>
<li
role="button"
style="height:44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
30
</div>
</li>
<li
role="button"
style="height:44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
40
Expand Down
33 changes: 33 additions & 0 deletions packages/vant/src/time-picker/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
},
]);
});