feat: redesign radio (#69)

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-06-17 19:16:20 +08:00
committed by GitHub
parent ba6184ddc3
commit 6d3f6c2584
7 changed files with 226 additions and 126 deletions
@@ -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<typeof RadioGroupPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>
>(({ className, ...props }, ref) => (
<RadioGroupPrimitive.Root
className={cn("grid gap-2", className)}
{...props}
ref={ref}
/>
))
RadioGroup.displayName = RadioGroupPrimitive.Root.displayName
const RadioGroupItem = React.forwardRef<
React.ElementRef<typeof RadioGroupPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>
>(({ className, ...props }, ref) => (
<RadioGroupPrimitive.Item
ref={ref}
className={cn(
"aspect-square size-4 rounded-full border border-primary text-primary ring-offset-background focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
{...props}
>
<RadioGroupPrimitive.Indicator className="flex items-center justify-center">
<Circle className="size-2.5 fill-current text-current" />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
))
RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName
export { RadioGroup, RadioGroupItem }
@@ -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>
)
}
@@ -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>
)
}
@@ -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)
@@ -0,0 +1,3 @@
export * from "./context"
export * from "./Radio"
export * from "./RadioGroup"
@@ -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 (
<TableCell size="sm">
<Select value={value} onValueChange={onValueChange}>
@@ -272,11 +272,7 @@ const SettingCollapsible = ({
</Collapsible>
)
const CommonSelectTrigger = ({
className,
}: {
className?: string
}) => (
const CommonSelectTrigger = ({ className }: { className?: string }) => (
<SelectTrigger className={cn("h-8", className)}>
<SelectValue />
</SelectTrigger>
@@ -321,26 +317,23 @@ export function ActionCard({
<CollapsibleContent className="mt-4 space-y-4">
<div className="space-y-3">
<p className="font-medium text-zinc-500">When feeds match…</p>
<RadioGroup
value={data.condition.length > 0 ? "filter" : "all"}
onValueChange={(value) => {
if (value === "all") {
data.condition = []
} else {
data.condition = [{}]
}
onChange(data)
}}
>
<div className="flex items-center space-x-2">
<RadioGroupItem value="all" id="all" />
<Label htmlFor="all">All followed feeds</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="filter" id="filter" />
<Label htmlFor="filter">Custom filter</Label>
</div>
</RadioGroup>
<div className="flex flex-col gap-2">
<RadioGroup
value={data.condition.length > 0 ? "filter" : "all"}
onValueChange={(value) => {
if (value === "all") {
data.condition = []
} else {
data.condition = [{}]
}
onChange(data)
}}
>
<Radio label="all" value="all" />
<Radio label="Custom filter" value="filter" />
</RadioGroup>
</div>
{data.condition.length > 0 && (
<div>
<Table>
@@ -357,9 +350,10 @@ export function ActionCard({
data.condition[conditionIdx][key] = value
onChange(data)
}
const type = FeedOptions.find(
(option) => option.value === condition.field,
)?.type || "text"
const type =
FeedOptions.find(
(option) => option.value === condition.field,
)?.type || "text"
return (
<TableRow key={conditionIdx}>
<DeleteTableCell
@@ -396,7 +390,8 @@ export function ActionCard({
<TableCell size="sm">
{type === "view" ? (
<Select
onValueChange={(value) => change("value", value)}
onValueChange={(value) =>
change("value", value)}
value={condition.value}
>
<CommonSelectTrigger />
@@ -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 && (
<>
<Table>
<TableHeader>
@@ -508,45 +506,50 @@ export function ActionCard({
</TableRow>
</TableHeader>
<TableBody>
{data.result.rewriteRules.map((rule, rewriteIdx) => {
const change = (key: string, value: string) => {
data.result.rewriteRules![rewriteIdx][key] = value
onChange(data)
}
return (
<TableRow key={rewriteIdx}>
<DeleteTableCell
onClick={() => {
if (
data.result.rewriteRules?.length === 1
) {
delete data.result.rewriteRules
} else {
data.result.rewriteRules?.splice(
rewriteIdx,
1,
)
}
onChange(data)
}}
/>
<TableCell size="sm">
<Input
value={rule.from}
className="h-8"
onChange={(e) => 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 (
<TableRow key={rewriteIdx}>
<DeleteTableCell
onClick={() => {
if (
data.result.rewriteRules?.length === 1
) {
delete data.result.rewriteRules
} else {
data.result.rewriteRules?.splice(
rewriteIdx,
1,
)
}
onChange(data)
}}
/>
</TableCell>
<TableCell size="sm">
<Input
value={rule.to}
className="h-8"
onChange={(e) => change("to", e.target.value)}
/>
</TableCell>
</TableRow>
)
})}
<TableCell size="sm">
<Input
value={rule.from}
className="h-8"
onChange={(e) =>
change("from", e.target.value)}
/>
</TableCell>
<TableCell size="sm">
<Input
value={rule.to}
className="h-8"
onChange={(e) =>
change("to", e.target.value)}
/>
</TableCell>
</TableRow>
)
},
)}
</TableBody>
</Table>
<AddTableRow
@@ -574,7 +577,8 @@ export function ActionCard({
onChange(data)
}}
>
{data.result.blockRules && data.result.blockRules.length > 0 && (
{data.result.blockRules &&
data.result.blockRules.length > 0 && (
<>
<Table>
<FieldTableHeader />
@@ -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 (
<TableRow key={index}>
<DeleteTableCell
onClick={() => {
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({
<OperationTableCell
type={type}
value={rule.operator}
onValueChange={(value) => change("operator", value)}
onValueChange={(value) =>
change("operator", value)}
/>
<TableCell size="sm">
<Input
type={type}
value={rule.value}
className="h-8"
onChange={(e) => change("value", e.target.value)}
onChange={(e) =>
change("value", e.target.value)}
/>
</TableCell>
</TableRow>
+24 -4
View File
@@ -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,