-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Add time limits report filters (#3392)
* feat: add reports filters * fix typo in ITimeReportTableByMemberProps
- Loading branch information
Showing
6 changed files
with
316 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 99 additions & 34 deletions
133
apps/web/app/[locale]/reports/weekly-limit/components/group-by-select.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,120 @@ | ||
import { Select, SelectTrigger, SelectValue, SelectContent, SelectGroup, SelectItem } from '@components/ui/select'; | ||
import { DottedLanguageObjectStringPaths, useTranslations } from 'next-intl'; | ||
import { useMemo, useState, useCallback } from 'react'; | ||
import { Fragment } from 'react'; | ||
import { Listbox, Transition } from '@headlessui/react'; | ||
import { CheckIcon, ChevronUpDownIcon } from '@heroicons/react/20/solid'; | ||
import { Badge } from '@components/ui/badge'; | ||
|
||
export type TGroupByOption = 'date' | 'week' | 'member'; | ||
|
||
interface IProps { | ||
onChange: (OPTION: TGroupByOption) => void; | ||
defaultValue: TGroupByOption; | ||
onChange: (options: TGroupByOption[]) => void; | ||
defaultValues: TGroupByOption[]; | ||
} | ||
|
||
/** | ||
* GroupBySelect component provides a dropdown selector for grouping data by day, week, or member. | ||
* [GroupBySelect] - A multi-select component that allows users to choose up to two options | ||
* from a predefined list ("date", "Week", "Member"). | ||
* | ||
* Rules enforced: | ||
* - Only two options can be selected at a time. | ||
* - "date" and "Week" cannot be selected together. | ||
* - At least one option must remain selected. | ||
* | ||
* @component | ||
* @param {IProps} props - The component props. | ||
* @param {(option: TGroupByOption) => void} props.onChange - Function to handle changes in the selected grouping option. | ||
* @param {TGroupByOption} props.defaultValue - The initial grouping option. | ||
* @param {Object} props - The properties of the component. | ||
* @param {TGroupByOption[]} props.defaultValues - Initial options selected when the component is rendered. | ||
* @param {Function} props.onChange - Callback function invoked when the selection changes. | ||
* | ||
* @returns {JSX.Element} A custom multi-select dropdown with badges representing selected items. | ||
* | ||
* @returns {JSX.Element} A dropdown for selecting a grouping option. | ||
*/ | ||
|
||
export type TGroupByOption = 'Day' | 'Week' | 'Member'; | ||
export function GroupBySelect(props: IProps) { | ||
const { onChange, defaultValue } = props; | ||
const options = useMemo<TGroupByOption[]>(() => ['Day', 'Week', 'Member'], []); | ||
const [selected, setSelected] = useState<TGroupByOption>(defaultValue); | ||
export function GroupBySelect({ defaultValues, onChange }: IProps) { | ||
const options = useMemo<TGroupByOption[]>(() => ['date', 'week', 'member'], []); | ||
const [selected, setSelected] = useState<TGroupByOption[]>(defaultValues); | ||
const t = useTranslations(); | ||
|
||
const handleChange = useCallback( | ||
(option: TGroupByOption) => { | ||
setSelected(option); | ||
onChange(option); | ||
(options: TGroupByOption[]) => { | ||
// Ensure 'date' and 'Week' cannot be selected together | ||
let updatedOptions = options; | ||
|
||
if (options.includes('date') && options.includes('week')) { | ||
// If 'date' is newly selected, remove 'Week' | ||
if (selected.includes('week')) { | ||
updatedOptions = options.filter((option) => option !== 'week'); | ||
} | ||
// If 'Week' is newly selected, remove 'date' | ||
else if (selected.includes('date')) { | ||
updatedOptions = options.filter((option) => option !== 'date'); | ||
} | ||
} | ||
|
||
setSelected(updatedOptions); | ||
onChange(updatedOptions); | ||
}, | ||
[onChange] | ||
[onChange, selected] | ||
); | ||
|
||
return ( | ||
<Select value={selected} onValueChange={handleChange}> | ||
<SelectTrigger className="w-36 overflow-hidden h-[2.2rem] text-clip border border-gray-200 dark:border-gray-700 bg-white dark:bg-dark--theme-light focus:ring-2 focus:ring-transparent"> | ||
<SelectValue placeholder="Group by" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
{options.map((option) => { | ||
return ( | ||
<SelectItem key={option} value={option}> | ||
<Listbox multiple value={selected} onChange={handleChange}> | ||
<div className="relative h-[2.2rem] w-[12rem]"> | ||
<Listbox.Button className="relative w-full h-full cursor-default rounded-lg bg-white py-2 pl-1 pr-6 text-left border focus:outline-none focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-white/75 focus-visible:ring-offset-2 focus-visible:ring-offset-primary sm:text-sm"> | ||
<div className=" items-center w-full h-full flex gap-1"> | ||
{selected.map((option) => ( | ||
<Badge | ||
key={option} | ||
className=" capitalize rounded flex gap-1 font-light" | ||
variant={'outline'} | ||
> | ||
{t( | ||
`common.${(option == 'Day' ? 'Date' : option).toUpperCase()}` as DottedLanguageObjectStringPaths | ||
`common.${(option == 'date' ? 'date' : option).toUpperCase()}` as DottedLanguageObjectStringPaths | ||
)} | ||
</Badge> | ||
))} | ||
</div> | ||
<span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2"> | ||
<ChevronUpDownIcon className="h-5 w-5 text-gray-400" aria-hidden="true" /> | ||
</span> | ||
</Listbox.Button> | ||
<Transition | ||
as={Fragment} | ||
leave="transition ease-in duration-100" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
> | ||
<Listbox.Options className="absolute mt-1 max-h-60 w-full z-[999] overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black/5 focus:outline-none sm:text-sm"> | ||
{options.map((option, index) => ( | ||
<Listbox.Option | ||
disabled={ | ||
// Prevents users from clearing all selections, ensuring at least one option is always selected. | ||
selected.includes(option) && selected.length == 1 | ||
} | ||
key={index} | ||
className={({ active }) => | ||
`relative cursor-default select-none py-2 pl-10 pr-4 ${ | ||
active ? 'bg-primary/10 text-primary' : 'text-gray-900' | ||
}` | ||
} | ||
value={option} | ||
> | ||
{({ selected }) => ( | ||
<> | ||
<span className={`block truncate ${selected ? 'font-medium' : 'font-normal'}`}> | ||
{option} | ||
</span> | ||
{selected ? ( | ||
<span className="absolute inset-y-0 left-0 flex items-center pl-3 text-primary"> | ||
<CheckIcon className="h-5 w-5" aria-hidden="true" /> | ||
</span> | ||
) : null} | ||
</> | ||
)} | ||
</SelectItem> | ||
); | ||
})} | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
</Listbox.Option> | ||
))} | ||
</Listbox.Options> | ||
</Transition> | ||
</div> | ||
</Listbox> | ||
); | ||
} |
Oops, something went wrong.