Skip to content

Commit

Permalink
add 'Apply' button for event filters
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkarimoff committed Jan 31, 2024
1 parent f5919dc commit 394b9a6
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { type Dispatch, type SetStateAction } from "react";
import { useState, type Dispatch, type SetStateAction } from "react";
import { Button } from "~/components/ui/button";
import { Checkbox } from "~/components/ui/checkbox";
import {
Expand All @@ -18,19 +18,21 @@ type TableFilterProps = {
};

const TableFilter = ({ eventTypes, setEventTypes }: TableFilterProps) => {
const [options, setOptions] = useState<EventType[]>(eventTypes);

const toggleOption = (option: string) => {
setEventTypes((prevState) =>
setOptions((prevState) =>
prevState.map((t) =>
t.text === option ? { ...t, isSelected: !t.isSelected } : t
)
);
};

const toggleAllOptions = (isSelected: boolean) => {
setEventTypes((prevState) => prevState.map((t) => ({ ...t, isSelected })));
setOptions((prevState) => prevState.map((t) => ({ ...t, isSelected })));
};

const isAllSelected = eventTypes.every((option) => option.isSelected);
const isAllSelected = options.every((option) => option.isSelected);

return (
<DropdownMenu>
Expand All @@ -53,18 +55,26 @@ const TableFilter = ({ eventTypes, setEventTypes }: TableFilterProps) => {
</label>
<DropdownMenuSeparator />

{eventTypes.map((type) => (
{options.map((option) => (
<label
key={type.text}
key={option.text}
className="text-sm flex items-center gap-3 pl-2 transition-colors hover:bg-muted p-1 rounded-md"
>
<Checkbox
checked={type.isSelected}
onCheckedChange={() => toggleOption(type.text)}
checked={option.isSelected}
onCheckedChange={() => toggleOption(option.text)}
/>
<span>{type.text}</span>
<span>{option.text}</span>
</label>
))}

<Button
onClick={() => setEventTypes(options)}
className="float-right"
size={"sm"}
>
Apply
</Button>
</div>
</DropdownMenuContent>
</DropdownMenu>
Expand Down

0 comments on commit 394b9a6

Please sign in to comment.