diff --git a/src/components/DataTableManager/filters.tsx b/src/components/DataTableManager/filters.tsx index b9068e6c..eb82bd21 100644 --- a/src/components/DataTableManager/filters.tsx +++ b/src/components/DataTableManager/filters.tsx @@ -24,20 +24,20 @@ export type DataTableFilterConfig = { }; const DataTableManagerFilters = ({ - filterConfig, filters, setFilters = () => {}, showFilters = false, + filterConfig, filters, setFilters = () => {}, } : { filterConfig: DataTableFilterConfig[], filters?: FilterInputs, setFilters?: (f: FilterInputs) => void, - showFilters?: boolean, }) => { - const [lastFetchKeyword, setFetchKeyword] = useState(null); - const [cachedOptions, setCachedOptions] = useState<{ [key: string]: { label: string, value: string }[] }>({}); + const [allOptions, setAllOptions] = useState<{ [key: string]: { label: string, value: string }[] }>({ + ...filterConfig.reduce((acc, f) => ({ ...acc, [f.key]: [] }), {}), + }); const getOptions = (f: DataTableFilterConfig) => { if (typeof f.onFetch === 'function') - return cachedOptions[f.key] || []; + return allOptions[f.key] || []; if (f.options) return f.options ?? []; return []; }; @@ -57,32 +57,34 @@ const DataTableManagerFilters = ({ ), [filterConfig]); const fetch = async (key: string, keyword: string, onFetch: (keyword: string) => Promise<{ label: string, value: string }[]>) => { - if (lastFetchKeyword === keyword) - return cachedOptions[key]; - const options = await onFetch(keyword); - - setCachedOptions({ ...cachedOptions, [key]: options }); - setFetchKeyword(keyword); + setAllOptions((prev) => ({ + ...prev, + [key]: [...new Set([ + ...(prev[key] || []), + ...(filterConfig.find((f) => f.key === key)?.options ?? []), + ...options, + ])], + })); return options; }; - return showFilters ? ( + return (
-
+
{filterConfig.filter((f) => (typeof f.onFetch === 'function' || f.options?.length) && !f.isHidden, ).map((f) => { const optionsLength = getOptions(f).length; const isFiltered = ( filters?.[f.key] && filters?.[f.key]?.length && - (optionsLength < 2 || (optionsLength > 1 && (filters?.[f.key]?.length < optionsLength))) + (optionsLength < 2 || (optionsLength > 1 && (filters?.[f.key]?.length < optionsLength))) ); return (
@@ -91,7 +93,7 @@ const DataTableManagerFilters = ({ isAsync={typeof f.onFetch === 'function'} onFetch={(keyword) => typeof f.onFetch === 'function' ? fetch(f.key, keyword, f.onFetch) : Promise.resolve([]) - } + } labels={{ searchLabel: f.labels?.searchLabel, optionsTitle: f.labels?.optionsTitle, @@ -114,7 +116,7 @@ const DataTableManagerFilters = ({ })}
{(isFilteredView) ? ( -
+
{filterConfig.filter((f) => { const optionsLength = getOptions(f).length; return ( @@ -122,7 +124,7 @@ const DataTableManagerFilters = ({ (optionsLength < 2 || (optionsLength > 1 && (filters?.[f.key]?.length < optionsLength))) ); }).map((f) => ( -
+
{(f.labels && f.labels.label && f.labels.label?.length > 0) ? (
{`${f.labels?.label}:`} @@ -139,7 +141,7 @@ const DataTableManagerFilters = ({ [f.key]: filters?.[f.key]?.filter((_s: any) => _s !== s), })} > - {filterConfig?.find((c) => f.key === c.key)?.options?.find((o) => o.value === s)?.label ?? s} + {allOptions[f.key]?.find((o) => o.value === s)?.label ?? s} ))} @@ -148,7 +150,7 @@ const DataTableManagerFilters = ({
) : null}
- ) :
; + ); }; diff --git a/src/components/DataTableManager/index.tsx b/src/components/DataTableManager/index.tsx index 117c4786..44a51fa2 100644 --- a/src/components/DataTableManager/index.tsx +++ b/src/components/DataTableManager/index.tsx @@ -158,7 +158,7 @@ const DataTableManager = ({ /> ) : null}
- {(filters && !showFilters) && ( + {(filters && !showFilters) ? ( - )} + ) : filters && showFilters ? ( + + ) : null} {(columns && columns?.length > 0) ? (
- {filterConfig && filterConfig?.length > 0 ? ( + {showFilters && filterConfig && filterConfig?.length > 0 ? ( ) : null} {(selections && (selections?.selected || selections.excluded)) ? ( diff --git a/src/components/DropdownFilter.tsx b/src/components/DropdownFilter.tsx index d729b966..c9cd0375 100644 --- a/src/components/DropdownFilter.tsx +++ b/src/components/DropdownFilter.tsx @@ -2,7 +2,6 @@ import React, { ReactElement, useEffect, useRef, useState } from 'react'; import { nanoid } from 'nanoid'; import clsx from 'clsx'; -import _ from 'lodash'; import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; import mcs from '../utils/merge'; @@ -131,9 +130,9 @@ const DropdownRender = ({ hideLabel autoFocus keyword={keyword} - inputClassName="py-1 px-2 !border-0 !rounded-b-none !border-b !bg-transparent !border-neutral-200/50" - buttonClassName="p-1 !border-none !outline-none !rounded-b-none !bg-transparent" - buttonWrapperClassName="!border-0 !outline-none !rounded-b-none !border-b !bg-transparent !border-neutral-200/50" + inputClassName="py-1 px-2 border-none rounded-b-none border-b bg-transparent dark:border-gray-500/70 border-gray-500/10" + buttonClassName="py-1 px-2 border-none outline-none rounded-b-none bg-transparent" + buttonWrapperClassName="border-none outline-none rounded-b-none border-b bg-transparent dark:border-gray-500/70 border-gray-500/10" onKeyDown={handleKeyDown} labels={{ placeholder: labels.searchPlaceholder, @@ -143,7 +142,7 @@ const DropdownRender = ({ />
{labels.optionsTitle?.length > 0 ? ( -
+
{labels.optionsTitle} @@ -151,7 +150,7 @@ const DropdownRender = ({ ) : null}
0) && 'border-t border-neutral-200/50', + !(labels.optionsTitle?.length > 0) && 'border-t dark:border-gray-500/70 border-gray-500/10', 'max-h-[30vh] overflow-y-auto', ])} > @@ -206,13 +205,13 @@ const DropdownRender = ({ )}
-
+