UNPKG

vuestic-ui

Version:
50 lines (49 loc) 1.23 kB
import { getCurrentInstance, computed, ref, watch } from "vue"; function useSyncProp(propName, props, emit, defaultValue) { const vm = getCurrentInstance(); const isPropPassed = computed(() => { const t = props[propName]; if (!(vm == null ? void 0 : vm.vnode.props)) { return t !== void 0; } return propName in vm.vnode.props && vm.vnode.props[propName] !== void 0; }); if (defaultValue === void 0) { return [ computed({ set(value) { emit(`update:${propName}`, value); }, get() { return props[propName]; } }) ]; } const currentValue = props[propName]; const statefulValue = ref(currentValue === void 0 ? defaultValue : currentValue); watch(() => props[propName], (newVal) => { if (newVal === void 0) { return; } statefulValue.value = newVal; }); return [ computed({ set(value) { statefulValue.value = value; emit(`update:${propName}`, value); }, get() { if (isPropPassed.value) { return props[propName]; } return statefulValue.value; } }) ]; } export { useSyncProp as u }; //# sourceMappingURL=useSyncProp.js.map