react-currency-input-field icon indicating copy to clipboard operation
react-currency-input-field copied to clipboard

CurrencyInput is not reset with Formik/ReactHookForm

Open SalahAdDin opened this issue 2 years ago • 15 comments

Describe the bug When using this input with either Formik or ReactHookForm and resetting the form state, it still keeps the updated value.

To Reproduce Steps to reproduce the behavior:

  1. Go to the modal with the form.
  2. Modify the value of the required input.
  3. Modify the value of other inputs.
  4. Close/Cancel the modal with the form.
  5. Open the modal again.
  6. Check the inputs and see CurrencyInput wasn't reset.

Expected behavior It should act like the other inputs and receive its value reset as well.

Additional context You can see the actual behavior in the video:

https://user-images.githubusercontent.com/5034215/190878969-764c1aed-01de-4b2f-a083-6ef3521392a1.mov

SalahAdDin avatar Sep 17 '22 23:09 SalahAdDin

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 19 '22 16:11 stale[bot]

👍🏽 ⬆️

SalahAdDin avatar Nov 19 '22 17:11 SalahAdDin

anyone solve this yet?

surgieboi avatar Jan 02 '23 04:01 surgieboi

@surgieboi I'm using the react hook form with a controller, this is the component I made

<Controller
  name="amount"
  control={control}
  render={({ field }) => (
    <CurrencyField
      allowNegativeValue={false}
      allowDecimals={false}
      groupSeparator="."
      decimalSeparator=","
      value={field.value}
      name={field.name}
      control={control}
    />
  )}
/>

call reset function to reset the field reset({amount: ""})

ekasuweantara avatar Jan 04 '23 08:01 ekasuweantara

I'm seeing the same issue.

edit: I think in my case the issue is related to changing the value prop to undefined. So coalescing to empty string seems to be a work-around for me.

ex: value={amount ?? ''}

jake-crane avatar Jan 05 '23 19:01 jake-crane

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 12 '23 17:03 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 18 '23 20:05 stale[bot]

@cchanxzy What do you think about this?

SalahAdDin avatar May 19 '23 09:05 SalahAdDin

@cchanxzy same problem

BLOCKMATERIAL avatar Jun 14 '23 15:06 BLOCKMATERIAL

I think the issue might be the state used here, which changes as keystroke happens.

All 4 examples uses their own state to set value.

There seems to be no way to have both programmatically set value and value from key stroke without maintaining your own state

@cchanxzy if userValue passed in ... !== undefined && ... !== null, including initial default value, no matter what key pressed no new value will be renedered.

xyz1hang avatar Aug 06 '23 15:08 xyz1hang

Is this still an issue? I imagine it is.

Can someone create a code sandbox so I can have a look?

Thanks

cchanxzy avatar Nov 07 '23 19:11 cchanxzy

Here's a codesandbox example of the Issue I was running into: https://codesandbox.io/s/elegant-darwin-lt8mpx?file=/src/App.tsx

To trigger the issue, enter a number in the input and then click the reset button. Notice that the value is set to undefined, but the currency input still displays the number that you entered.

jake-crane avatar Nov 08 '23 02:11 jake-crane

Having the same issue here

mochetts avatar Dec 06 '23 18:12 mochetts

I'm not sure about the original issue, but other folks on this thread seem to encounter an issue specifically when the value is set to undefined. To separate concerns from the original issue, I've create a new ticket specifically when undefined is passed with a Sandbox reproducible case. Feel free to follow along: https://github.com/cchanxzy/react-currency-input-field/issues/322

aconrad avatar Dec 20 '23 18:12 aconrad

as already mentioned, the value is turned to undefined when it's empty

here is how I deal with it

<FormField
  control={form.control}
  name="price"
  render={({ field }) => (
    <FormItem>
      <FormLabel>Price</FormLabel>
      <FormControl>
        <CurrencyInput
          prefix="Rp "
          groupSeparator="."
          decimalSeparator=","
          placeholder="Rp 0"
          decimalsLimit={2}
          value={field.value}
          onValueChange={(value) => {
            field.onChange(value ?? '');
          }}
        />
      </FormControl>
      <FormMessage />
    </FormItem>
  )}
/>

williamtobing avatar Feb 11 '24 19:02 williamtobing

Why is it closed?

SalahAdDin avatar Apr 20 '24 00:04 SalahAdDin