From 8c7a2650825798e231c3e1827b80d22a78ee1144 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 26 Jan 2021 21:10:54 +0800 Subject: [PATCH] perf(ContactEdit): using Form (#8015) --- src/contact-edit/index.js | 103 ++++++------------ .../test/__snapshots__/demo.spec.js.snap | 6 +- src/contact-edit/test/index.spec.js | 37 ++----- 3 files changed, 49 insertions(+), 97 deletions(-) diff --git a/src/contact-edit/index.js b/src/contact-edit/index.js index 3be35d7f5..c444387c2 100644 --- a/src/contact-edit/index.js +++ b/src/contact-edit/index.js @@ -6,6 +6,7 @@ import { isMobile } from '../utils/validate/mobile'; // Components import Cell from '../cell'; +import Form from '../form'; import Field from '../field'; import Button from '../button'; import Dialog from '../dialog'; @@ -38,42 +39,14 @@ export default createComponent({ emits: ['save', 'delete', 'change-default'], setup(props, { emit }) { - const state = reactive({ - contact: { - ...DEFAULT_CONTACT, - ...props.contactInfo, - }, - errorInfo: { - name: '', - tel: '', - }, + const contact = reactive({ + ...DEFAULT_CONTACT, + ...props.contactInfo, }); - const onFocus = (key) => { - state.errorInfo[key] = ''; - }; - - const getErrorMessageByKey = (key) => { - const value = state.contact[key].trim(); - switch (key) { - case 'name': - return value ? '' : t('nameInvalid'); - case 'tel': - return props.telValidator(value) ? '' : t('telInvalid'); - } - }; - const onSave = () => { - const isValid = ['name', 'tel'].every((item) => { - const msg = getErrorMessageByKey(item); - if (msg) { - state.errorInfo[item] = msg; - } - return !msg; - }); - - if (isValid && !props.isSaving) { - emit('save', state.contact); + if (!props.isSaving) { + emit('save', contact); } }; @@ -81,7 +54,7 @@ export default createComponent({ Dialog.confirm({ title: t('confirmDelete'), }).then(() => { - emit('delete', state.contact); + emit('delete', contact); }); }; @@ -93,7 +66,7 @@ export default createComponent({ type="danger" text={t('save')} loading={props.isSaving} - onClick={onSave} + nativeType="submit" /> {props.isEdit && ( - + `; diff --git a/src/contact-edit/test/index.spec.js b/src/contact-edit/test/index.spec.js index 498ebfdfd..09850f56b 100644 --- a/src/contact-edit/test/index.spec.js +++ b/src/contact-edit/test/index.spec.js @@ -6,6 +6,12 @@ const contactInfo = { tel: '13000000000', }; +async function submitForm(wrapper) { + const form = wrapper.find('form'); + await form.trigger('submit'); + return later(); +} + test('should validate contact name before submitting form', async () => { const wrapper = mount(ContactEdit, { props: { @@ -16,16 +22,8 @@ test('should validate contact name before submitting form', async () => { }, }); - const button = wrapper.find('.van-button'); - button.trigger('click'); - await later(); + await submitForm(wrapper); expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot(); - - const fields = wrapper.findAll('.van-field__control'); - - fields[0].trigger('focus'); - await later(); - expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy(); }); test('should validate contact tel before submitting form', async () => { @@ -38,16 +36,8 @@ test('should validate contact tel before submitting form', async () => { }, }); - const button = wrapper.find('.van-button'); - button.trigger('click'); - await later(); + await submitForm(wrapper); expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot(); - - const fields = wrapper.findAll('.van-field__control'); - - fields[1].trigger('focus'); - await later(); - expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy(); }); test('should emit save event after submitting form', async () => { @@ -57,19 +47,14 @@ test('should emit save event after submitting form', async () => { }, }); - const button = wrapper.find('.van-button'); - button.trigger('click'); - await later(); + await submitForm(wrapper); expect(wrapper.emitted('save')[0][0]).toEqual(contactInfo); }); test('should watch contact info', async () => { const wrapper = mount(ContactEdit); - const button = wrapper.find('.van-button'); - - wrapper.setProps({ contactInfo }); - await later(); - button.trigger('click'); + await wrapper.setProps({ contactInfo }); + await submitForm(wrapper); expect(wrapper.emitted('save')[0][0]).toEqual(contactInfo); });