UNPKG

@fesjs/fes-design

Version:
41 lines (38 loc) 1.23 kB
import { ref, watch, computed } from 'vue'; import { isEqual } from 'lodash-es'; var useSpecialModel = (function (props, emit) { var _config$prop; let config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { prop: 'modelValue', isEqual: false }; let callback = arguments.length > 3 ? arguments[3] : undefined; const usingProp = (_config$prop = config === null || config === void 0 ? void 0 : config.prop) !== null && _config$prop !== void 0 ? _config$prop : 'modelValue'; const currentValue = ref(props[usingProp]); const pureUpdateCurrentValue = value => { if (value === currentValue.value || config.isEqual && isEqual(value, currentValue.value)) { return; } currentValue.value = value; }; const updateCurrentValue = value => { if (value === currentValue.value) { return; } pureUpdateCurrentValue(value); emit(`update:${usingProp}`, value); callback(); }; watch(() => props[usingProp], val => { pureUpdateCurrentValue(val); }); return [computed({ get() { return currentValue.value; }, set(value) { updateCurrentValue(value); } }), updateCurrentValue]; }); export { useSpecialModel as default };