react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

customInput typescript issue (React.ReactNode)

Open donghakang opened this issue 1 year ago • 4 comments

Hello, I am currently using react-datepicker for my project, using next.js and typescript.

I followed an example online

const [startDate, setStartDate] = useState(new Date());
  const ExampleCustomInput = forwardRef(({ value, onClick }, ref) => (
    <button className="example-custom-input" onClick={onClick} ref={ref}>
      {value}
    </button>
  ));
  ExampleCustomInput.displayName = 'ExampleCustomInput';
  return (
    <DatePicker
      selected={startDate}
      onChange={(date: Date) => setStartDate(date)}
      customInput={<ExampleCustomInput />}
    />
  );

and it shows this error.

No overload matches this call.
  Overload 1 of 2, '(props: ReactDatePickerProps<never, undefined> | Readonly<ReactDatePickerProps<never, undefined>>): ReactDatePicker<never, undefined>', gave the following error.
    Type 'Element' is not assignable to type 'ReactNode'.
      Property 'children' is missing in type 'Element' but required in type 'ReactPortal'.
  Overload 2 of 2, '(props: ReactDatePickerProps<never, undefined>, context: any): ReactDatePicker<never, undefined>', gave the following error.
    Type 'Element' is not assignable to type 'ReactNode'.ts(2769)
index.d.ts(175, 9): 'children' is declared here.
index.d.ts(59, 5): The expected type comes from property 'customInput' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<ReactDatePicker<never, undefined>> & { css?: Interpolation<Theme>; } & Readonly<...>'
index.d.ts(59, 5): The expected type comes from property 'customInput' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<ReactDatePicker<never, undefined>> & { css?: Interpolation<Theme>; } & Readonly<...>'
(property) customInput?: React.ReactNode

Anyone SO good at typescript that can help me ? 스크린샷 2023-10-27 오후 8 58 10

donghakang avatar Oct 27 '23 11:10 donghakang

upgrade "@types/react": "18.2.28". It solved my problem

Long6695 avatar Oct 30 '23 02:10 Long6695

How should one type forwardRef in this case? Still has the same error

JosephKorel avatar Nov 14 '23 10:11 JosephKorel

I'm having the same issue. Upgrading to [email protected] @types/[email protected] did not solve the issue

chetta19 avatar Jan 10 '24 14:01 chetta19

Simply use as from typescript. This is not a good practice, but can be used for the workaround

type CustomInputProps = {
  onClick(): void;
  // Add any other to fulfill your need
};
const CustomInput = forwardRef((props, ref) => {
  const shadowProps = props as CustomInputProps;
  const shadowRef = ref as ForwardedRef<HTMLButtonElement>; // Or `HtmlInputElement`, or anything you need 
  return (
    <button
      ref={shadowRef}
      onClick={shadowProps.onClick}
      className='bg-purple-700'
    >
      your value
    </button>
  );
});

and use it

function YourComponent() {
  return <ReactDatePicker
     {...yourOtherProps}
     customInput={<CustomInput />}
  />
}

cholazzzb avatar Feb 27 '24 08:02 cholazzzb