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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added universal `<CurrencySelect>` component and improved `<CountryCombobox>`
Damian Legawiec96567efc16b2
diff --git a/packages/admin/src/components/spree/country-flag.tsx b/packages/admin/src/components/spree/country-flag.tsxnew file mode 100644index 0000000000..476abe42c8--- /dev/null+++ b/packages/admin/src/components/spree/country-flag.tsx@@ -0,0 +1,30 @@+import { cn } from '@/lib/utils'++interface CountryFlagProps {+ /** ISO 3166-1 alpha-2 country code (case-insensitive). */+ iso: string+ /** Override the default `1.333em` × `1em` size. Pass any Tailwind size. */+ className?: string+}++/**+ * Renders a country flag via the `flag-icons` CSS sprite. The sprite resolves+ * `.fi.fi-{iso}` to a `background-image` URL pointing at the matching SVG —+ * the browser lazy-loads each flag only when it actually appears on screen.+ *+ * Decorative by design (`aria-hidden`); the surrounding label/text carries+ * the country name for screen readers.+ */+export function CountryFlag({ iso, className }: CountryFlagProps) {+ if (!iso) return null+ return (+ <span+ aria-hidden+ className={cn(+ 'fi shrink-0 rounded-[2px] shadow-[0_0_0_0.5px_rgba(0,0,0,0.08)]',+ `fi-${iso.toLowerCase()}`,+ className,+ )}+ />+ )+}diff --git a/packages/admin/src/components/spree/country-state-fields.tsx b/packages/admin/src/components/spree/country-state-fields.tsxindex 8a1413bfd3..7784c82f01 100644--- a/packages/admin/src/components/spree/country-state-fields.tsx+++ b/packages/admin/src/components/spree/country-state-fields.tsx@@ -1,5 +1,6 @@import type { State } from '@spree/admin-sdk'import { useMemo } from 'react'+import { CountryFlag } from '@/components/spree/country-flag'import {Combobox,ComboboxContent,@@ -8,15 +9,9 @@ import {ComboboxItem,ComboboxList,} from '@/components/ui/combobox'+import { InputGroupAddon } from '@/components/ui/input-group'import { useCountries } from '@/hooks/use-countries'-/** 2-letter ISO code → flag emoji ("US" → "🇺🇸"). */-export function countryFlag(iso: string): string {- return [...iso.toUpperCase()]- .map((c) => String.fromCodePoint(0x1f1e6 + c.charCodeAt(0) - 65))- .join('')-}-type CountryOption = { iso: string; name: string }type StateOption = { abbr: string; name: string }@@ -73,18 +68,26 @@ export function CountryCombobox({items={items}value={selected}onValueChange={(c: CountryOption | null) => onValueChange(c?.iso ?? '')}- itemToStringLabel={(c: CountryOption | null) =>- c?.iso ? `${countryFlag(c.iso)} ${c.name}` : (c?.name ?? '')- }+ itemToStringLabel={(c: CountryOption | null) => c?.name ?? ''}itemToStringValue={(c: CountryOption | null) => c?.iso ?? ''}>- <ComboboxInput placeholder={placeholder} disabled={disabled} />+ <ComboboxInput placeholder={placeholder} disabled={disabled}>+ {/* When a country is picked the InputGroup gets a leading flag —+ mirrors the dropdown items so the trigger shows the same shape.+ Hidden while empty so the input doesn't lurch when typing a new+ search query. */}+ {selected?.iso && (+ <InputGroupAddon align="inline-start">+ <CountryFlag iso={selected.iso} />+ </InputGroupAddon>+ )}+ </ComboboxInput><ComboboxContent><ComboboxEmpty>No countries found</ComboboxEmpty><ComboboxList>{(country: CountryOption) => (<ComboboxItem key={country.iso} value={country}>- <span className="mr-1.5">{countryFlag(country.iso)}</span>+ <CountryFlag iso={country.iso} />{country.name}</ComboboxItem>)}diff --git a/packages/admin/src/components/spree/currency-select.tsx b/packages/admin/src/components/spree/currency-select.tsxnew file mode 100644index 0000000000..d90663dc4d--- /dev/null+++ b/packages/admin/src/components/spree/currency-select.tsx@@ -0,0 +1,103 @@+import { useMemo, useState } from 'react'+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from '@/components/ui/select'+import { useStore } from '@/providers/store-provider'++interface CurrencySelectProps {+ /** ID for the trigger — paired with the parent `<FieldLabel htmlFor>`. */+ id?: string+ /** Submit name — a hidden input mirrors the value so plain `FormData` works. */+ name?: string+ /** Initial selection. Falls back to the store's default currency. */+ defaultValue?: string+ /** Controlled value. Pair with `onChange` to lift state out. */+ value?: string+ /** Fires on every selection change. */+ onChange?: (currency: string) => void+ /** Marks the field required for screen readers + native form submission. */+ required?: boolean+ disabled?: boolean+}++/**+ * Resolves a currency code (`USD`) to its localized display name (`United+ * States Dollar`) via the browser's built-in `Intl.DisplayNames`. Falls+ * back to the code itself when the runtime lacks coverage (very old+ * browsers, exotic codes) so the user always sees something.+ */+function useCurrencyDisplayName(locale: string) {+ return useMemo(() => {+ try {+ const formatter = new Intl.DisplayNames([locale, 'en'], { type: 'currency' })+ return (code: string) => formatter.of(code) ?? code+ } catch {+ return (code: string) => code+ }+ }, [locale])+}++/**+ * Picker for one of the current store's `supported_currencies`. Defaults to+ * the store's `default_currency` so callers don't have to wire it up+ * themselves. Each option reads `CODE — Full Name` (e.g.+ * `USD — United States Dollar`), localized via the browser's+ * `Intl.DisplayNames` against the store's `default_locale`. Use this+ * anywhere the merchant is choosing a currency for an admin-side action+ * (issuing store credit, recording a refund, manual money entry on an+ * order, etc.).+ */+export function CurrencySelect({+ id,+ name,+ defaultValue,+ value: controlledValue,+ onChange,+ required,+ disabled,+}: CurrencySelectProps) {+ const { currencies, defaultCurrency, defaultLocale } = useStore()+ const [internalValue, setInternalValue] = useState(defaultValue ?? defaultCurrency)+ const isControlled = controlledValue !== undefined+ const value = isControlled ? controlledValue : internalValue+ const displayNameFor = useCurrencyDisplayName(defaultLocale)++ const handleChange = (next: string) => {+ if (!isControlled) setInternalValue(next)+ onChange?.(next)+ }++ const renderOption = (code: string) => {+ const name = displayNameFor(code)+ return name === code ? code : `${code} — ${name}`+ }++ return (+ <>+ {/* Hidden input keeps the parent `<form>` submit / FormData path working+ without each caller having to thread the value through state. */}+ {name && <input type="hidden" name={name} value={value} />}+ <Select value={value} onValueChange={handleChange} disabled={disabled}>+ <SelectTrigger id={id} aria-required={required}>+ {/* Base UI's `<SelectValue>` defaults to rendering the raw `value`+ (the bare ISO code). Use the children render-prop so the+ trigger shows the same `CODE — Full Name` as the items. */}+ <SelectValue placeholder="Select a currency">+ {(v) => (v ? renderOption(v as string) : (v as string))}+ </SelectValue>+ </SelectTrigger>+ <SelectContent>+ {currencies.map((currency) => (+ <SelectItem key={currency} value={currency}>+ {renderOption(currency)}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </>+ )+}diff --git a/packages/admin/src/components/ui/combobox.tsx b/packages/admin/src/components/ui/combobox.tsxindex 5ebd3b953d..2aa30cedcb 100644--- a/packages/admin/src/components/ui/combobox.tsx+++ b/packages/admin/src/components/ui/combobox.tsx@@ -133,7 +133,10 @@ function ComboboxItem({ className, children, ...props }: ComboboxPrimitive.Item.<ComboboxPrimitive.Itemdata-slot="combobox-item"className={cn(- "relative flex w-full cursor-default items-center gap-2 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",+ // Padding + font sized to match `<Input>` (`py-1.5 px-2.5 text-base+ // leading-normal`) so the dropdown rows feel like a continuation of+ // the trigger field. `pr-8` keeps room for the check indicator.+ "relative flex w-full cursor-default items-center gap-2 rounded-md py-1.5 pr-8 pl-2.5 text-base font-normal leading-normal outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",className,)}{...props}diff --git a/packages/admin/src/components/ui/input.tsx b/packages/admin/src/components/ui/input.tsxindex ef09aed116..42d912c6c5 100644--- a/packages/admin/src/components/ui/input.tsx+++ b/packages/admin/src/components/ui/input.tsx@@ -8,7 +8,7 @@ function Input({ className, type, ...props }: React.ComponentProps<'input'>) {type={type}data-slot="input"className={cn(- 'block w-full min-w-0 rounded-lg border border-border bg-card py-1.5 px-2.5 text-base font-normal leading-normal text-foreground shadow-xs transition-all duration-100 ease-in-out outline-none placeholder:text-muted-foreground focus:border-blue-500 focus:shadow-[0_0_0_3px_rgba(59,130,246,0.15)] disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-muted disabled:border-border disabled:text-muted-foreground disabled:shadow-none aria-invalid:border-destructive file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground',+ 'block w-full min-w-0 min-h-9 rounded-lg border border-border bg-card py-1.5 px-2.5 text-base font-normal leading-normal text-foreground shadow-xs transition-all duration-100 ease-in-out outline-none placeholder:text-muted-foreground focus:border-blue-500 focus:shadow-[0_0_0_3px_rgba(59,130,246,0.15)] disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-muted disabled:border-border disabled:text-muted-foreground disabled:shadow-none aria-invalid:border-destructive file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground',className,)}{...props}diff --git a/packages/admin/src/components/ui/select.tsx b/packages/admin/src/components/ui/select.tsxindex 46e888cce5..0c4c7391df 100644--- a/packages/admin/src/components/ui/select.tsx+++ b/packages/admin/src/components/ui/select.tsx@@ -54,7 +54,12 @@ function SelectTrigger({data-slot="select-trigger"data-size={size}className={cn(- "flex w-full items-center justify-between gap-1.5 rounded-lg border border-border bg-card py-1.5 pr-2 pl-2.5 text-base font-normal leading-normal text-foreground shadow-xs transition-all duration-100 ease-in-out outline-none select-none focus:border-blue-500 focus:shadow-[0_0_0_3px_rgba(59,130,246,0.15)] disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-muted disabled:border-border disabled:text-muted-foreground disabled:shadow-none aria-invalid:border-destructive data-[placeholder]:text-muted-foreground data-[size=sm]:py-1 data-[size=sm]:px-2 data-[size=sm]:text-sm *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",+ // `min-h-9.5` (default) and `data-[size=sm]:min-h-[1.9375rem]` lock+ // the trigger to the same natural height as `<Input>` so the field+ // doesn't collapse to chevron-height when the SelectValue render-prop+ // returns an empty string (e.g., when value is unset and the+ // placeholder isn't surfaced through the render-prop).+ "flex w-full min-h-9 items-center justify-between gap-1.5 rounded-lg border border-border bg-card py-1.5 pr-2 pl-2.5 text-base font-normal leading-normal text-foreground shadow-xs transition-all duration-100 ease-in-out outline-none select-none focus:border-blue-500 focus:shadow-[0_0_0_3px_rgba(59,130,246,0.15)] disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-muted disabled:border-border disabled:text-muted-foreground disabled:shadow-none aria-invalid:border-destructive data-[placeholder]:text-muted-foreground data-[size=sm]:py-1 data-[size=sm]:px-2 data-[size=sm]:text-sm data-[size=sm]:min-h-[1.9375rem] *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",className,)}{...props}@@ -129,7 +134,10 @@ function SelectItem({<SelectPrimitive.Itemdata-slot="select-item"className={cn(- "relative flex w-full cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-2 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",+ // Padding + font sized to match `<Input>` and `<SelectTrigger>` so+ // the dropdown rows feel like a continuation of the trigger field.+ // `pr-8` reserves room for the absolute-positioned check indicator.+ "relative flex w-full cursor-default items-center gap-2 rounded-md py-1.5 pr-8 pl-2.5 text-base font-normal leading-normal outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",className,)}{...props}diff --git a/packages/admin/src/index.css b/packages/admin/src/index.cssindex 0c3592aac9..e9106ad44e 100644--- a/packages/admin/src/index.css+++ b/packages/admin/src/index.css@@ -3,6 +3,10 @@@import "shadcn/tailwind.css";@import "@fontsource-variable/geist";@import "@fontsource-variable/geist-mono";+/* Country flag icons — `<CountryFlag iso="US" />` resolves to `.fi.fi-us`,+ which lazy-loads `flags/4x3/us.svg` via background-image. Only flags+ actually rendered hit the network. */+@import "flag-icons/css/flag-icons.min.css";@custom-variant dark (&:is(.dark *));diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 31046844b3..b3646e5b5b 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -69,6 +69,9 @@ importers:date-fns:specifier: ^4.1.0version: 4.1.0+ flag-icons:+ specifier: ^7.5.0+ version: 7.5.0lucide-react:specifier: ^1.14.0version: 1.14.0([email protected])@@ -2418,6 +2421,9 @@ packages:resolution: {integrity: sha512-pVIECanWFC61Hzl2+oOCtoJ3F17kglZC/6N94eRWycFgBH35hHx0Li604ZIzhseh97mf2p0cv7vVrOZGoqhlEg==}+ resolution: {integrity: sha512-kd+MNXviFIg5hijH766tt+3x76ele1AXlo4zDdCxIvqWZhKt4T83bOtxUOOMlTx/EcFdUMH5yvQgYlFh1EqqFg==}+resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}engines: {node: '>=12.20.0'}@@ -6024,6 +6030,8 @@ snapshots:mlly: 1.8.0rollup: 4.57.1+ [email protected]: {}+dependencies:fetch-blob: 3.2.0