UNPKG

element-plus

Version:
110 lines (109 loc) 3.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useModelToggle = exports.useModelToggleEmits = exports.useModelToggleProps = void 0; const vue_1 = require("vue"); const shared_1 = require("@vue/shared"); const util_1 = require("../../utils/util"); const constants_1 = require("../../utils/constants"); const isServer_1 = __importDefault(require("../../utils/isServer")); exports.useModelToggleProps = { modelValue: { type: Boolean, default: null, }, 'onUpdate:modelValue': Function, }; exports.useModelToggleEmits = [constants_1.UPDATE_MODEL_EVENT]; const useModelToggle = ({ indicator, shouldHideWhenRouteChanges, shouldProceed, onShow, onHide, }) => { const { appContext, props, proxy, emit } = vue_1.getCurrentInstance(); const hasUpdateHandler = vue_1.computed(() => shared_1.isFunction(props['onUpdate:modelValue'])); // when it matches the default value we say this is absent // though this could be mistakenly passed from the user but we need to rule out that // condition const isModelBindingAbsent = vue_1.computed(() => props.modelValue === null); const doShow = () => { if (indicator.value === true) { return; } indicator.value = true; if (shared_1.isFunction(onShow)) { onShow(); } }; const doHide = () => { if (indicator.value === false) { return; } indicator.value = false; if (shared_1.isFunction(onHide)) { onHide(); } }; const show = () => { if (props.disabled === true || (shared_1.isFunction(shouldProceed) && !shouldProceed())) return; const shouldEmit = hasUpdateHandler.value && !isServer_1.default; if (shouldEmit) { emit(constants_1.UPDATE_MODEL_EVENT, true); } if (isModelBindingAbsent.value || !shouldEmit) { doShow(); } }; const hide = () => { if (props.disabled === true || isServer_1.default) return; const shouldEmit = hasUpdateHandler.value && !isServer_1.default; if (shouldEmit) { emit(constants_1.UPDATE_MODEL_EVENT, false); } if (isModelBindingAbsent.value || !shouldEmit) { doHide(); } }; const onChange = (val) => { if (!util_1.isBool(val)) return; if (props.disabled && val) { if (hasUpdateHandler.value) { emit(constants_1.UPDATE_MODEL_EVENT, false); } } else if (indicator.value !== val) { if (val) { doShow(); } else { doHide(); } } }; const toggle = () => { if (indicator.value) { hide(); } else { show(); } }; vue_1.watch(() => props.modelValue, onChange); if (shouldHideWhenRouteChanges && appContext.config.globalProperties.$route !== void 0) { vue_1.watch(() => (Object.assign({}, proxy.$route)), () => { if (shouldHideWhenRouteChanges.value && indicator.value) { hide(); } }); } vue_1.onMounted(() => { onChange(props.modelValue); }); return { hide, show, toggle, }; }; exports.useModelToggle = useModelToggle;