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

DateInput `onClose` raises `e.replace is not a function`

Open mjewell opened this issue 5 years ago • 1 comments

Using a DateInput with onClose causes parse to be called with a Date, but it seems like it is expected to only be called with a string:

import { dateParse } from 'js-utils-apm';

function Example() {
  const [date, setDate] = useState('');
  return <DateInput
    dateFormat="MM/DD/YYYY"
    parse={dateParse}
    onChange={setDate}
    value={date}
    onClose={console.log}
  />;
}

The error comes from this line: https://github.com/appfolio/react-gears/blob/v5.7.2/src/components/DateInput.js#L249

It can be fixed by handling the case where the value is a date:

parse={(dateOrString: string | Date) => {
  if (dateOrString instanceof Date) {
    return dateOrString;
  }
  return dateParse(dateOrString);
}}

We should either change it so parse is expected to handle being given a Date or a string, or change it so parse does not get called when the value is a Date.

mjewell avatar Mar 31 '20 18:03 mjewell