import { watch, reactive } from 'vue'; // Utils import { createNamespace } from '../utils'; import { isMobile } from '../utils/validate/mobile'; // Components import Cell from '../cell'; import Field from '../field'; import Button from '../button'; import Dialog from '../dialog'; import Switch from '../switch'; const [createComponent, bem, t] = createNamespace('contact-edit'); const DEFAULT_CONTACT = { tel: '', name: '', }; export default createComponent({ props: { isEdit: Boolean, isSaving: Boolean, isDeleting: Boolean, showSetDefault: Boolean, setDefaultLabel: String, contactInfo: { type: Object, default: () => ({ ...DEFAULT_CONTACT }), }, telValidator: { type: Function, default: isMobile, }, }, emits: ['save', 'delete', 'change-default'], setup(props, { emit }) { const state = reactive({ contact: { ...DEFAULT_CONTACT, ...props.contactInfo, }, errorInfo: { name: '', tel: '', }, }); 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); } }; const onDelete = () => { Dialog.confirm({ title: t('confirmDelete'), }).then(() => { emit('delete', state.contact); }); }; const renderButtons = () => (
); const renderSwitch = () => (