diff --git a/front/src/components/table/table-header/DropdownButton.tsx b/front/src/components/table/table-header/DropdownButton.tsx index df7c1f952e..75469b77e4 100644 --- a/front/src/components/table/table-header/DropdownButton.tsx +++ b/front/src/components/table/table-header/DropdownButton.tsx @@ -1,14 +1,14 @@ import styled from '@emotion/styled'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { useState, useRef } from 'react'; +import { useRef, ReactNode } from 'react'; import { useOutsideAlerter } from '../../../hooks/useOutsideAlerter'; import { modalBackground } from '../../../layout/styles/themes'; -import { SortType } from './SortAndFilterBar'; type OwnProps = { label: string; - options: Array; - onSortSelect?: (id: string) => void; + isActive: boolean; + children?: ReactNode; + isUnfolded?: boolean; + setIsUnfolded?: React.Dispatch>; }; const StyledDropdownButtonContainer = styled.div` @@ -20,6 +20,7 @@ const StyledDropdownButtonContainer = styled.div` type StyledDropdownButtonProps = { isUnfolded: boolean; + isActive: boolean; }; const StyledDropdownButton = styled.div` @@ -27,6 +28,7 @@ const StyledDropdownButton = styled.div` margin-left: ${(props) => props.theme.spacing(3)}; cursor: pointer; background: ${(props) => props.theme.primaryBackground}; + color: ${(props) => (props.isActive ? props.theme.blue : 'none')}; padding: ${(props) => props.theme.spacing(1)}; border-radius: 4px; filter: ${(props) => (props.isUnfolded ? 'brightness(0.95)' : 'none')}; @@ -85,15 +87,19 @@ const StyledIcon = styled.div` margin-right: ${(props) => props.theme.spacing(1)}; `; -function DropdownButton({ label, options, onSortSelect }: OwnProps) { - const [isUnfolded, setIsUnfolded] = useState(false); - +function DropdownButton({ + label, + isActive, + children, + isUnfolded = false, + setIsUnfolded, +}: OwnProps) { const onButtonClick = () => { - setIsUnfolded(!isUnfolded); + setIsUnfolded && setIsUnfolded(!isUnfolded); }; const onOutsideClick = () => { - setIsUnfolded(false); + setIsUnfolded && setIsUnfolded(false); }; const dropdownRef = useRef(null); @@ -101,31 +107,21 @@ function DropdownButton({ label, options, onSortSelect }: OwnProps) { return ( - + {label} - {isUnfolded && options.length > 0 && ( - - {options.map((option, index) => ( - { - setIsUnfolded(false); - if (onSortSelect) { - onSortSelect(option.id); - } - }} - > - - {option.icon && } - - {option.label} - - ))} - + {isUnfolded && ( + {children} )} ); } +DropdownButton.StyledDropdownItem = StyledDropdownItem; +DropdownButton.StyledIcon = StyledIcon; + export default DropdownButton; diff --git a/front/src/components/table/table-header/SortDropdownButton.tsx b/front/src/components/table/table-header/SortDropdownButton.tsx new file mode 100644 index 0000000000..35d1dcb2dd --- /dev/null +++ b/front/src/components/table/table-header/SortDropdownButton.tsx @@ -0,0 +1,50 @@ +import { useCallback, useState } from 'react'; +import DropdownButton from './DropdownButton'; +import { SortType } from './SortAndFilterBar'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; + +type OwnProps = { + sorts: SortType[]; + setSorts: (sorts: SortType[]) => void; + sortsAvailable: SortType[]; +}; + +export function SortDropdownButton({ + sortsAvailable, + setSorts, + sorts, +}: OwnProps) { + const [isUnfolded, setIsUnfolded] = useState(false); + + const onSortItemSelect = useCallback( + (sort: SortType) => { + const newSorts = [sort]; + setSorts(newSorts); + }, + [setSorts], + ); + + return ( + 0} + isUnfolded={isUnfolded} + setIsUnfolded={setIsUnfolded} + > + {sortsAvailable.map((option, index) => ( + { + setIsUnfolded(false); + onSortItemSelect(option); + }} + > + + {option.icon && } + + {option.label} + + ))} + + ); +} diff --git a/front/src/components/table/table-header/TableHeader.tsx b/front/src/components/table/table-header/TableHeader.tsx index 8c3077df0d..97f5a7384c 100644 --- a/front/src/components/table/table-header/TableHeader.tsx +++ b/front/src/components/table/table-header/TableHeader.tsx @@ -4,6 +4,7 @@ import DropdownButton from './DropdownButton'; import { IconProp } from '@fortawesome/fontawesome-svg-core'; import SortAndFilterBar, { SortType } from './SortAndFilterBar'; import { useCallback, useState } from 'react'; +import { SortDropdownButton } from './SortDropdownButton'; type OwnProps = { viewName: string; @@ -50,19 +51,12 @@ function TableHeader({ onSortsUpdate, sortsAvailable, }: OwnProps) { - const [sorts, setSorts] = useState([] as Array); + const [sorts, innerSetSorts] = useState([] as Array); - const onSortItemSelect = useCallback( - (sortId: string) => { - const newSorts = [ - { - label: 'Created at', - order: 'asc', - id: sortId, - } satisfies SortType, - ]; - setSorts(newSorts); - onSortsUpdate && onSortsUpdate(newSorts); + const setSorts = useCallback( + (sorts: SortType[]) => { + innerSetSorts(sorts); + onSortsUpdate && onSortsUpdate(sorts); }, [onSortsUpdate], ); @@ -70,7 +64,7 @@ function TableHeader({ const onSortItemUnSelect = useCallback( (sortId: string) => { const newSorts = [] as SortType[]; - setSorts(newSorts); + innerSetSorts(newSorts); onSortsUpdate && onSortsUpdate(newSorts); }, [onSortsUpdate], @@ -86,13 +80,14 @@ function TableHeader({ {viewName} - - + - + + {sorts.length > 0 && (