-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d82df5a
commit 40ca5be
Showing
3 changed files
with
55 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
packages/kamalion-ui/src/components/Input/InputCheckbox.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from "react"; | ||
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"; | ||
import { FaCheck } from "react-icons/fa6"; | ||
import { cn } from "../.."; | ||
import { useFormContext } from "react-hook-form"; | ||
|
||
export type InputCheckboxState = CheckboxPrimitive.CheckedState; | ||
|
||
type InputCheckboxProps = { | ||
noControl?: boolean; | ||
}; | ||
|
||
const InputCheckbox = React.forwardRef< | ||
React.ElementRef<typeof CheckboxPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root> & InputCheckboxProps | ||
>(({ className, noControl, name, checked, onCheckedChange, ...props }, ref) => { | ||
const formContext = useFormContext(); | ||
|
||
if (!name) return null; | ||
|
||
const uncontrolled = !formContext || !formContext.control || noControl; | ||
|
||
checked = uncontrolled ? checked : formContext.watch(name); | ||
|
||
return ( | ||
<CheckboxPrimitive.Root | ||
ref={ref} | ||
className={cn( | ||
"peer h-5 w-5 mt-[1px] shrink-0 rounded border border-primary", | ||
"ring-offset-background focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", | ||
"disabled:cursor-not-allowed disabled:opacity-50", | ||
"data-[state=checked]:border-emerald-400 data-[state=checked]:bg-emerald-400 data-[state=checked]:text-white", | ||
"transition-all", | ||
className, | ||
)} | ||
{...props} | ||
id={name} | ||
checked={checked} | ||
onCheckedChange={(val) => { | ||
uncontrolled ? onCheckedChange!(val) : formContext.setValue(name, val); | ||
}} | ||
> | ||
<CheckboxPrimitive.Indicator className={cn("flex items-center justify-center text-current")}> | ||
<FaCheck className="h-3 w-3" /> | ||
</CheckboxPrimitive.Indicator> | ||
</CheckboxPrimitive.Root> | ||
); | ||
}); | ||
|
||
InputCheckbox.displayName = "InputCheckbox"; | ||
|
||
export { InputCheckbox }; |
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