-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
7 changed files
with
226 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { cn } from "@renderer/lib/utils" | ||
import type { FC, ReactNode } from "react" | ||
import { useEventCallback } from "usehooks-ts" | ||
|
||
import { useRadioContext, useRadioGroupValue } from "./context" | ||
|
||
export const Radio: FC< | ||
React.DetailedHTMLProps< | ||
React.InputHTMLAttributes<HTMLInputElement>, | ||
HTMLInputElement | ||
> & { | ||
label: ReactNode | ||
wrapperClassName?: string | ||
} | ||
> = (props) => { | ||
const { id, label, className, wrapperClassName, value, onChange, ...rest } = | ||
props | ||
const { groupId, onChange: ctxOnChange } = useRadioContext() || {} | ||
|
||
const ctxValue = useRadioGroupValue() | ||
|
||
const handleChange: React.ChangeEventHandler<HTMLInputElement> = | ||
useEventCallback((e) => { | ||
ctxOnChange?.(e.target.value) | ||
onChange?.(e) | ||
}) | ||
return ( | ||
<div className={cn("flex items-center gap-2", wrapperClassName)}> | ||
<input | ||
{...rest} | ||
type="radio" | ||
id={groupId || id} | ||
className={cn( | ||
"radio radio-primary radio-sm disabled:radio-current disabled:cursor-not-allowed disabled:text-theme-disabled", | ||
className, | ||
)} | ||
value={value} | ||
checked={ctxValue === value} | ||
onChange={handleChange} | ||
/> | ||
|
||
<label | ||
className={cn( | ||
rest.disabled ? "text-theme-disabled" : "", | ||
)} | ||
htmlFor={groupId || id} | ||
> | ||
{label} | ||
</label> | ||
</div> | ||
) | ||
} |
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,33 @@ | ||
import { useId, useMemo, useRef, useState } from "react" | ||
|
||
import { RadioGroupContextProvider, RadioGroupValueProvider } from "./context" | ||
|
||
export const RadioGroup: Component<{ | ||
value?: string | ||
onValueChange?: (value: string) => void | ||
}> = (props) => { | ||
const id = useId() | ||
const { onValueChange, value } = props | ||
|
||
const stableOnValueChange = useRef(onValueChange).current | ||
|
||
const [currentValue, setCurrentValue] = useState(value) | ||
return ( | ||
<RadioGroupContextProvider | ||
value={useMemo( | ||
() => ({ | ||
groupId: id, | ||
onChange(value) { | ||
setCurrentValue(value) | ||
stableOnValueChange?.(value) | ||
}, | ||
}), | ||
[id, stableOnValueChange], | ||
)} | ||
> | ||
<RadioGroupValueProvider value={currentValue}> | ||
{props.children} | ||
</RadioGroupValueProvider> | ||
</RadioGroupContextProvider> | ||
) | ||
} |
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,17 @@ | ||
import { createContext, useContext } from "react" | ||
|
||
interface RadioContext { | ||
groupId: string | ||
onChange: (value: string) => void | ||
|
||
} | ||
const RadioContext = createContext<RadioContext>(null!) | ||
|
||
export const useRadioContext = () => useContext(RadioContext) | ||
|
||
export const RadioGroupContextProvider = RadioContext.Provider | ||
type RadioGroupValue = string | undefined | ||
|
||
const RadioGroupValueContext = createContext<RadioGroupValue>("") | ||
export const RadioGroupValueProvider = RadioGroupValueContext.Provider | ||
export const useRadioGroupValue = () => useContext(RadioGroupValueContext) |
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,3 @@ | ||
export * from "./context" | ||
export * from "./Radio" | ||
export * from "./RadioGroup" |
Oops, something went wrong.