import { watch, reactive, PropType, defineComponent } from 'vue'; // Utils import { isMobile, createNamespace, extend } from '../utils'; // Components import { Cell } from '../cell'; import { Form } from '../form'; import { Field } from '../field'; import { Button } from '../button'; import { Dialog } from '../dialog'; import { Switch } from '../switch'; const [name, bem, t] = createNamespace('contact-edit'); export type ContactEditInfo = { tel: string; name: string; isDefault?: boolean; }; const DEFAULT_CONTACT: ContactEditInfo = { tel: '', name: '', }; export default defineComponent({ name, props: { isEdit: Boolean, isSaving: Boolean, isDeleting: Boolean, showSetDefault: Boolean, setDefaultLabel: String, contactInfo: { type: Object as PropType, default: () => extend({}, DEFAULT_CONTACT), }, telValidator: { type: Function as PropType<(val: string) => boolean>, default: isMobile, }, }, emits: ['save', 'delete', 'change-default'], setup(props, { emit }) { const contact = reactive(extend({}, DEFAULT_CONTACT, props.contactInfo)); const onSave = () => { if (!props.isSaving) { emit('save', contact); } }; const onDelete = () => { Dialog.confirm({ title: t('confirmDelete'), }).then(() => emit('delete', contact)); }; const renderButtons = () => (
); const renderSwitch = () => ( emit('change-default', checked)} /> ); const renderSetDefault = () => { if (props.showSetDefault) { return ( ); } }; watch( () => props.contactInfo, (value) => extend(contact, DEFAULT_CONTACT, value) ); return () => (
{renderSetDefault()} {renderButtons()}
); }, });