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.

Let the merchant dismiss the auto-expanded option row

Damian Legawiec95f461658588
diff --git a/packages/dashboard/src/components/spree/products/variants-options-builder.tsx b/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
index 2d9330eee7..e13a8247ce 100644
--- a/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
+++ b/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
@@ -12,7 +12,7 @@ import {
SelectValue,
} from '@spree/dashboard-ui'
import { CheckIcon, PencilIcon, PlusIcon, XIcon } from 'lucide-react'
-import { useState } from 'react'
+import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import {
@@ -25,16 +25,31 @@ import type { SelectedOptionType } from './variants-matrix'
interface Props {
selected: SelectedOptionType[]
onChange: (next: SelectedOptionType[]) => void
+ /**
+ * Option types resolved by id for the product's type. Merged over the paged
+ * registry so a type referencing one past the first page still renders its
+ * row and its values picker.
+ */
+ extraOptionTypes?: OptionType[]
}
// The options builder lets the merchant choose option types + values that
// drive the variants matrix. It DOES NOT mutate the product itself — it
// only updates the local `selected` state. The matrix component owns the
// reconcile step that turns this state into RHF variant rows.
-export function VariantsOptionsBuilder({ selected, onChange }: Props) {
+export function VariantsOptionsBuilder({ selected, onChange, extraOptionTypes }: Props) {
const { t } = useTranslation()
const { data: optionTypesData } = useOptionTypes({ limit: 100 })
- const allOptionTypes = optionTypesData?.data ?? []
+ const allOptionTypes = useMemo(() => {
+ const paged = optionTypesData?.data ?? []
+ if (!extraOptionTypes?.length) return paged
+
+ const byId = new Map(paged.map((optionType) => [optionType.id, optionType]))
+ extraOptionTypes.forEach((optionType) => {
+ if (!byId.has(optionType.id)) byId.set(optionType.id, optionType)
+ })
+ return [...byId.values()]
+ }, [optionTypesData, extraOptionTypes])
const [editingIndex, setEditingIndex] = useState<number | null>(null)
const [isAdding, setIsAdding] = useState(false)
@@ -43,9 +58,17 @@ export function VariantsOptionsBuilder({ selected, onChange }: Props) {
// product type seeds exactly that. Open the first one so the values picker
// is in front of them instead of hidden behind a click. Only while nothing
// else is being edited, so it never steals focus mid-interaction.
- const firstEmptyIndex = selected.findIndex((optionType) => optionType.values.length === 0)
- const expandedIndex =
- editingIndex ?? (isAdding ? null : firstEmptyIndex >= 0 ? firstEmptyIndex : null)
+ //
+ // It is an offer they can decline: without tracking the dismissal, cancelling
+ // reopens the row on the next render (it is still empty, so it qualifies
+ // again) and its remove action stays unreachable.
+ const [dismissedAutoExpand, setDismissedAutoExpand] = useState<string | null>(null)
+ const firstEmpty = selected.find((optionType) => optionType.values.length === 0)
+ const autoExpandIndex =
+ isAdding || !firstEmpty || dismissedAutoExpand === firstEmpty.id
+ ? null
+ : selected.indexOf(firstEmpty)
+ const expandedIndex = editingIndex ?? autoExpandIndex
const selectedIds = new Set(selected.map((s) => s.id))
const availableTypes = allOptionTypes.filter((ot) => !selectedIds.has(ot.id))
@@ -77,7 +100,10 @@ export function VariantsOptionsBuilder({ selected, onChange }: Props) {
<OptionPicker
optionType={optionType}
initialValues={ot.values}
- onCancel={() => setEditingIndex(null)}
+ onCancel={() => {
+ setEditingIndex(null)
+ if (ot.values.length === 0) setDismissedAutoExpand(ot.id)
+ }}
onSave={(values) =>
upsertAt(i, {
id: optionType.id,
diff --git a/packages/dashboard/src/components/spree/products/variants-section.tsx b/packages/dashboard/src/components/spree/products/variants-section.tsx
index 4c3fc85759..efe0c4fca3 100644
--- a/packages/dashboard/src/components/spree/products/variants-section.tsx
+++ b/packages/dashboard/src/components/spree/products/variants-section.tsx
@@ -328,7 +328,11 @@ export function VariantsSection({ form, seedFromType = false }: Props) {
<CardTitle>{t('admin.products.variants.title')}</CardTitle>
</CardHeader>
<CardContent className="flex flex-col gap-6">
- <VariantsOptionsBuilder selected={selected} onChange={handleOptionsChange} />
+ <VariantsOptionsBuilder
+ selected={selected}
+ onChange={handleOptionsChange}
+ extraOptionTypes={typeOptionTypes}
+ />
{orphanedKeys.length > 0 && (
<div className="rounded-lg border border-amber-300 bg-amber-50 p-3 text-sm dark:border-amber-700 dark:bg-amber-950/40">
diff --git a/packages/dashboard/src/hooks/use-option-types.ts b/packages/dashboard/src/hooks/use-option-types.ts
index 3a3f1395fa..e852ed3d44 100644
--- a/packages/dashboard/src/hooks/use-option-types.ts
+++ b/packages/dashboard/src/hooks/use-option-types.ts
@@ -53,7 +53,14 @@ export function useOptionTypesByIds(ids: string[] | undefined) {
const idList = ids ?? []
return useQuery({
queryKey: useResourceKey('option-types', 'by-ids', idList),
- queryFn: () => optionTypeAutocompleteProps('by-ids').hydrate(idList),
+ // Expands option_values, unlike the autocomplete's `hydrate`: callers here
+ // feed the values picker, which reads `option_type.option_values`.
+ queryFn: () =>
+ adminClient.optionTypes.list({
+ id_in: idList,
+ limit: idList.length,
+ expand: ['option_values'],
+ }),
enabled: idList.length > 0,
staleTime: 1000 * 60 * 5,
})
diff --git a/spree/core/spec/models/spree/product_type_spec.rb b/spree/core/spec/models/spree/product_type_spec.rb
index 746b2d8c45..820d21cc70 100644
--- a/spree/core/spec/models/spree/product_type_spec.rb
+++ b/spree/core/spec/models/spree/product_type_spec.rb
@@ -101,6 +101,7 @@ describe Spree::ProductType, type: :model do
product_type.update(custom_field_definitions: [{ id: order_definition.prefixed_id }])
expect(product_type.reload.custom_field_definitions).to eq([existing])
+ expect(product_type.errors[:custom_field_definitions]).to be_present
end
end
end