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 <ActiveBadge> component; unify boolean-state cells in admin tables

Damian Legawiec988b5485bd1a
diff --git a/packages/admin/src/components/ui/badge.tsx b/packages/admin/src/components/ui/badge.tsx
index 1b5267300c..ee2c000d24 100644
--- a/packages/admin/src/components/ui/badge.tsx
+++ b/packages/admin/src/components/ui/badge.tsx
@@ -1,4 +1,5 @@
import { cva, type VariantProps } from 'class-variance-authority'
+import { CheckIcon } from 'lucide-react'
import * as React from 'react'
import { Slot } from '@/components/ui/slot'
import { cn } from '@/lib/utils'
@@ -91,4 +92,52 @@ function StatusBadge({ status, className }: { status: string; className?: string
)
}
-export { Badge, badgeVariants, StatusBadge }
+/**
+ * Boolean-state badge — mirrors the Rails admin's `active_badge` helper.
+ *
+ * Renders a `success` (check + label) badge when `active`, an `outline`
+ * badge with the inactive label otherwise. Use it for "Yes/No" or
+ * "Enabled/Disabled" cells in tables and detail rows.
+ *
+ * @example Default Yes/No
+ * <ActiveBadge active={user.confirmed} />
+ *
+ * @example Custom labels
+ * <ActiveBadge active={sl.pickup_enabled} activeLabel="Enabled" inactiveLabel="Disabled" />
+ *
+ * @example Hide inactive (renders a muted dash instead — matches the
+ * pre-existing `<Badge> : <span>—</span>` pattern in stock-locations).
+ * <ActiveBadge active={sl.pickup_enabled} activeLabel="Enabled" dashWhenInactive />
+ */
+function ActiveBadge({
+ active,
+ activeLabel = 'Yes',
+ inactiveLabel = 'No',
+ dashWhenInactive = false,
+ className,
+}: {
+ active: boolean | null | undefined
+ activeLabel?: string
+ inactiveLabel?: string
+ dashWhenInactive?: boolean
+ className?: string
+}) {
+ if (active) {
+ return (
+ <Badge variant="outline" className={className}>
+ <CheckIcon />
+ {activeLabel}
+ </Badge>
+ )
+ }
+ if (dashWhenInactive) {
+ return <span className={cn('text-muted-foreground', className)}>—</span>
+ }
+ return (
+ <Badge variant="ghost" className={className}>
+ {inactiveLabel}
+ </Badge>
+ )
+}
+
+export { ActiveBadge, Badge, badgeVariants, StatusBadge }
diff --git a/packages/admin/src/tables/customers.tsx b/packages/admin/src/tables/customers.tsx
index c99148ff89..a2ed5787c9 100644
--- a/packages/admin/src/tables/customers.tsx
+++ b/packages/admin/src/tables/customers.tsx
@@ -1,6 +1,7 @@
import { Link } from '@tanstack/react-router'
import { UsersIcon } from 'lucide-react'
import { RelativeTime } from '@/components/spree/relative-time'
+import { ActiveBadge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
defineTable('customers', {
@@ -74,7 +75,7 @@ defineTable('customers', {
filterable: true,
default: false,
filterType: 'boolean',
- render: (c) => (c.accepts_email_marketing ? 'Yes' : 'No'),
+ render: (c) => <ActiveBadge active={c.accepts_email_marketing} />,
},
{
key: 'created_at',
diff --git a/packages/admin/src/tables/payment-methods.tsx b/packages/admin/src/tables/payment-methods.tsx
index 8332fcfe8d..c98ca0eec4 100644
--- a/packages/admin/src/tables/payment-methods.tsx
+++ b/packages/admin/src/tables/payment-methods.tsx
@@ -1,6 +1,6 @@
import type { PaymentMethod, PaymentMethodDisplayOn } from '@spree/admin-sdk'
import { CreditCardIcon } from 'lucide-react'
-import { Badge } from '@/components/ui/badge'
+import { ActiveBadge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
const DISPLAY_ON_LABELS: Record<PaymentMethodDisplayOn, string> = {
@@ -61,14 +61,7 @@ defineTable<PaymentMethod>('payment-methods', {
filterable: true,
default: true,
filterType: 'boolean',
- render: (pm) =>
- pm.active ? (
- <Badge variant="outline">Active</Badge>
- ) : (
- <Badge variant="outline" className="text-muted-foreground">
- Disabled
- </Badge>
- ),
+ render: (pm) => <ActiveBadge active={pm.active} activeLabel="Active" inactiveLabel="Disabled" />,
},
],
})
diff --git a/packages/admin/src/tables/stock-locations.tsx b/packages/admin/src/tables/stock-locations.tsx
index 361aa691bf..fc6a0fa8b4 100644
--- a/packages/admin/src/tables/stock-locations.tsx
+++ b/packages/admin/src/tables/stock-locations.tsx
@@ -1,6 +1,6 @@
import type { StockLocation } from '@spree/admin-sdk'
import { PencilIcon, WarehouseIcon } from 'lucide-react'
-import { Badge } from '@/components/ui/badge'
+import { ActiveBadge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { defineTable } from '@/lib/table-registry'
@@ -25,7 +25,7 @@ defineTable<StockLocation>('stock-locations', {
<button
type="button"
data-stock-location-id={sl.id}
- className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded"
+ className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded cursor-pointer w-full"
>
<span className="font-medium">{sl.name}</span>
{sl.admin_name && (
@@ -68,8 +68,9 @@ defineTable<StockLocation>('stock-locations', {
filterable: true,
default: true,
filterType: 'boolean',
- render: (sl) =>
- sl.pickup_enabled ? <Badge variant="secondary">Enabled</Badge> : <span>—</span>,
+ render: (sl) => (
+ <ActiveBadge active={sl.pickup_enabled} activeLabel="Enabled" dashWhenInactive />
+ ),
},
{
key: 'active',
@@ -78,8 +79,9 @@ defineTable<StockLocation>('stock-locations', {
filterable: true,
default: true,
filterType: 'boolean',
- render: (sl) =>
- sl.active ? <Badge variant="secondary">Active</Badge> : <Badge>Inactive</Badge>,
+ render: (sl) => (
+ <ActiveBadge active={sl.active} activeLabel="Active" inactiveLabel="Inactive" />
+ ),
},
{
key: 'default',
@@ -87,7 +89,7 @@ defineTable<StockLocation>('stock-locations', {
default: true,
filterable: true,
filterType: 'boolean',
- render: (sl) => (sl.default ? <Badge>Default</Badge> : '—'),
+ render: (sl) => <ActiveBadge active={sl.default} activeLabel="Default" dashWhenInactive />,
},
{
key: 'actions',
diff --git a/packages/admin/src/tables/tax-categories.tsx b/packages/admin/src/tables/tax-categories.tsx
index ade1ac3e06..7cc73f47c8 100644
--- a/packages/admin/src/tables/tax-categories.tsx
+++ b/packages/admin/src/tables/tax-categories.tsx
@@ -1,6 +1,6 @@
import type { TaxCategory } from '@spree/admin-sdk'
import { PercentIcon } from 'lucide-react'
-import { Badge } from '@/components/ui/badge'
+import { ActiveBadge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
defineTable<TaxCategory>('tax-categories', {
@@ -44,7 +44,7 @@ defineTable<TaxCategory>('tax-categories', {
key: 'is_default',
label: 'Default',
default: true,
- render: (tc) => (tc.is_default ? <Badge variant="outline">Default</Badge> : null),
+ render: (tc) => <ActiveBadge active={tc.is_default} activeLabel="Default" dashWhenInactive />,
},
],
})