Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add `<Checkbox>` and `<RadioGroup>` components

Damian Legawiec4b2e6a85d81e
diff --git a/packages/admin/e2e/promotions.spec.ts b/packages/admin/e2e/promotions.spec.ts
index 9eb2ec25dc..2fa96df05d 100644
--- a/packages/admin/e2e/promotions.spec.ts
+++ b/packages/admin/e2e/promotions.spec.ts
@@ -176,7 +176,9 @@ test.describe('promotions', () => {
await expect(page.getByRole('heading', { name: /^product\(s\)$/i })).toBeVisible({
timeout: 5_000,
})
- await page.getByRole('button', { name: /all of these products/i }).click()
+ // The match-policy options render as `<FieldLabel>` cards wrapping a
+ // Base UI radio. Click the visible card title to pick "all".
+ await page.getByText(/^all of these products$/i).click()
await pickAutocompleteOption(page, /search products by name/i, FIXTURE_PROMO_PRODUCT)
await saveEditor(page)
@@ -198,7 +200,7 @@ test.describe('promotions', () => {
await expect(page.getByRole('heading', { name: /^categories$/i })).toBeVisible({
timeout: 5_000,
})
- await page.getByRole('button', { name: /any of these categories/i }).click()
+ await page.getByText(/^any of these categories$/i).click()
await pickAutocompleteOption(page, /search categories/i, FIXTURE_PROMO_TAXON)
await saveEditor(page)
diff --git a/packages/admin/src/components/spree/address-form-dialog.tsx b/packages/admin/src/components/spree/address-form-dialog.tsx
index e245573aa8..1708eb4139 100644
--- a/packages/admin/src/components/spree/address-form-dialog.tsx
+++ b/packages/admin/src/components/spree/address-form-dialog.tsx
@@ -6,6 +6,7 @@ import {
useCountryStates,
} from '@/components/spree/country-state-fields'
import { Button } from '@/components/ui/button'
+import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field'
import { Input } from '@/components/ui/input'
import {
@@ -177,9 +178,12 @@ export function AddressFormDialog({
{showDefaultFlags && (
<>
<Field>
- <label className="flex items-center gap-2 text-sm">
- <input
- type="checkbox"
+ <label
+ htmlFor="addr-default-billing"
+ className="flex items-center gap-2 text-sm"
+ >
+ <Checkbox
+ id="addr-default-billing"
name="is_default_billing"
defaultChecked={address?.is_default_billing}
/>
@@ -187,9 +191,12 @@ export function AddressFormDialog({
</label>
</Field>
<Field>
- <label className="flex items-center gap-2 text-sm">
- <input
- type="checkbox"
+ <label
+ htmlFor="addr-default-shipping"
+ className="flex items-center gap-2 text-sm"
+ >
+ <Checkbox
+ id="addr-default-shipping"
name="is_default_shipping"
defaultChecked={address?.is_default_shipping}
/>
diff --git a/packages/admin/src/components/spree/export-button.tsx b/packages/admin/src/components/spree/export-button.tsx
index 5e3eaa686e..a1e0c9c7b3 100644
--- a/packages/admin/src/components/spree/export-button.tsx
+++ b/packages/admin/src/components/spree/export-button.tsx
@@ -12,9 +12,16 @@ import {
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
+import {
+ Field,
+ FieldContent,
+ FieldDescription,
+ FieldLabel,
+ FieldTitle,
+} from '@/components/ui/field'
+import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { useExport } from '@/hooks/use-export'
import { filtersToRansack } from '@/lib/filters-to-ransack'
-import { cn } from '@/lib/utils'
interface ExportButtonProps extends ResourceActionsContext {
/** Which dataset to export. Server validates against `Spree::Export.available_types`. */
@@ -77,21 +84,24 @@ export function ExportButton({
</DialogDescription>
</DialogHeader>
- <DialogBody className="grid gap-3">
- <RadioOption
- icon={<FilterIcon className="size-4" />}
- title="Current filter"
- description={describeFiltered(hasActiveFilter, totalCount)}
- selected={selection === 'filtered'}
- onSelect={() => setSelection('filtered')}
- />
- <RadioOption
- icon={<GlobeIcon className="size-4" />}
- title="All records"
- description="Ignore filters and export everything in this store."
- selected={selection === 'all'}
- onSelect={() => setSelection('all')}
- />
+ <DialogBody>
+ <RadioGroup
+ value={selection}
+ onValueChange={(value) => setSelection(value as Selection)}
+ >
+ <ChoiceCard
+ value="filtered"
+ icon={<FilterIcon className="size-4" />}
+ title="Current filter"
+ description={describeFiltered(hasActiveFilter, totalCount)}
+ />
+ <ChoiceCard
+ value="all"
+ icon={<GlobeIcon className="size-4" />}
+ title="All records"
+ description="Ignore filters and export everything in this store."
+ />
+ </RadioGroup>
</DialogBody>
<DialogFooter>
@@ -115,47 +125,27 @@ function describeFiltered(hasActiveFilter: boolean, totalCount: number | undefin
return `Export the ${totalCount.toLocaleString()} ${noun} matching your current filter.`
}
-function RadioOption({
+function ChoiceCard({
+ value,
icon,
title,
description,
- selected,
- onSelect,
}: {
- icon: React.ReactNode
+ value: string
+ icon?: React.ReactNode
title: string
description: string
- selected: boolean
- onSelect: () => void
}) {
return (
- <button
- type="button"
- onClick={onSelect}
- aria-pressed={selected}
- className={cn(
- 'flex items-start gap-3 rounded-lg border p-3 text-left transition-colors',
- 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
- selected
- ? 'border-blue-300 bg-blue-50 dark:border-blue-500/60 dark:bg-blue-500/10'
- : 'border-border hover:bg-muted/50',
- )}
- >
- <span className="mt-0.5 text-muted-foreground">{icon}</span>
- <span className="flex-1">
- <span className="block text-sm font-medium">{title}</span>
- <span className="block text-xs text-muted-foreground">{description}</span>
- </span>
- <span
- className={cn(
- 'mt-1 size-4 shrink-0 rounded-full border',
- selected ? 'border-blue-600 bg-blue-600' : 'border-border',
- )}
- >
- {selected && (
- <span className="block size-full rounded-full ring-2 ring-background ring-inset" />
- )}
- </span>
- </button>
+ <FieldLabel>
+ <Field orientation="horizontal">
+ {icon && <span className="mt-0.5 text-muted-foreground">{icon}</span>}
+ <FieldContent>
+ <FieldTitle>{title}</FieldTitle>
+ <FieldDescription>{description}</FieldDescription>
+ </FieldContent>
+ <RadioGroupItem value={value} />
+ </Field>
+ </FieldLabel>
)
}
diff --git a/packages/admin/src/components/spree/promotion-editors/match-policy-picker.tsx b/packages/admin/src/components/spree/promotion-editors/match-policy-picker.tsx
index 07b8b29928..d2d6f71133 100644
--- a/packages/admin/src/components/spree/promotion-editors/match-policy-picker.tsx
+++ b/packages/admin/src/components/spree/promotion-editors/match-policy-picker.tsx
@@ -1,5 +1,12 @@
-import { FieldGroup, FieldLabel } from '@/components/ui/field'
-import { cn } from '@/lib/utils'
+import {
+ Field,
+ FieldContent,
+ FieldDescription,
+ FieldGroup,
+ FieldLabel,
+ FieldTitle,
+} from '@/components/ui/field'
+import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
export interface MatchPolicyOption<TValue extends string> {
value: TValue
@@ -9,9 +16,9 @@ export interface MatchPolicyOption<TValue extends string> {
/**
* Card-style toggle group used by promotion rules with a `match_policy`
- * preference (Product → any/all/none, Taxon → any/all). Each option
- * is a labelled card with a description; visually emphasises the
- * selected one with a primary border + tinted background.
+ * preference (Product → any/all/none, Taxon → any/all). Each option is
+ * a labelled card with a description; the selected one tints via the
+ * shared `FieldLabel` choice-card styling.
*/
export function MatchPolicyPicker<TValue extends string>({
label = 'Match policy',
@@ -27,28 +34,19 @@ export function MatchPolicyPicker<TValue extends string>({
return (
<FieldGroup>
<FieldLabel>{label}</FieldLabel>
- <div className="grid gap-2">
- {policies.map((policy) => {
- const active = value === policy.value
- return (
- <button
- key={policy.value}
- type="button"
- onClick={() => onChange(policy.value)}
- className={cn(
- 'flex flex-col items-start gap-0.5 rounded-lg border p-3 text-left transition-colors',
- active
- ? 'border-blue-300 bg-blue-50 dark:border-blue-500/60 dark:bg-blue-500/10'
- : 'hover:bg-muted',
- )}
- aria-pressed={active}
- >
- <span className="text-sm font-medium">{policy.label}</span>
- <span className="text-xs text-muted-foreground">{policy.description}</span>
- </button>
- )
- })}
- </div>
+ <RadioGroup value={value} onValueChange={(next) => onChange(next as TValue)}>
+ {policies.map((policy) => (
+ <FieldLabel key={policy.value}>
+ <Field orientation="horizontal">
+ <FieldContent>
+ <FieldTitle>{policy.label}</FieldTitle>
+ <FieldDescription>{policy.description}</FieldDescription>
+ </FieldContent>
+ <RadioGroupItem value={policy.value} />
+ </Field>
+ </FieldLabel>
+ ))}
+ </RadioGroup>
</FieldGroup>
)
}
diff --git a/packages/admin/src/components/ui/checkbox.tsx b/packages/admin/src/components/ui/checkbox.tsx
new file mode 100644
index 0000000000..b8c4adc42c
--- /dev/null
+++ b/packages/admin/src/components/ui/checkbox.tsx
@@ -0,0 +1,50 @@
+import { Checkbox as CheckboxPrimitive } from '@base-ui/react/checkbox'
+import { CheckIcon, MinusIcon } from 'lucide-react'
+import type * as React from 'react'
+import { cn } from '@/lib/utils'
+
+function Checkbox({
+ className,
+ checked,
+ defaultChecked,
+ onCheckedChange,
+ indeterminate,
+ disabled,
+ name,
+ ...props
+}: Omit<React.ComponentProps<typeof CheckboxPrimitive.Root>, 'children' | 'onCheckedChange'> & {
+ onCheckedChange?: (checked: boolean) => void
+}) {
+ return (
+ <CheckboxPrimitive.Root
+ data-slot="checkbox"
+ checked={checked}
+ defaultChecked={defaultChecked}
+ onCheckedChange={onCheckedChange ? (next) => onCheckedChange(next) : undefined}
+ indeterminate={indeterminate}
+ disabled={disabled}
+ name={name}
+ className={cn(
+ 'peer relative inline-block size-4 shrink-0 rounded-[4px] border border-input align-[-3px] shadow-xs outline-none transition-shadow',
+ 'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50',
+ 'disabled:cursor-not-allowed disabled:opacity-50',
+ 'aria-invalid:border-destructive aria-invalid:ring-destructive/20',
+ 'data-[checked]:border-primary data-[checked]:bg-primary data-[checked]:text-primary-foreground',
+ 'data-[indeterminate]:border-primary data-[indeterminate]:bg-primary data-[indeterminate]:text-primary-foreground',
+ 'dark:bg-input/30 dark:aria-invalid:ring-destructive/40 dark:data-[checked]:bg-primary',
+ className,
+ )}
+ {...props}
+ >
+ <CheckboxPrimitive.Indicator
+ keepMounted
+ data-slot="checkbox-indicator"
+ className="absolute inset-0 flex items-center justify-center text-current transition-none data-[unchecked]:hidden"
+ >
+ {indeterminate ? <MinusIcon className="size-3.5" /> : <CheckIcon className="size-3.5" />}
+ </CheckboxPrimitive.Indicator>
+ </CheckboxPrimitive.Root>
+ )
+}
+
+export { Checkbox }
diff --git a/packages/admin/src/components/ui/radio-group.tsx b/packages/admin/src/components/ui/radio-group.tsx
new file mode 100644
index 0000000000..3be66d4035
--- /dev/null
+++ b/packages/admin/src/components/ui/radio-group.tsx
@@ -0,0 +1,42 @@
+import { Radio as RadioPrimitive } from '@base-ui/react/radio'
+import { RadioGroup as RadioGroupPrimitive } from '@base-ui/react/radio-group'
+import type * as React from 'react'
+import { cn } from '@/lib/utils'
+
+function RadioGroup({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive>) {
+ return (
+ <RadioGroupPrimitive
+ data-slot="radio-group"
+ className={cn('grid w-full gap-2', className)}
+ {...props}
+ />
+ )
+}
+
+function RadioGroupItem({ className, ...props }: React.ComponentProps<typeof RadioPrimitive.Root>) {
+ return (
+ <RadioPrimitive.Root
+ data-slot="radio-group-item"
+ className={cn(
+ 'peer relative inline-block size-4 shrink-0 rounded-full border border-input align-[-3px] shadow-xs outline-none transition-shadow',
+ 'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50',
+ 'disabled:cursor-not-allowed disabled:opacity-50',
+ 'aria-invalid:border-destructive aria-invalid:ring-destructive/20',
+ 'data-[checked]:border-primary data-[checked]:bg-primary data-[checked]:text-primary-foreground',
+ 'dark:bg-input/30 dark:aria-invalid:ring-destructive/40 dark:data-[checked]:bg-primary',
+ className,
+ )}
+ {...props}
+ >
+ <RadioPrimitive.Indicator
+ keepMounted
+ data-slot="radio-group-indicator"
+ className="absolute inset-0 flex items-center justify-center text-current transition-none data-[unchecked]:hidden"
+ >
+ <span className="size-1.5 rounded-full bg-current" />
+ </RadioPrimitive.Indicator>
+ </RadioPrimitive.Root>
+ )
+}
+
+export { RadioGroup, RadioGroupItem }
diff --git a/packages/admin/src/routes/_authenticated/$storeId/customers/$customerId.tsx b/packages/admin/src/routes/_authenticated/$storeId/customers/$customerId.tsx
index f906ab9b65..8670919e16 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/customers/$customerId.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/customers/$customerId.tsx
@@ -25,6 +25,7 @@ import { TagCombobox } from '@/components/spree/tag-combobox'
import { Badge, StatusBadge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Card, CardAction, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
+import { Checkbox } from '@/components/ui/checkbox'
import {
Dialog,
DialogBody,
@@ -296,9 +297,12 @@ function EditProfileSheet({
<TagCombobox taggableType="Spree::User" value={tags} onChange={setTags} />
</Field>
<Field>
- <label className="flex items-center gap-2 text-sm">
- <input
- type="checkbox"
+ <label
+ htmlFor="accepts_email_marketing"
+ className="flex items-center gap-2 text-sm"
+ >
+ <Checkbox
+ id="accepts_email_marketing"
name="accepts_email_marketing"
defaultChecked={customer.accepts_email_marketing}
/>
diff --git a/packages/admin/src/routes/_authenticated/$storeId/settings/api-keys.tsx b/packages/admin/src/routes/_authenticated/$storeId/settings/api-keys.tsx
index 06b6913ba6..73322d441c 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/settings/api-keys.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/settings/api-keys.tsx
@@ -20,6 +20,7 @@ import { RelativeTime } from '@/components/spree/relative-time'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
+import { Checkbox } from '@/components/ui/checkbox'
import {
Table,
TableBody,
@@ -44,9 +45,16 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
-import { Field, FieldLabel } from '@/components/ui/field'
+import {
+ Field,
+ FieldContent,
+ FieldDescription,
+ FieldLabel,
+ FieldTitle,
+} from '@/components/ui/field'
import { Input } from '@/components/ui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
+import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import {
Sheet,
SheetContent,
@@ -468,22 +476,18 @@ function CreateApiKeyDialog({
name="key_type"
control={form.control}
render={({ field }) => (
- <div className="grid grid-cols-2 gap-2">
- <KeyTypeOption
+ <RadioGroup value={field.value} onValueChange={(value) => field.onChange(value)}>
+ <KeyTypeChoice
value="secret"
- label="Secret"
+ title="Secret"
description="Admin API · server-to-server"
- selected={field.value === 'secret'}
- onSelect={() => field.onChange('secret')}
/>
- <KeyTypeOption
+ <KeyTypeChoice
value="publishable"
- label="Publishable"
+ title="Publishable"
description="Storefront · safe to expose"
- selected={field.value === 'publishable'}
- onSelect={() => field.onChange('publishable')}
/>
- </div>
+ </RadioGroup>
)}
/>
</Field>
@@ -524,32 +528,25 @@ function CreateApiKeyDialog({
)
}
-function KeyTypeOption({
- label,
+function KeyTypeChoice({
+ value,
+ title,
description,
- selected,
- onSelect,
}: {
value: 'publishable' | 'secret'
- label: string
+ title: string
description: string
- selected: boolean
- onSelect: () => void
}) {
return (
- <button
- type="button"
- onClick={onSelect}
- className={cn(
- 'flex flex-col items-start gap-1 rounded-lg border p-3 text-left transition-colors',
- selected
- ? 'border-blue-300 bg-blue-500/5 text-blue-600 dark:border-blue-600/75'
- : 'border-border hover:border-foreground/30',
- )}
- >
- <span className="font-medium">{label}</span>
- <span className="text-xs text-muted-foreground">{description}</span>
- </button>
+ <FieldLabel>
+ <Field orientation="horizontal">
+ <FieldContent>
+ <FieldTitle>{title}</FieldTitle>
+ <FieldDescription>{description}</FieldDescription>
+ </FieldContent>
+ <RadioGroupItem value={value} />
+ </Field>
+ </FieldLabel>
)
}
@@ -578,23 +575,17 @@ function ScopePicker({ value, onChange }: { value: string[]; onChange: (next: st
{/* Quick access: write_all / read_all toggles. Selecting one blocks the
per-resource grid because the catch-all already covers it. */}
<div className="flex flex-col gap-2 border-b border-border bg-muted/30 p-3">
- <label className="flex cursor-pointer items-center gap-2 text-sm">
- <input
- type="checkbox"
- checked={hasWriteAll}
- onChange={(e) => setAllWrite(e.target.checked)}
- className="size-4 rounded border-border accent-primary"
- />
+ <label htmlFor="scope-write-all" className="flex cursor-pointer items-center gap-2 text-sm">
+ <Checkbox id="scope-write-all" checked={hasWriteAll} onCheckedChange={setAllWrite} />
<span className="font-medium">Full access (write_all)</span>
<span className="text-xs text-muted-foreground">— read + write on every resource</span>
</label>
- <label className="flex cursor-pointer items-center gap-2 text-sm">
- <input
- type="checkbox"
+ <label htmlFor="scope-read-all" className="flex cursor-pointer items-center gap-2 text-sm">
+ <Checkbox
+ id="scope-read-all"
checked={hasReadAll}
- onChange={(e) => setAllRead(e.target.checked)}
+ onCheckedChange={setAllRead}
disabled={hasWriteAll}
- className="size-4 rounded border-border accent-primary"
/>
<span className="font-medium">Read all (read_all)</span>
<span className="text-xs text-muted-foreground">— read on every resource</span>
@@ -656,25 +647,23 @@ function ScopeRow({
return (
<>
<span>{label}</span>
- <input
- type="checkbox"
+ <Checkbox
checked={hasRead}
- onChange={onToggleRead}
+ onCheckedChange={onToggleRead}
// Checking write implies read; reflect that here so the UI doesn't
// look out of sync with what the server will enforce.
disabled={hasWrite}
aria-label={`Read ${label}`}
- className="size-4 justify-self-center rounded border-border accent-primary"
+ className="justify-self-center"
/>
{readOnly ? (
<span className="text-xs text-muted-foreground justify-self-center">—</span>
) : (
- <input
- type="checkbox"
+ <Checkbox
checked={hasWrite}
- onChange={onToggleWrite}
+ onCheckedChange={onToggleWrite}
aria-label={`Write ${label}`}
- className="size-4 justify-self-center rounded border-border accent-primary"
+ className="justify-self-center"
/>
)}
</>
diff --git a/packages/admin/src/routes/_authenticated/$storeId/settings/staff.tsx b/packages/admin/src/routes/_authenticated/$storeId/settings/staff.tsx
index 323bc4c77f..3661b3e0c0 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/settings/staff.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/settings/staff.tsx
@@ -22,6 +22,7 @@ import { Avatar, AvatarFallback } from '@/components/ui/avatar'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
+import { Checkbox } from '@/components/ui/checkbox'
import {
Table,
TableBody,
@@ -643,17 +644,14 @@ function RoleCheckboxGroup({
<div className="flex flex-col gap-1.5 rounded-md border border-border p-3">
{roles.map((role) => {
const checked = value.includes(role.id)
+ const id = `role-${role.id}`
return (
<label
key={role.id}
+ htmlFor={id}
className="flex cursor-pointer items-center gap-2 text-sm capitalize"
>
- <input
- type="checkbox"
- checked={checked}
- onChange={() => toggle(role.id)}
- className="size-4 rounded border-border accent-primary"
- />
+ <Checkbox id={id} checked={checked} onCheckedChange={() => toggle(role.id)} />
{role.name}
</label>
)