Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the value in case of an invalid date string? #174

Open
shimikano opened this issue Dec 22, 2023 · 2 comments
Open

Clear the value in case of an invalid date string? #174

shimikano opened this issue Dec 22, 2023 · 2 comments

Comments

@shimikano
Copy link

Hello and thank you for this library.

I would like to implement some custom date parse logic using the format.toValue(...) option.

Currently, when this parse function returns undefined for an invalid date string (e.g. "xyz"), the value of the date picker is reverted to the last selected date.

Is there a way that the value is cleared instead of reverted if the parser returns undefined?

Codepen to experiment: https://codepen.io/shimikano/pen/OJqJZZY

@mymth
Copy link
Owner

mymth commented Dec 23, 2023

You cannot do it with custom parser because it's the internal setDate() function that ignores the invalid date (undefined) returned by the parser.

You need to override the update() API method, like this: https://codepen.io/mymth/pen/YzgzMBR

@shimikano
Copy link
Author

@mymth I'm sorry, I somehow missed your reply.

Thank you - I was able to use your workaround of overriding update() to get the desired behavior. For reference:

class MyDatepicker extends Datepicker {

  override update(options = undefined) {
    const text = this.inputField.value;

    const parsed = myParseLogic(text)

    if (isValid(parsed)) {
      this.setDate(parsed, options);
    } else {
      this.setDate({clear: true});
    }
  }

}

This issue can be closed as far as I'm concerned. Thank you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants