aranlucas / react-hook-form-mantine

React Hook Form bindings for Mantine components
https://aranlucas.github.io/react-hook-form-mantine/
83 stars 12 forks source link

Improve DateInput to support other formats #13

Closed mrljsh closed 7 months ago

mrljsh commented 11 months ago

Issue:

Support for Custom Date Formats and Handling ISO Strings in DateInput Component

Issue Description:

The current implementation of the DateInput component lacks proper support for using custom date formats, such as YYYY-MM-DD, in conjunction with the toISOString() method. When attempting to set the react-hook-form value using an ISO string representation of a date (e.g., date.toISOString()), the application crashes due to the component's behavior when reading and formatting the value.

Steps to Reproduce:

Expected Behavior:

The DateInput component should gracefully handle custom date formats and ISO string representations of dates without causing application crashes. When the value is set using an ISO string, the component should correctly convert the string back to a Date object before proceeding with its internal processing.

For example:

  const { control, handleSubmit, setValue } = useForm({
    defaultValues: {
      date: '',
    },
  })
        <DateInput
          label="Date"
          name="date"
          onChange={(value) => {
            const date = new Date(value).toISOString()
            setValue('date', date)
          }}
          control={control}
        />

Proposed Solution:

To address this issue, it's suggested to enhance the DateInput component's behavior as follows:

When setting the value of the component, check if the provided value is a string. If it is, convert it to a Date object using new Date(value) before proceeding with further processing. This adjustment will allow the component to accept ISO string representations of dates and handle custom date formats more gracefully.

Additional Notes:

Properly handling custom date formats and ISO strings in the DateInput component will improve its usability and prevent crashes that currently occur when attempting to set values using different formats.

aranlucas commented 10 months ago

I would prefer to keep this library to the bare minimum.

I would suggest you either open this suggestion in https://github.com/mantinedev/mantine or you use null instead of '' as the value as show in https://mantine.dev/dates/date-input#usage

sanginovs commented 10 months ago

@aranlucas can u add example usage of Dateinput/DatePickerInput to App.tsx ? thanks

aranlucas commented 10 months ago

Here's an example: https://github.com/aranlucas/react-hook-form-mantine/commit/d62c7ab344191b862053c0c2c5e7701c6496126f

github-actions[bot] commented 8 months ago

Stale issue message