Reset variation form if a new variation is given (#36078)

This commit is contained in:
Maikel David Pérez Gómez 2022-12-21 15:00:35 -03:00 committed by GitHub
parent c7c0322163
commit 1ab7a851cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -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 (
<Form< Partial< ProductVariation > >
initialValues={ productVariation }
errors={ {} }
ref={ formRef }
>
<ProductFormHeader />
<ProductFormLayout>

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Reset variation form if a new variation is given