vira
Version:
A simple and highly versatile design system using element-vir.
59 lines (58 loc) • 1.66 kB
JavaScript
import { joinWithFinalConjunction } from '@augment-vir/common';
/**
* Verifies that all items have unique ids.
*
* @category Internal
*/
export function assertUniqueIdProps(items) {
const usedIds = new Set();
const duplicateIds = [];
items.forEach((option) => {
if (usedIds.has(option.id)) {
duplicateIds.push(option.id);
}
else {
usedIds.add(option.id);
}
});
if (duplicateIds.length) {
throw new Error(`Duplicate option ids were given: ${joinWithFinalConjunction(duplicateIds)}`);
}
}
/**
* Creates a new array of selections based on the current selection and new selection id. This
* behaves differently when multi select is enabled, hence this function.
*
* @category Internal
*/
export function updateSelectedItems(
/** The item that should be newly toggled. */
newItem, currentSelection = [], isMultiSelect = false) {
if (isMultiSelect) {
return currentSelection.includes(newItem.id)
? currentSelection.filter((entry) => entry !== newItem.id)
: [
...currentSelection,
newItem.id,
];
}
else {
/** In single select, only the toggled item is allowed. */
return [newItem.id];
}
}
/**
* Handles toggling pop up state for `ViraDropdown`.
*
* @category Internal
*/
export function triggerPopUpState({ open, callback, popUpManager, host, }) {
if (open) {
const showPopUpResult = popUpManager.showPopUp(host);
callback?.(showPopUpResult);
}
else {
popUpManager.removePopUp();
callback?.(undefined);
}
}