197 lines
6.4 KiB
JavaScript
197 lines
6.4 KiB
JavaScript
import { Bt as onUpdated, Ft as onMounted, Qn as toRef, U as computed, Ut as provide, Wn as reactive, cn as useId, gn as watch, jt as onBeforeUnmount, nr as unref, xt as inject } from "./vue.runtime.esm-bundler-BvoXUmaf.js";
|
|
import { T as findChildrenWithProvide, Z as wrapInArray, it as consoleWarn, l as propsFactory, s as getCurrentInstance } from "./defineComponent-DB6xIcDy.js";
|
|
import { t as deepEqual } from "./deepEqual-DDqmGqyF.js";
|
|
import { t as useProxiedModel } from "./proxiedModel-DSlSIQ0y.js";
|
|
//#region node_modules/vuetify/lib/composables/group.js
|
|
var makeGroupProps = propsFactory({
|
|
modelValue: {
|
|
type: null,
|
|
default: void 0
|
|
},
|
|
multiple: Boolean,
|
|
mandatory: [Boolean, String],
|
|
max: Number,
|
|
selectedClass: String,
|
|
disabled: Boolean
|
|
}, "group");
|
|
var makeGroupItemProps = propsFactory({
|
|
value: null,
|
|
disabled: Boolean,
|
|
selectedClass: String
|
|
}, "group-item");
|
|
function useGroupItem(props, injectKey, required = true) {
|
|
const vm = getCurrentInstance("useGroupItem");
|
|
if (!vm) throw new Error("[Vuetify] useGroupItem composable must be used inside a component setup function");
|
|
const id = useId();
|
|
provide(Symbol.for(`${injectKey.description}:id`), id);
|
|
const group = inject(injectKey, null);
|
|
if (!group) {
|
|
if (!required) return group;
|
|
throw new Error(`[Vuetify] Could not find useGroup injection with symbol ${injectKey.description}`);
|
|
}
|
|
const value = toRef(() => props.value);
|
|
const disabled = computed(() => !!(group.disabled.value || props.disabled));
|
|
function register() {
|
|
group?.register({
|
|
id,
|
|
value,
|
|
disabled
|
|
}, vm);
|
|
}
|
|
function unregister() {
|
|
group?.unregister(id);
|
|
}
|
|
register();
|
|
onBeforeUnmount(() => unregister());
|
|
const isSelected = computed(() => {
|
|
return group.isSelected(id);
|
|
});
|
|
const isFirst = computed(() => {
|
|
return group.items.value[0].id === id;
|
|
});
|
|
const isLast = computed(() => {
|
|
return group.items.value[group.items.value.length - 1].id === id;
|
|
});
|
|
const selectedClass = computed(() => isSelected.value && [group.selectedClass.value, props.selectedClass]);
|
|
watch(isSelected, (value) => {
|
|
vm.emit("group:selected", { value });
|
|
}, { flush: "sync" });
|
|
return {
|
|
id,
|
|
isSelected,
|
|
isFirst,
|
|
isLast,
|
|
toggle: () => group.select(id, !isSelected.value),
|
|
select: (value) => group.select(id, value),
|
|
selectedClass,
|
|
value,
|
|
disabled,
|
|
group,
|
|
register,
|
|
unregister
|
|
};
|
|
}
|
|
function useGroup(props, injectKey) {
|
|
let isUnmounted = false;
|
|
const items = reactive([]);
|
|
const selected = useProxiedModel(props, "modelValue", [], (v) => {
|
|
if (v === void 0) return [];
|
|
return getIds(items, v === null ? [null] : wrapInArray(v));
|
|
}, (v) => {
|
|
const arr = getValues(items, v);
|
|
return props.multiple ? arr : arr[0];
|
|
});
|
|
const groupVm = getCurrentInstance("useGroup");
|
|
function register(item, vm) {
|
|
const unwrapped = item;
|
|
const index = findChildrenWithProvide(Symbol.for(`${injectKey.description}:id`), groupVm?.vnode).indexOf(vm);
|
|
if (unref(unwrapped.value) === void 0) {
|
|
unwrapped.value = index;
|
|
unwrapped.useIndexAsValue = true;
|
|
}
|
|
if (index > -1) items.splice(index, 0, unwrapped);
|
|
else items.push(unwrapped);
|
|
}
|
|
function unregister(id) {
|
|
if (isUnmounted) return;
|
|
forceMandatoryValue();
|
|
const index = items.findIndex((item) => item.id === id);
|
|
items.splice(index, 1);
|
|
}
|
|
function forceMandatoryValue() {
|
|
const item = items.find((item) => !item.disabled);
|
|
if (item && props.mandatory === "force" && !selected.value.length) selected.value = [item.id];
|
|
}
|
|
onMounted(() => {
|
|
forceMandatoryValue();
|
|
});
|
|
onBeforeUnmount(() => {
|
|
isUnmounted = true;
|
|
});
|
|
onUpdated(() => {
|
|
for (let i = 0; i < items.length; i++) if (items[i].useIndexAsValue) items[i].value = i;
|
|
});
|
|
function select(id, value) {
|
|
const item = items.find((item) => item.id === id);
|
|
if (value && item?.disabled) return;
|
|
if (props.multiple) {
|
|
const internalValue = selected.value.slice();
|
|
const index = internalValue.findIndex((v) => v === id);
|
|
const isSelected = ~index;
|
|
value = value ?? !isSelected;
|
|
if (isSelected && props.mandatory && internalValue.length <= 1) return;
|
|
if (!isSelected && props.max != null && internalValue.length + 1 > props.max) return;
|
|
if (index < 0 && value) internalValue.push(id);
|
|
else if (index >= 0 && !value) internalValue.splice(index, 1);
|
|
selected.value = internalValue;
|
|
} else {
|
|
const isSelected = selected.value.includes(id);
|
|
if (props.mandatory && isSelected) return;
|
|
if (!isSelected && !value) return;
|
|
selected.value = value ?? !isSelected ? [id] : [];
|
|
}
|
|
}
|
|
function step(offset) {
|
|
if (props.multiple) consoleWarn("This method is not supported when using \"multiple\" prop");
|
|
if (!selected.value.length) {
|
|
const item = items.find((item) => !item.disabled);
|
|
item && (selected.value = [item.id]);
|
|
} else {
|
|
const currentId = selected.value[0];
|
|
const currentIndex = items.findIndex((i) => i.id === currentId);
|
|
let newIndex = (currentIndex + offset) % items.length;
|
|
let newItem = items[newIndex];
|
|
while (newItem.disabled && newIndex !== currentIndex) {
|
|
newIndex = (newIndex + offset) % items.length;
|
|
newItem = items[newIndex];
|
|
}
|
|
if (newItem.disabled) return;
|
|
selected.value = [items[newIndex].id];
|
|
}
|
|
}
|
|
const state = {
|
|
register,
|
|
unregister,
|
|
selected,
|
|
select,
|
|
disabled: toRef(() => props.disabled),
|
|
prev: () => step(items.length - 1),
|
|
next: () => step(1),
|
|
isSelected: (id) => selected.value.includes(id),
|
|
selectedClass: toRef(() => props.selectedClass),
|
|
items: toRef(() => items),
|
|
getItemIndex: (value) => getItemIndex(items, value)
|
|
};
|
|
provide(injectKey, state);
|
|
return state;
|
|
}
|
|
function getItemIndex(items, value) {
|
|
const ids = getIds(items, [value]);
|
|
if (!ids.length) return -1;
|
|
return items.findIndex((item) => item.id === ids[0]);
|
|
}
|
|
function getIds(items, modelValue) {
|
|
const ids = [];
|
|
modelValue.forEach((value) => {
|
|
const item = items.find((item) => deepEqual(value, item.value));
|
|
const itemByIndex = items[value];
|
|
if (item?.value !== void 0) ids.push(item.id);
|
|
else if (itemByIndex?.useIndexAsValue) ids.push(itemByIndex.id);
|
|
});
|
|
return ids;
|
|
}
|
|
function getValues(items, ids) {
|
|
const values = [];
|
|
ids.forEach((id) => {
|
|
const itemIndex = items.findIndex((item) => item.id === id);
|
|
if (~itemIndex) {
|
|
const item = items[itemIndex];
|
|
values.push(item.value !== void 0 ? item.value : itemIndex);
|
|
}
|
|
});
|
|
return values;
|
|
}
|
|
//#endregion
|
|
export { useGroupItem as i, makeGroupProps as n, useGroup as r, makeGroupItemProps as t };
|
|
|
|
//# sourceMappingURL=group-Cm2viEWK.js.map
|