diff --git a/frontend/src/component/common/FilterDateItem/FilterDateItem.tsx b/frontend/src/component/common/FilterDateItem/FilterDateItem.tsx index e0dbb82fea..96d96c4502 100644 --- a/frontend/src/component/common/FilterDateItem/FilterDateItem.tsx +++ b/frontend/src/component/common/FilterDateItem/FilterDateItem.tsx @@ -21,6 +21,7 @@ export interface IFilterDateItemProps { onChipClose?: () => void; state: FilterItemParams | null | undefined; operators: [string, ...string[]]; + initMode?: 'auto-open' | 'manual'; } export const FilterDateItem: FC = ({ @@ -31,6 +32,7 @@ export const FilterDateItem: FC = ({ onChipClose, state, operators, + initMode = 'auto-open', }) => { const ref = useRef(null); const [anchorEl, setAnchorEl] = useState(null); @@ -41,7 +43,7 @@ export const FilterDateItem: FC = ({ }; useEffect(() => { - if (!state) { + if (!state && initMode === 'auto-open') { open(); } }, []); diff --git a/frontend/src/component/filter/FilterItem/FilterItem.tsx b/frontend/src/component/filter/FilterItem/FilterItem.tsx index 0b79ea6552..3cf31790b0 100644 --- a/frontend/src/component/filter/FilterItem/FilterItem.tsx +++ b/frontend/src/component/filter/FilterItem/FilterItem.tsx @@ -15,10 +15,11 @@ export interface IFilterItemProps { label: ReactNode; options: Array<{ label: string; value: string }>; onChange: (value: FilterItemParams) => void; - onChipClose: () => void; + onChipClose?: () => void; state: FilterItemParams | null | undefined; singularOperators: [string, ...string[]]; pluralOperators: [string, ...string[]]; + initMode?: 'auto-open' | 'manual'; } export type FilterItemParams = { @@ -80,6 +81,7 @@ export const FilterItem: FC = ({ state, singularOperators, pluralOperators, + initMode = 'auto-open', }) => { const ref = useRef(null); const [anchorEl, setAnchorEl] = useState(); @@ -93,7 +95,7 @@ export const FilterItem: FC = ({ }; useEffect(() => { - if (!state) { + if (!state && initMode === 'auto-open') { open(); } }, []); @@ -108,11 +110,13 @@ export const FilterItem: FC = ({ .filter((label): label is string => label !== undefined); const currentOperator = state ? state.operator : currentOperators[0]; - const onDelete = () => { - onChange({ operator: singularOperators[0], values: [] }); - onClose(); - onChipClose(); - }; + const onDelete = onChipClose + ? () => { + onChange({ operator: singularOperators[0], values: [] }); + onClose(); + onChipClose(); + } + : undefined; const filteredOptions = options.filter((option) => option.label.toLowerCase().includes(searchText.toLowerCase()), diff --git a/frontend/src/component/filter/Filters/Filters.tsx b/frontend/src/component/filter/Filters/Filters.tsx index 71dc3935f2..6241f6c041 100644 --- a/frontend/src/component/filter/Filters/Filters.tsx +++ b/frontend/src/component/filter/Filters/Filters.tsx @@ -35,6 +35,7 @@ type IBaseFilterItem = { value: string; }[]; filterKey: string; + persistent?: boolean; }; type ITextFilterItem = IBaseFilterItem & { @@ -46,7 +47,6 @@ export type IDateFilterItem = IBaseFilterItem & { dateOperators: [string, ...string[]]; fromFilterKey?: string; toFilterKey?: string; - persistent?: boolean; }; export type IFilterItem = ITextFilterItem | IDateFilterItem; @@ -64,10 +64,116 @@ const StyledIcon = styled(Icon)(({ theme }) => ({ }, })); -export const Filters: FC = ({ +type RangeChangeHandler = (filter: IDateFilterItem) => + | ((value: { + from: FilterItemParams; + to: FilterItemParams; + }) => void) + | undefined; + +type RenderFilterProps = { + onChipClose?: (label: string) => void; + state: FilterItemParams | null | undefined; + onChange: (value: FilterItemParamHolder) => void; + filter: ITextFilterItem | IDateFilterItem; + rangeChangeHandler: RangeChangeHandler; + initMode?: 'auto-open' | 'manual'; +}; + +const RenderFilter: FC = ({ + filter, + onChipClose, + onChange, + state, + rangeChangeHandler, + initMode, +}) => { + const label = ( + <> + + {filter.icon} + + {filter.label} + + ); + + if ('dateOperators' in filter) { + return ( + { + onChange({ [filter.filterKey]: value }); + }} + onRangeChange={rangeChangeHandler?.(filter)} + operators={filter.dateOperators} + onChipClose={ + filter.persistent + ? undefined + : () => onChipClose?.(filter.label) + } + /> + ); + } + + return ( + onChange({ [filter.filterKey]: value })} + singularOperators={filter.singularOperators} + pluralOperators={filter.pluralOperators} + onChipClose={ + filter.persistent + ? undefined + : () => onChipClose?.(filter.label) + } + /> + ); +}; + +type SingleFilterProps = Omit & { + filter: IFilterItem; + rangeChangeHandler: RangeChangeHandler; +}; + +const SingleFilter: FC = ({ + state, + onChange, + className, + filter, + rangeChangeHandler, +}) => { + return ( + + + + ); +}; + +type MultiFilterProps = IFilterProps & { + rangeChangeHandler: RangeChangeHandler; +}; + +const MultiFilter: FC = ({ state, onChange, availableFilters, + rangeChangeHandler, className, }) => { const [unselectedFilters, setUnselectedFilters] = useState([]); @@ -123,21 +229,6 @@ export const Filters: FC = ({ const hasAvailableFilters = unselectedFilters.length > 0; - const rangeChangeHandler = (filter: IDateFilterItem) => { - const fromKey = filter.fromFilterKey; - const toKey = filter.toFilterKey; - if (fromKey && toKey) { - return (value: { - from: FilterItemParams; - to: FilterItemParams; - }) => { - onChange({ [fromKey]: value.from }); - onChange({ [toKey]: value.to }); - }; - } - return undefined; - }; - return ( {selectedFilters.map((selectedFilter) => { @@ -149,48 +240,13 @@ export const Filters: FC = ({ return null; } - const label = ( - <> - - {filter.icon} - - {filter.label} - - ); - - if ('dateOperators' in filter) { - return ( - { - onChange({ [filter.filterKey]: value }); - }} - onRangeChange={rangeChangeHandler(filter)} - operators={filter.dateOperators} - onChipClose={ - filter.persistent - ? undefined - : () => deselectFilter(filter.label) - } - /> - ); - } - return ( - - onChange({ [filter.filterKey]: value }) - } - singularOperators={filter.singularOperators} - pluralOperators={filter.pluralOperators} + onChange={onChange} + rangeChangeHandler={rangeChangeHandler} onChipClose={() => deselectFilter(filter.label)} /> ); @@ -211,3 +267,33 @@ export const Filters: FC = ({ ); }; + +export const Filters: FC = (props) => { + const rangeChangeHandler = (filter: IDateFilterItem) => { + const fromKey = filter.fromFilterKey; + const toKey = filter.toFilterKey; + if (fromKey && toKey) { + return (value: { + from: FilterItemParams; + to: FilterItemParams; + }) => { + props.onChange({ [fromKey]: value.from }); + props.onChange({ [toKey]: value.to }); + }; + } + return undefined; + }; + + if (props.availableFilters.length === 1) { + const filter = props.availableFilters[0]; + return ( + + ); + } + + return ; +};