tusen-ai / naive-ui

A Vue 3 Component Library. Fairly Complete. Theme Customizable. Uses TypeScript. Fast.

Home Page:https://www.naiveui.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Let the user handle format exception in date and time pickers throw a function, not only a string format

calebeaires opened this issue · comments

Clear and concise description of the problem

The format definitions use date-fns to operate the dates in the desired format. This is very good and allows our date picker to be more dynamic. However, there are cases where the user will enter wrong date values that will throw an exception through the "format" function.

I understand that the software should handle wrong dates before they are used, but there are cases where the end user is responsible for working with this date. Therefore, it is necessary to allow wrong dates to be handled internally without throwing an exception.

Suggested solution

We could handle this with some alternatives, one or another:

  1. The "Date Type Props" with the name "format" be not only a string but a function defined by us. Internally the date picker component will let the us handle the format. If we use the value as a string, nothing change from what it is today, if we use the value as a function, we are responsible to return the format we wish.
    CleanShot 2024-04-10 at 08 55 52@2x

  2. Null if format throw exception.

const defineFormat = () => {
    let resultFormattedDate = null
    try {
       resultFormattedDate = format(props.modelValue, props.format)
    } catch (e) {
        resultFormattedDate = null
    }
    return resultFormattedDate
}

Alternative

Also, this issue happens in Range Date Picker too. In some cases the final user pass in a parameter a correct date, but to the last value of the range the date is not correct. So, we need to have an option to turn the value undefined if the date is not correct. This happens in post values, dates that comes from a outside source and so on. A good solution would be, turn it null/undefined if the "format" date-fns throw an exception.

Additional context

There is another good point to it. when you let us choose the label format can help us add more info to the date input display, if needed to the app context.

Validations

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.