-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from kudos-ink/feat/checkbox-filter-good-first…
…-issue Feat/checkbox filter good first issue
Showing
10 changed files
with
108 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"use client"; | ||
import React from "react"; | ||
import { usePathname, useRouter, useSearchParams } from "next/navigation"; | ||
import { Checkbox } from "@nextui-org/checkbox"; | ||
import { createUrl } from "@/utils/url"; | ||
|
||
interface ICheckboxFilterProps { | ||
paramKey: string; | ||
placeholder: string; | ||
isSelected?: boolean; | ||
onSelect: (value: string | null) => void; | ||
} | ||
export const CheckboxFilter = ({ | ||
paramKey, | ||
placeholder, | ||
isSelected, | ||
onSelect, | ||
}: ICheckboxFilterProps) => { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const params = useSearchParams(); | ||
|
||
const handleValueChange = (value: boolean) => { | ||
const newValue = value ? value.toString() : null; | ||
onSelect(newValue); | ||
const newUrl = createUrl(paramKey, newValue, pathname, params); | ||
router.replace(newUrl, { scroll: false }); | ||
}; | ||
|
||
return ( | ||
<Checkbox | ||
classNames={{ | ||
wrapper: "before:border-default-200 before:border-medium", | ||
label: "whitespace-nowrap", | ||
}} | ||
isSelected={isSelected} | ||
onValueChange={handleValueChange} | ||
> | ||
{placeholder} | ||
</Checkbox> | ||
); | ||
}; | ||
|
||
export default CheckboxFilter; |
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
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
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,11 +1,21 @@ | ||
import { ReadonlyURLSearchParams } from "next/navigation"; | ||
|
||
export const createUrl = ( | ||
key: string, | ||
value: string | null | undefined, | ||
pathname: string, | ||
params: URLSearchParams | ReadonlyURLSearchParams, | ||
) => { | ||
const paramsString = params.toString(); | ||
const queryString = `${paramsString.length ? "?" : ""}${paramsString}`; | ||
const keyLowerCase = key.toLowerCase(); | ||
const searchParams = new URLSearchParams(params.toString()); | ||
if (value) { | ||
searchParams.set(keyLowerCase, value.toString()); | ||
} else { | ||
searchParams.delete(keyLowerCase); | ||
} | ||
|
||
return `${pathname}${queryString}`; | ||
const newParams = searchParams.toString(); | ||
const queryString = `${newParams.length ? "?" : ""}${newParams}`; | ||
const url = `${pathname}${queryString}`; | ||
return url; | ||
}; |