1

I am trying to render customInput in React CurrencyInput field, What I have so far is:

import { TextField, TextFieldProps } from '@mui/material';
import React from 'react';
import CurrencyInput from 'react-currency-input-field';
import { Controller, RegisterOptions, useFormContext } from 'react-hook-form';

type IProps = {
  name: string;
  rules?: RegisterOptions;
  defaultValue?: string;
};

type Props = IProps & TextFieldProps;

export default function RHFCurrencyField({ name, rules, defaultValue, ...other }: Props) {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      rules={rules}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <CurrencyInput
          name={name}
          groupSeparator=" "
          defaultValue={defaultValue}
          decimalsLimit={2}
          onValueChange={(value, name) => {
            field.onChange(value);
          }}
          customInput={() => <CurrencyTextField {...other} {...field} />}
        />
      )}
    />
  );
}

export const CurrencyTextField = React.forwardRef((props: Props, ref: any) => {
  return <TextField {...props} ref={ref} />;
});

And I am getting a warning, and input i

react-dom.development.js:67 Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of CurrencyInput.

What am I missing ?

Cadoiz
  • 1,446
  • 21
  • 31
Wings
  • 502
  • 4
  • 14

0 Answers0