From 4abc67e4ac5aeef5080199447a92568032d7908e Mon Sep 17 00:00:00 2001 From: ShuGang Zhou Date: Sat, 23 Sep 2023 17:50:37 +0800 Subject: [PATCH] feat(AddressEdit): add change event for name and tel input (#12310) * feat(AddressEdit): add change event for name and tel input * feat(AddressEdit): add change event for name and tel input --- packages/vant/src/address-edit/AddressEdit.tsx | 7 +++++++ packages/vant/src/address-edit/README.md | 1 + packages/vant/src/address-edit/README.zh-CN.md | 1 + .../vant/src/address-edit/test/index.spec.ts | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/packages/vant/src/address-edit/AddressEdit.tsx b/packages/vant/src/address-edit/AddressEdit.tsx index 73ccb73b7..eec2212a3 100644 --- a/packages/vant/src/address-edit/AddressEdit.tsx +++ b/packages/vant/src/address-edit/AddressEdit.tsx @@ -95,6 +95,7 @@ export default defineComponent({ emits: [ 'save', 'focus', + 'change', 'delete', 'clickArea', 'changeArea', @@ -136,6 +137,10 @@ export default defineComponent({ emit('focus', key); }; + const onChange = (key: string, value: string) => { + emit('change', { key, value }); + }; + const rules = computed>(() => { const { validator, telValidator } = props; @@ -273,6 +278,7 @@ export default defineComponent({ rules={rules.value.name} placeholder={t('name')} onFocus={() => onFocus('name')} + onUpdate:modelValue={(val) => onChange('name', val)} /> onFocus('tel')} + onUpdate:modelValue={(val) => onChange('tel', val)} /> { expect(wrapper.emitted('changeDetail')).toEqual([['123']]); }); +test('should emit change event after name or tel input', () => { + const wrapper = mount(AddressEdit); + + const field = wrapper.findAll('.van-field__control')[0]; + (field.element as HTMLInputElement).value = '123'; + field.trigger('input'); + expect(wrapper.emitted('change')?.[0]).toEqual([ + { key: 'name', value: '123' }, + ]); + + const field1 = wrapper.findAll('.van-field__control')[1]; + (field1.element as HTMLInputElement).value = '123'; + field1.trigger('input'); + expect(wrapper.emitted('change')?.[1]).toEqual([ + { key: 'tel', value: '123' }, + ]); +}); + test('should show search result after focusing to address detail', async () => { const wrapper = mount(AddressEdit, { props: {