yanzhen-design-vue
Version:
48 lines (47 loc) • 1.18 kB
JavaScript
import { reactive, onMounted, computed } from "vue";
import { useProps, createWatcher, unref } from "@yanzhen-libs/utils-vue";
import { isEmptyArray } from "@yanzhen-libs/utils";
import { contactsModelValueType } from "../constants/contacts.mjs";
function useSelectedIds() {
const refs = useProps([...contactsModelValueType]);
const config = reactive({
ids: []
});
const watcher = createWatcher();
function hasValue(value) {
return ["string", "number"].includes(typeof value);
}
function setIds() {
const ids = [];
const values = Object.values(unref(refs));
for (const value of values) {
if (hasValue(value) && !ids.includes(value)) {
ids.push(value);
} else if (!isEmptyArray(value)) {
for (const val of value) {
if (hasValue(val) && !ids.includes(val)) {
ids.push(val);
}
}
}
}
config.ids = ids;
}
watcher.set("ids", {
source: () => unref(refs),
handler() {
setIds();
},
deep: true,
debounce: 300
});
onMounted(() => {
setIds();
});
return computed(() => {
return config.ids;
});
}
export {
useSelectedIds
};