From 1ab7a851cb436b7abf3aa5d5f23ee4a98b30822f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20David=20P=C3=A9rez=20G=C3=B3mez?= Date: Wed, 21 Dec 2022 15:00:35 -0300 Subject: [PATCH] Reset variation form if a new variation is given (#36078) --- .../client/products/product-variation-form.tsx | 17 ++++++++++++++++- plugins/woocommerce/changelog/fix-36077 | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-36077 diff --git a/plugins/woocommerce-admin/client/products/product-variation-form.tsx b/plugins/woocommerce-admin/client/products/product-variation-form.tsx index b3bba8eafc8..c86f1b00c30 100644 --- a/plugins/woocommerce-admin/client/products/product-variation-form.tsx +++ b/plugins/woocommerce-admin/client/products/product-variation-form.tsx @@ -2,7 +2,8 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { Form } from '@woocommerce/components'; +import { useEffect, useRef } from '@wordpress/element'; +import { Form, FormRef } from '@woocommerce/components'; import { PartialProduct, ProductVariation } from '@woocommerce/data'; /** @@ -24,15 +25,29 @@ export const ProductVariationForm: React.FC< { product: PartialProduct; productVariation: Partial< ProductVariation >; } > = ( { product, productVariation } ) => { + const previousVariationIdRef = useRef< number >(); + const formRef = useRef< FormRef< Partial< ProductVariation > > >( null ); + const navigationProps = useProductVariationNavigation( { product, productVariation, } ); + useEffect( () => { + if ( + productVariation && + previousVariationIdRef.current !== productVariation.id + ) { + formRef.current?.resetForm( productVariation ); + previousVariationIdRef.current = productVariation.id; + } + }, [ productVariation ] ); + return ( > initialValues={ productVariation } errors={ {} } + ref={ formRef } > diff --git a/plugins/woocommerce/changelog/fix-36077 b/plugins/woocommerce/changelog/fix-36077 new file mode 100644 index 00000000000..4d38db78f21 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-36077 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Reset variation form if a new variation is given