analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
236 lines (226 loc) • 9.74 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/Filter/useTableFilter.ts
var _react = require('react');
// src/components/CheckBoxGroup/CheckBoxGroup.helpers.ts
var areSelectedIdsEqual = (ids1, ids2) => {
if (ids1 === ids2) return true;
if (!ids1 || !ids2) return ids1 === ids2;
if (ids1.length !== ids2.length) return false;
for (let i = 0; i < ids1.length; i++) {
if (ids1[i] !== ids2[i]) return false;
}
return true;
};
var isCategoryEnabled = (category, allCategories) => {
if (!category.dependsOn || category.dependsOn.length === 0) {
return true;
}
return category.dependsOn.every((depKey) => {
const depCat = allCategories.find((c) => c.key === depKey);
return _optionalChain([depCat, 'optionalAccess', _ => _.selectedIds]) && depCat.selectedIds.length > 0;
});
};
var isItemMatchingFilter = (item, filter, allCategories) => {
const parentCat = allCategories.find((c) => c.key === filter.key);
const parentSelectedIds = _optionalChain([parentCat, 'optionalAccess', _2 => _2.selectedIds]) || [];
const itemFieldValue = item[filter.internalField];
return parentSelectedIds.includes(String(itemFieldValue));
};
var getBadgeText = (category, formattedItems) => {
const visibleIds = formattedItems.flatMap((group) => group.itens || []).map((i) => i.id);
const selectedVisibleCount = visibleIds.filter(
(id) => _optionalChain([category, 'access', _3 => _3.selectedIds, 'optionalAccess', _4 => _4.includes, 'call', _5 => _5(id)])
).length;
const totalVisible = visibleIds.length;
return `${selectedVisibleCount} de ${totalVisible} ${selectedVisibleCount === 1 ? "selecionado" : "selecionados"}`;
};
var handleAccordionValueChange = (value, categories, isCategoryEnabledFn) => {
if (typeof value !== "string") {
if (!value) {
return "";
}
return null;
}
if (!value) {
return "";
}
const category = categories.find((c) => c.key === value);
if (!category) {
return null;
}
const isEnabled = isCategoryEnabledFn(category);
if (!isEnabled) {
return null;
}
return value;
};
var calculateFormattedItemsForAutoSelection = (category, allCategories) => {
if (!_optionalChain([category, 'optionalAccess', _6 => _6.dependsOn]) || category.dependsOn.length === 0) {
return _optionalChain([category, 'optionalAccess', _7 => _7.itens]) || [];
}
const isEnabled = isCategoryEnabled(category, allCategories);
if (!isEnabled) {
return [];
}
const filters = category.filteredBy || [];
if (filters.length === 0) {
return _optionalChain([category, 'optionalAccess', _8 => _8.itens]) || [];
}
const selectedIdsArr = filters.map((f) => {
const parentCat = allCategories.find((c) => c.key === f.key);
if (!_optionalChain([parentCat, 'optionalAccess', _9 => _9.selectedIds, 'optionalAccess', _10 => _10.length])) {
return [];
}
return parentCat.selectedIds;
});
if (selectedIdsArr.some((arr) => arr.length === 0)) {
return [];
}
const filteredItems = (category.itens || []).filter(
(item) => filters.every((filter) => isItemMatchingFilter(item, filter, allCategories))
);
return filteredItems;
};
// src/components/Filter/useTableFilter.ts
var mergeConfigsWithSelections = (newConfigs, prevConfigs) => {
let changed = false;
const result = newConfigs.map((newConfig, i) => ({
...newConfig,
categories: newConfig.categories.map((newCat, j) => {
const prevCat = _optionalChain([prevConfigs, 'access', _11 => _11[i], 'optionalAccess', _12 => _12.categories, 'access', _13 => _13[j]]);
const prevItems = _nullishCoalesce(_optionalChain([prevCat, 'optionalAccess', _14 => _14.itens]), () => ( []));
const newItems = _nullishCoalesce(newCat.itens, () => ( []));
if (prevItems.length !== newItems.length) {
changed = true;
}
if (_optionalChain([prevCat, 'optionalAccess', _15 => _15.key]) === newCat.key && _optionalChain([prevCat, 'optionalAccess', _16 => _16.selectedIds, 'optionalAccess', _17 => _17.length])) {
const availableIds = new Set(newItems.map((item) => item.id));
const selectedIds = prevCat.selectedIds.filter(
(id) => availableIds.has(id)
);
if (selectedIds.length !== prevCat.selectedIds.length) {
changed = true;
}
return { ...newCat, selectedIds };
}
return newCat;
})
}));
return changed ? result : prevConfigs;
};
var useTableFilter = (initialConfigs, options = {}) => {
const { syncWithUrl = false } = options;
const getInitialState = _react.useCallback.call(void 0, () => {
if (!syncWithUrl || globalThis.window === void 0) {
return initialConfigs;
}
const params = new URLSearchParams(globalThis.window.location.search);
const configsWithUrlState = initialConfigs.map((config) => ({
...config,
categories: config.categories.map((category) => {
const urlValue = params.get(`filter_${category.key}`);
const selectedIds = urlValue ? urlValue.split(",").filter(Boolean) : [];
return {
...category,
selectedIds
};
})
}));
return configsWithUrlState;
}, [initialConfigs, syncWithUrl]);
const [filterConfigs, setFilterConfigs] = _react.useState.call(void 0, getInitialState);
const activeFilters = _react.useMemo.call(void 0, () => {
const filters = {};
for (const config of filterConfigs) {
for (const category of config.categories) {
if (category.selectedIds && category.selectedIds.length > 0) {
filters[category.key] = category.selectedIds;
}
}
}
return filters;
}, [filterConfigs]);
const hasActiveFilters = Object.keys(activeFilters).length > 0;
const activeFiltersCount = _react.useMemo.call(void 0, () => {
const allCategories = filterConfigs.flatMap((config) => config.categories);
let count = 0;
for (const category of allCategories) {
if (!category.selectedIds || category.selectedIds.length === 0) continue;
const availableItems = calculateFormattedItemsForAutoSelection(
category,
allCategories
);
const isAutoSelected = availableItems.length === 1 && category.selectedIds.length === 1 && category.selectedIds[0] === _optionalChain([availableItems, 'access', _18 => _18[0], 'optionalAccess', _19 => _19.id]);
if (!isAutoSelected) {
count++;
}
}
return count;
}, [filterConfigs]);
const updateFilters = _react.useCallback.call(void 0, (configs) => {
setFilterConfigs(configs);
}, []);
const applyFilters = _react.useCallback.call(void 0, () => {
if (!syncWithUrl || globalThis.window === void 0) {
return;
}
const url = new URL(globalThis.window.location.href);
const params = url.searchParams;
for (const config of filterConfigs) {
for (const category of config.categories) {
const paramKey = `filter_${category.key}`;
if (category.selectedIds && category.selectedIds.length > 0) {
params.set(paramKey, category.selectedIds.join(","));
} else {
params.delete(paramKey);
}
}
}
globalThis.window.history.replaceState({}, "", url.toString());
}, [filterConfigs, syncWithUrl]);
const clearFilters = _react.useCallback.call(void 0, () => {
const clearedConfigs = filterConfigs.map((config) => ({
...config,
categories: config.categories.map((category) => ({
...category,
selectedIds: []
}))
}));
setFilterConfigs(clearedConfigs);
if (syncWithUrl && globalThis.window !== void 0) {
const url = new URL(globalThis.window.location.href);
const params = url.searchParams;
for (const config of filterConfigs) {
for (const category of config.categories) {
params.delete(`filter_${category.key}`);
}
}
globalThis.window.history.replaceState({}, "", url.toString());
}
}, [filterConfigs, syncWithUrl]);
_react.useEffect.call(void 0, () => {
setFilterConfigs(
(prev) => mergeConfigsWithSelections(initialConfigs, prev)
);
}, [initialConfigs]);
_react.useEffect.call(void 0, () => {
if (!syncWithUrl || globalThis.window === void 0) {
return;
}
const handlePopState = () => {
setFilterConfigs(getInitialState());
};
globalThis.window.addEventListener("popstate", handlePopState);
return () => globalThis.window.removeEventListener("popstate", handlePopState);
}, [syncWithUrl, getInitialState]);
return {
filterConfigs,
activeFilters,
hasActiveFilters,
activeFiltersCount,
updateFilters,
applyFilters,
clearFilters
};
};
exports.areSelectedIdsEqual = areSelectedIdsEqual; exports.isCategoryEnabled = isCategoryEnabled; exports.getBadgeText = getBadgeText; exports.handleAccordionValueChange = handleAccordionValueChange; exports.calculateFormattedItemsForAutoSelection = calculateFormattedItemsForAutoSelection; exports.useTableFilter = useTableFilter;
//# sourceMappingURL=chunk-IT7YRLPM.js.map