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(Picker): correct v-model when emit confirm event #11194

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/vant/src/date-picker/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('should emit confirm event correctly', async () => {
});

await later();
wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted<[PickerConfirmEventParams]>('confirm')![0][0]).toEqual(
{
selectedOptions: [
Expand Down Expand Up @@ -90,7 +90,7 @@ test('should render with max-date correctly', async () => {
});

await later();
wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(
wrapper.emitted<[PickerConfirmEventParams]>('confirm')![0][0].selectedValues
).toEqual(['2010', '01', '10']);
Expand All @@ -115,7 +115,7 @@ test('should render with min-date correctly', async () => {
});

await later();
wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(
wrapper.emitted<[PickerConfirmEventParams]>('confirm')![0][0].selectedValues
).toEqual(['2000', '10', '10']);
Expand Down Expand Up @@ -193,7 +193,7 @@ test('should update value correctly when dynamically change min-date', async ()
minDate: new Date(2020, 11, 20),
});

wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(
wrapper.emitted<[PickerConfirmEventParams]>('confirm')![0][0].selectedValues
).toEqual(['2020', '12', '20']);
Expand Down
9 changes: 8 additions & 1 deletion packages/vant/src/picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ref,
watch,
computed,
nextTick,
defineComponent,
type PropType,
type ExtractPropTypes,
Expand Down Expand Up @@ -162,7 +163,13 @@ export default defineComponent({
const confirm = () => {
children.forEach((child) => child.stopMomentum());
const params = getEventParams();
emit('confirm', params);

// wait nextTick to ensure the model value is update to date
// when confirm event is emitted
nextTick(() => {
emit('confirm', params);
});

return params;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/vant/src/picker/test/cascade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ const cascadeColumns = [
},
];

test('should emit confirm event for cascade picker correctly', () => {
test('should emit confirm event for cascade picker correctly', async () => {
const wrapper = mount(Picker, {
props: {
columns: cascadeColumns,
},
});

wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');

const params = wrapper.emitted<PickerConfirmEventParams[]>('confirm')?.[0];
expect(params?.[0].selectedValues).toEqual(['A1', 'B1', 'C1']);
Expand Down
6 changes: 3 additions & 3 deletions packages/vant/src/picker/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const simpleColumn = [
{ text: '1995', value: '1995' },
];

test('should emit confirm event after clicking the confirm button', () => {
test('should emit confirm event after clicking the confirm button', async () => {
const wrapper = mount(Picker, {
props: {
showToolbar: true,
columns: simpleColumn,
},
});

wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted('confirm')![0]).toEqual([
{
selectedOptions: [{ text: '1990', value: '1990' }],
Expand Down Expand Up @@ -178,7 +178,7 @@ test('should allow to update columns props dynamically', async () => {
],
});

wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted<[string, number]>('confirm')![0]).toEqual([
{ selectedOptions: [{ text: '2', value: '2' }], selectedValues: ['2'] },
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/vant/src/time-picker/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ test('should format options correctly when using formatter prop', async () => {
expect(wrapper.html()).toMatchSnapshot();
});

test('should emit confirm event after clicking the confirm button', () => {
test('should emit confirm event after clicking the confirm button', async () => {
const wrapper = mount(TimePicker, {
props: {
modelValue: ['12', '00'],
},
});

wrapper.find('.van-picker__confirm').trigger('click');
await wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted('confirm')).toEqual([
[
{
Expand Down