UNPKG

@empathyco/x-components

Version:
63 lines (60 loc) 2.16 kB
import { isHierarchicalFilter } from '@empathyco/x-types'; import { inject, computed } from 'vue'; import { isArrayEmpty } from '../../../utils/array.js'; /** * Composable to share filters injection logic. * * @param props - Composable props. * @returns An array of filters. * @public */ function useFiltersInjection(props) { /** * The injected filters array. * * @public */ const injectedFilters = inject('filters', undefined); /** * An array of filters formed by those that are passed through prop or injected. * * @returns An array of filters. * * @internal */ const propOrInjectedFilters = computed(() => { return (props.filters ?? injectedFilters?.value ?? console.warn('It is necessary to pass a prop or inject the list of filters')); }); /** * In the case that the filters are {@link @empathyco/x-types#HierarchicalFilter}, this method * removes from the filter list passed as a param, the filters that are not part of the level of * the hierarchy, depending on the value of the `parentId` prop. In case this prop is undefined, * then only the first level of filters hierarchy are returned. In the case the prop `parentId` is * defined, then only the filters with the same `parentId` are returned. * * @param filters - The list of the filters to apply the filter. * @returns The list of the filters filtered by parentId. * @internal */ const filterByParentId = (filters) => { if (!isArrayEmpty(filters) && isHierarchicalFilter(filters[0])) { return filters.filter(filter => filter.parentId === (props.parentId ?? null)); } else { return filters; } }; /** * The prop or injected filters array, filtered by parentId if they are * {@link @empathyco/x-types#HierarchicalFilter}. * * @returns An array of filters. * * @internal */ return computed(() => filterByParentId(propOrInjectedFilters.value)); } export { useFiltersInjection }; //# sourceMappingURL=use-filters-injection.js.map