From b7083b9f0f21d0b6d9f3f3afd65b7ae0f8d378dc Mon Sep 17 00:00:00 2001 From: William Bouchard <46496014+willbouch@users.noreply.github.com> Date: Wed, 6 Aug 2025 08:53:04 -0400 Subject: [PATCH] fix(dashboard): variants disappearing when removing an option on product creation (#13150) * fix(dashboard): variants disappearing when removing an option on product creation * error border on option without values --------- Co-authored-by: william bouchard --- .changeset/tasty-donuts-laugh.md | 5 ++ ...product-create-details-variant-section.tsx | 53 ++++++++----------- 2 files changed, 28 insertions(+), 30 deletions(-) create mode 100644 .changeset/tasty-donuts-laugh.md diff --git a/.changeset/tasty-donuts-laugh.md b/.changeset/tasty-donuts-laugh.md new file mode 100644 index 0000000000..df7fdefcee --- /dev/null +++ b/.changeset/tasty-donuts-laugh.md @@ -0,0 +1,5 @@ +--- +"@medusajs/dashboard": patch +--- + +fix(dashboard): variants disappearing when removing an option on product creation diff --git a/packages/admin/dashboard/src/routes/products/product-create/components/product-create-details-form/components/product-create-details-variant-section/product-create-details-variant-section.tsx b/packages/admin/dashboard/src/routes/products/product-create/components/product-create-details-form/components/product-create-details-variant-section/product-create-details-variant-section.tsx index 92dad7f071..67fbbc062b 100644 --- a/packages/admin/dashboard/src/routes/products/product-create/components/product-create-details-form/components/product-create-details-variant-section/product-create-details-variant-section.tsx +++ b/packages/admin/dashboard/src/routes/products/product-create/components/product-create-details-form/components/product-create-details-variant-section/product-create-details-variant-section.tsx @@ -3,6 +3,7 @@ import { Alert, Button, Checkbox, + clx, Heading, Hint, IconButton, @@ -10,13 +11,12 @@ import { Input, Label, Text, - clx, } from "@medusajs/ui" import { Controller, FieldArrayWithId, - UseFormReturn, useFieldArray, + UseFormReturn, useWatch, } from "react-hook-form" import { useTranslation } from "react-i18next" @@ -104,7 +104,9 @@ export const ProductCreateVariantsSection = ({ const newOptions = [...watchedOptions] newOptions[index].values = value - const permutations = getPermutations(newOptions) + const permutations = getPermutations( + newOptions.filter(({ values }) => values.length) + ) const oldVariants = [...watchedVariants] const findMatchingPermutation = (options: Record) => { @@ -157,46 +159,29 @@ export const ProductCreateVariantsSection = ({ const newOptions = [...watchedOptions] newOptions.splice(index, 1) + const validOptionTitles = new Set(newOptions.map((option) => option.title)) const permutations = getPermutations(newOptions) const oldVariants = [...watchedVariants] - const findMatchingPermutation = (options: Record) => { - return permutations.find((permutation) => - Object.keys(options).every((key) => options[key] === permutation[key]) + const newVariants = permutations.reduce((variants, permutation) => { + const variant = oldVariants.find(({ options }) => + Object.keys(options) + .filter((option) => validOptionTitles.has(option)) + .every((key) => options[key] === permutation[key]) ) - } - const newVariants = oldVariants.reduce((variants, variant) => { - const match = findMatchingPermutation(variant.options) - - if (match) { + if (variant) { variants.push({ ...variant, - title: getVariantName(match), - options: match, + title: variant.title, + options: permutation, }) } return variants }, [] as typeof oldVariants) - const usedPermutations = new Set( - newVariants.map((variant) => variant.options) - ) - const unusedPermutations = permutations.filter( - (permutation) => !usedPermutations.has(permutation) - ) - - unusedPermutations.forEach((permutation) => { - newVariants.push({ - title: getVariantName(permutation), - options: permutation, - should_create: false, - variant_rank: newVariants.length, - }) - }) - form.setValue("variants", newVariants) } @@ -347,10 +332,18 @@ export const ProductCreateVariantsSection = ({ )}
    {options.fields.map((option, index) => { + const hasError = + !!form.formState.errors.options?.[index] return (