diff --git a/src/contact-edit/index.js b/src/contact-edit/index.js index 5189b24ad..3be35d7f5 100644 --- a/src/contact-edit/index.js +++ b/src/contact-edit/index.js @@ -1,3 +1,5 @@ +import { watch, reactive } from 'vue'; + // Utils import { createNamespace } from '../utils'; import { isMobile } from '../utils/validate/mobile'; @@ -11,7 +13,7 @@ import Switch from '../switch'; const [createComponent, bem, t] = createNamespace('contact-edit'); -const defaultContact = { +const DEFAULT_CONTACT = { tel: '', name: '', }; @@ -25,7 +27,7 @@ export default createComponent({ setDefaultLabel: String, contactInfo: { type: Object, - default: () => ({ ...defaultContact }), + default: () => ({ ...DEFAULT_CONTACT }), }, telValidator: { type: Function, @@ -35,130 +37,137 @@ export default createComponent({ emits: ['save', 'delete', 'change-default'], - data() { - return { - data: { - ...defaultContact, - ...this.contactInfo, + setup(props, { emit }) { + const state = reactive({ + contact: { + ...DEFAULT_CONTACT, + ...props.contactInfo, }, errorInfo: { name: '', tel: '', }, + }); + + const onFocus = (key) => { + state.errorInfo[key] = ''; }; - }, - watch: { - contactInfo(val) { - this.data = { - ...defaultContact, - ...val, - }; - }, - }, - - methods: { - onFocus(key) { - this.errorInfo[key] = ''; - }, - - getErrorMessageByKey(key) { - const value = this.data[key].trim(); + const getErrorMessageByKey = (key) => { + const value = state.contact[key].trim(); switch (key) { case 'name': return value ? '' : t('nameInvalid'); case 'tel': - return this.telValidator(value) ? '' : t('telInvalid'); + return props.telValidator(value) ? '' : t('telInvalid'); } - }, + }; - onSave() { + const onSave = () => { const isValid = ['name', 'tel'].every((item) => { - const msg = this.getErrorMessageByKey(item); + const msg = getErrorMessageByKey(item); if (msg) { - this.errorInfo[item] = msg; + state.errorInfo[item] = msg; } return !msg; }); - if (isValid && !this.isSaving) { - this.$emit('save', this.data); + if (isValid && !props.isSaving) { + emit('save', state.contact); } - }, + }; - onDelete() { + const onDelete = () => { Dialog.confirm({ title: t('confirmDelete'), }).then(() => { - this.$emit('delete', this.data); + emit('delete', state.contact); }); - }, - }, + }; - render() { - const { data, errorInfo } = this; - const onFocus = (name) => () => this.onFocus(name); - - return ( -
-
- - -
- {this.showSetDefault && ( - ( - { - this.$emit('change-default', event); - }} - /> - ), - }} - title={this.setDefaultLabel} - class={bem('switch-cell')} - border={false} - > - )} -
+ const renderButtons = () => ( +
+
+ )}
); + + const renderSwitch = () => ( + { + emit('change-default', event); + }} + /> + ); + + const renderSetDefault = () => { + if (props.showSetDefault) { + return ( + + ); + } + }; + + watch( + () => props.contactInfo, + (value) => { + state.contact = { + ...DEFAULT_CONTACT, + ...value, + }; + } + ); + + return () => { + const { contact, errorInfo } = state; + return ( +
+
+ onFocus('name')} + /> + onFocus('tel')} + /> +
+ {renderSetDefault()} + {renderButtons()} +
+ ); + }; }, });