Skip to content

Commit

Permalink
fix: incorrectly behavior when click radio label (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs authored Jul 28, 2024
1 parent 2ef9806 commit 5ff0d3d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/renderer/src/components/ui/radio-group/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cn } from "@renderer/lib/utils"
import type { FC, ReactNode } from "react"
import { useId } from "react"
import { useEventCallback } from "usehooks-ts"

import { useRadioContext, useRadioGroupValue } from "./context"
Expand All @@ -15,7 +16,8 @@ export const Radio: FC<
> = (props) => {
const { id, label, className, wrapperClassName, value, onChange, ...rest } =
props
const { groupId, onChange: ctxOnChange } = useRadioContext() || {}
const { onChange: ctxOnChange } = useRadioContext() || {}
const fallbackId = useId()

const ctxValue = useRadioGroupValue()

Expand All @@ -29,7 +31,7 @@ export const Radio: FC<
<input
{...rest}
type="radio"
id={groupId || id}
id={id ?? fallbackId}
className={cn(
"radio radio-primary radio-sm disabled:radio-current disabled:cursor-not-allowed disabled:text-theme-disabled",
className,
Expand All @@ -43,7 +45,7 @@ export const Radio: FC<
className={cn(
rest.disabled ? "text-theme-disabled" : "",
)}
htmlFor={groupId || id}
htmlFor={id ?? fallbackId}
>
{label}
</label>
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/src/components/ui/radio-group/RadioCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cn } from "@renderer/lib/utils"
import type { FC, ReactNode } from "react"
import { useId } from "react"
import { useEventCallback } from "usehooks-ts"

import { useRadioContext, useRadioGroupValue } from "./context"
Expand All @@ -15,7 +16,8 @@ export const RadioCard: FC<
> = (props) => {
const { id, label, className, wrapperClassName, value, onChange, ...rest } =
props
const { groupId, onChange: ctxOnChange } = useRadioContext() || {}
const { onChange: ctxOnChange } = useRadioContext() || {}
const fallbackId = useId()

const ctxValue = useRadioGroupValue()

Expand All @@ -29,7 +31,7 @@ export const RadioCard: FC<

return (
<label
id={groupId || id}
htmlFor={id ?? fallbackId}
className={cn(
"flex cursor-pointer items-center rounded-md p-2",
"border",
Expand All @@ -41,7 +43,7 @@ export const RadioCard: FC<
)}
>
<input
id={id}
id={id ?? fallbackId}
type="radio"
className={cn("hidden size-0", className)}
value={value}
Expand Down
6 changes: 2 additions & 4 deletions src/renderer/src/components/ui/radio-group/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useId, useMemo, useRef, useState } from "react"
import { 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
Expand All @@ -16,13 +15,12 @@ export const RadioGroup: Component<{
<RadioGroupContextProvider
value={useMemo(
() => ({
groupId: id,
onChange(value) {
setCurrentValue(value)
stableOnValueChange?.(value)
},
}),
[id, stableOnValueChange],
[stableOnValueChange],
)}
>
<RadioGroupValueProvider value={currentValue}>
Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/components/ui/radio-group/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createContext, useContext } from "react"

interface RadioContext {
groupId: string
onChange: (value: string) => void

}
Expand Down

0 comments on commit 5ff0d3d

Please sign in to comment.