From 6d3f6c25847d088e4664c1b6ef3ff47cb7a67166 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 17 Jun 2024 19:16:20 +0800 Subject: [PATCH] feat: redesign radio (#69) Signed-off-by: Innei --- .../src/components/ui/radio-group.tsx | 37 ---- .../src/components/ui/radio-group/Radio.tsx | 52 +++++ .../components/ui/radio-group/RadioGroup.tsx | 33 ++++ .../src/components/ui/radio-group/context.ts | 17 ++ .../src/components/ui/radio-group/index.ts | 3 + .../src/modules/settings/action-card.tsx | 182 ++++++++++-------- src/renderer/src/styles/tailwind-extend.css | 28 ++- 7 files changed, 226 insertions(+), 126 deletions(-) delete mode 100644 src/renderer/src/components/ui/radio-group.tsx create mode 100644 src/renderer/src/components/ui/radio-group/Radio.tsx create mode 100644 src/renderer/src/components/ui/radio-group/RadioGroup.tsx create mode 100644 src/renderer/src/components/ui/radio-group/context.ts create mode 100644 src/renderer/src/components/ui/radio-group/index.ts diff --git a/src/renderer/src/components/ui/radio-group.tsx b/src/renderer/src/components/ui/radio-group.tsx deleted file mode 100644 index 31596b10d..000000000 --- a/src/renderer/src/components/ui/radio-group.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import * as RadioGroupPrimitive from "@radix-ui/react-radio-group" -import { cn } from "@renderer/lib/utils" -import { Circle } from "lucide-react" -import * as React from "react" - -const RadioGroup = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - -)) -RadioGroup.displayName = RadioGroupPrimitive.Root.displayName - -const RadioGroupItem = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - - - - - -)) -RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName - -export { RadioGroup, RadioGroupItem } diff --git a/src/renderer/src/components/ui/radio-group/Radio.tsx b/src/renderer/src/components/ui/radio-group/Radio.tsx new file mode 100644 index 000000000..7b81349d5 --- /dev/null +++ b/src/renderer/src/components/ui/radio-group/Radio.tsx @@ -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 + > & { + 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 = + useEventCallback((e) => { + ctxOnChange?.(e.target.value) + onChange?.(e) + }) + return ( +
+ + + +
+ ) +} diff --git a/src/renderer/src/components/ui/radio-group/RadioGroup.tsx b/src/renderer/src/components/ui/radio-group/RadioGroup.tsx new file mode 100644 index 000000000..65d6bf90c --- /dev/null +++ b/src/renderer/src/components/ui/radio-group/RadioGroup.tsx @@ -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 ( + ({ + groupId: id, + onChange(value) { + setCurrentValue(value) + stableOnValueChange?.(value) + }, + }), + [id, stableOnValueChange], + )} + > + + {props.children} + + + ) +} diff --git a/src/renderer/src/components/ui/radio-group/context.ts b/src/renderer/src/components/ui/radio-group/context.ts new file mode 100644 index 000000000..5c40666ae --- /dev/null +++ b/src/renderer/src/components/ui/radio-group/context.ts @@ -0,0 +1,17 @@ +import { createContext, useContext } from "react" + +interface RadioContext { + groupId: string + onChange: (value: string) => void + +} +const RadioContext = createContext(null!) + +export const useRadioContext = () => useContext(RadioContext) + +export const RadioGroupContextProvider = RadioContext.Provider +type RadioGroupValue = string | undefined + +const RadioGroupValueContext = createContext("") +export const RadioGroupValueProvider = RadioGroupValueContext.Provider +export const useRadioGroupValue = () => useContext(RadioGroupValueContext) diff --git a/src/renderer/src/components/ui/radio-group/index.ts b/src/renderer/src/components/ui/radio-group/index.ts new file mode 100644 index 000000000..d1e49febe --- /dev/null +++ b/src/renderer/src/components/ui/radio-group/index.ts @@ -0,0 +1,3 @@ +export * from "./context" +export * from "./Radio" +export * from "./RadioGroup" diff --git a/src/renderer/src/modules/settings/action-card.tsx b/src/renderer/src/modules/settings/action-card.tsx index 667d90ff7..46bcf3789 100644 --- a/src/renderer/src/modules/settings/action-card.tsx +++ b/src/renderer/src/modules/settings/action-card.tsx @@ -9,10 +9,8 @@ import { import { Divider } from "@renderer/components/ui/divider" import { Input } from "@renderer/components/ui/input" import { Label } from "@renderer/components/ui/label" -import { - RadioGroup, - RadioGroupItem, -} from "@renderer/components/ui/radio-group" +import { Radio } from "@renderer/components/ui/radio-group" +import { RadioGroup } from "@renderer/components/ui/radio-group/RadioGroup" import { Select, SelectContent, @@ -231,7 +229,9 @@ const OperationTableCell = ({ value?: Operation onValueChange?: (value: Operation) => void }) => { - const options = OperationOptions.filter((option) => option.types.includes(type)) + const options = OperationOptions.filter((option) => + option.types.includes(type), + ) return ( change("value", value)} + onValueChange={(value) => + change("value", value)} value={condition.value} > @@ -487,17 +482,20 @@ export function ActionCard({ open={!!data.result.rewriteRules} onOpenChange={(open) => { if (open) { - data.result.rewriteRules = [{ - from: "", - to: "", - }] + data.result.rewriteRules = [ + { + from: "", + to: "", + }, + ] } else { delete data.result.rewriteRules } onChange(data) }} > - {data.result.rewriteRules && data.result.rewriteRules.length > 0 && ( + {data.result.rewriteRules && + data.result.rewriteRules.length > 0 && ( <> @@ -508,45 +506,50 @@ export function ActionCard({ - {data.result.rewriteRules.map((rule, rewriteIdx) => { - const change = (key: string, value: string) => { - data.result.rewriteRules![rewriteIdx][key] = value - onChange(data) - } - return ( - - { - if ( - data.result.rewriteRules?.length === 1 - ) { - delete data.result.rewriteRules - } else { - data.result.rewriteRules?.splice( - rewriteIdx, - 1, - ) - } - onChange(data) - }} - /> - - change("from", e.target.value)} + {data.result.rewriteRules.map( + (rule, rewriteIdx) => { + const change = (key: string, value: string) => { + data.result.rewriteRules![rewriteIdx][key] = + value + onChange(data) + } + return ( + + { + if ( + data.result.rewriteRules?.length === 1 + ) { + delete data.result.rewriteRules + } else { + data.result.rewriteRules?.splice( + rewriteIdx, + 1, + ) + } + onChange(data) + }} /> - - - change("to", e.target.value)} - /> - - - ) - })} + + + change("from", e.target.value)} + /> + + + + change("to", e.target.value)} + /> + + + ) + }, + )}
- {data.result.blockRules && data.result.blockRules.length > 0 && ( + {data.result.blockRules && + data.result.blockRules.length > 0 && ( <> @@ -587,17 +591,23 @@ export function ActionCard({ data.result.blockRules![index][key] = value onChange(data) } - const type = EntryOptions.find( - (option) => option.value === rule.field, - )?.type || "text" + const type = + EntryOptions.find( + (option) => option.value === rule.field, + )?.type || "text" return ( { - if (data.result.blockRules?.length === 1) { + if ( + data.result.blockRules?.length === 1 + ) { delete data.result.blockRules } else { - data.result.blockRules?.splice(index, 1) + data.result.blockRules?.splice( + index, + 1, + ) } onChange(data) }} @@ -624,14 +634,16 @@ export function ActionCard({ change("operator", value)} + onValueChange={(value) => + change("operator", value)} /> change("value", e.target.value)} + onChange={(e) => + change("value", e.target.value)} /> diff --git a/src/renderer/src/styles/tailwind-extend.css b/src/renderer/src/styles/tailwind-extend.css index 44fc3b1b1..561ffdf09 100644 --- a/src/renderer/src/styles/tailwind-extend.css +++ b/src/renderer/src/styles/tailwind-extend.css @@ -53,12 +53,14 @@ .shadow-modal { @apply shadow-2xl shadow-stone-300 dark:shadow-stone-800; } +} +/* Radio styling */ +@layer components { .radio { flex-shrink: 0; - --chkbg: var(--bc); - height: 1.5rem; - width: 1.5rem; + height: 1rem; + width: 1rem; cursor: pointer; -webkit-appearance: none; -moz-appearance: none; @@ -66,12 +68,30 @@ border-radius: 9999px; border-width: 1px; border-color: var(--fallback-bc, theme(colors.theme.accent.DEFAULT)); - --tw-border-opacity: 0.2; + + --bc: theme(colors.accent.foreground); + --fallback-bc: var(--bc); + --chkbg: var(--bc); + --fallback-b1: theme(colors.background); + } + + .radio-current { + --chkbg: currentColor; + --fallback-bc: currentColor; } .radio-primary { --chkbg: theme(colors.theme.accent.DEFAULT); --fallback-bc: theme(colors.theme.accent.DEFAULT); + --fallback-b1: theme(colors.background); + } + + .radio-sm { + @apply size-[0.9rem]; + } + + .radio-md { + @apply size-[1.2rem]; } .radio:checked,