yanzhen-design-vue
Version:
48 lines (47 loc) • 1.5 kB
JavaScript
import { ref, computed } from "vue";
import { isString, isArray } from "lodash-es";
import { isEmptyValue, def } from "@yanzhen-libs/utils";
import { unref } from "@yanzhen-libs/utils-vue";
import { handleContactsData } from "../hook/use-contacts-selected-data.mjs";
import { handleDataType } from "../utils/handleDataType.mjs";
const contactsDataTypeKey = Symbol("contacts-data-type-key");
function createContactsDefaultRef(...args) {
const types = args.filter(isString);
const dataType = types.join(".");
const _ref = ref([]);
function setItemType(item) {
if (!isEmptyValue(item.dataType)) {
item.type = handleDataType(item, types[0]);
}
if (isEmptyValue(item.dataType)) {
item.dataType = dataType;
}
def(item, contactsDataTypeKey, dataType);
return item;
}
function handlePush(...items) {
if (isArray(items)) handleContactsData([], items, setItemType);
unref(_ref).push(...items);
}
return computed({
get() {
const items = unref(_ref);
return new Proxy(items, {
get(target, p, receiver) {
if (p === "push") return handlePush;
return Reflect.get(target, p, receiver);
},
set(target, p, value, receiver) {
return Reflect.set(target, p, value, receiver);
}
});
},
set(items) {
_ref.value = isArray(items) ? items : [];
if (isArray(items)) handleContactsData([], items, setItemType);
}
});
}
export {
createContactsDefaultRef
};