Skip to content

Commit

Permalink
Merge pull request #2183 from graphcommerce-org/fix/various
Browse files Browse the repository at this point in the history
When updating the quantity of the product in the cart it could happen that the form would become out of sync with the actual data.
  • Loading branch information
paales authored Jan 29, 2024
2 parents 43364b8 + afc8810 commit 66ca467
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-falcons-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/magento-cart-items": patch
---

When updating the quantity of the product in the cart it could happen that the form would become out of sync with the actual data.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NumberFieldElement, NumberFieldElementProps } from '@graphcommerce/ecommerce-ui'
import { ApolloCartErrorSnackbar, useFormGqlMutationCart } from '@graphcommerce/magento-cart'
import { TextInputNumber, TextInputNumberProps } from '@graphcommerce/next-ui'
import { useFormAutoSubmit, UseFormGraphQlOptions } from '@graphcommerce/react-hook-form'
import { TextInputNumberProps } from '@graphcommerce/next-ui'
import { FormAutoSubmit, UseFormGraphQlOptions } from '@graphcommerce/react-hook-form'
import { SxProps, Theme } from '@mui/material'
import React from 'react'
import {
Expand All @@ -16,31 +17,36 @@ type UpdateItemQuantityFormReturn = UseFormGraphQlOptions<

export type UpdateItemQuantityProps = Omit<UpdateItemQuantityMutationVariables, 'cartId'> & {
formOptions?: Omit<UpdateItemQuantityFormReturn, 'mode' | 'defaultValues'>
textInputProps?: TextInputNumberProps
textInputProps?: Omit<
NumberFieldElementProps<UpdateItemQuantityMutationVariables>,
'rules' | 'shouldUnregister' | 'defaultValue' | 'control' | 'disabled'
>
sx?: SxProps<Theme>
}

export function UpdateItemQuantity(props: UpdateItemQuantityProps) {
const { uid, quantity, sx, textInputProps, formOptions } = props
const form = useFormGqlMutationCart(UpdateItemQuantityDocument, {
experimental_useV2: true,
defaultValues: { uid, quantity },
mode: 'onChange',
...formOptions,
})
const { muiRegister, required, handleSubmit, formState, error, reset } = form
const { handleSubmit, formState, error, reset, control } = form

const submit = handleSubmit(() => {})
useFormAutoSubmit({ form, submit })

return (
<form noValidate onSubmit={submit}>
<TextInputNumber
<FormAutoSubmit control={control} submit={submit} initialWait={0} />
<NumberFieldElement
control={control}
name='quantity'
size='small'
variant='standard'
inputProps={{ min: 1 }}
InputProps={{ disableUnderline: true }}
error={!!formState.errors.quantity}
{...muiRegister('quantity', { required: required.quantity })}
helperText={formState.errors.quantity?.message}
sx={sx}
{...textInputProps}
Expand Down
3 changes: 3 additions & 0 deletions packages/next-ui/TextInputNumber/TextInputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const name = 'TextInputNumber' as const
const parts = ['quantity', 'quantityInput', 'button'] as const
const { withState } = extendableComponent<OwnerState, typeof name, typeof parts>(name, parts)

/**
* @deprecated Please us NumberFieldElement
*/
export function TextInputNumber(props: TextInputNumberProps) {
const {
DownProps = {},
Expand Down

0 comments on commit 66ca467

Please sign in to comment.