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

Can not reset the value of a Datepicker component #145

Open
glennverschooren opened this issue Apr 29, 2020 · 0 comments
Open

Can not reset the value of a Datepicker component #145

glennverschooren opened this issue Apr 29, 2020 · 0 comments

Comments

@glennverschooren
Copy link

The problem

It is not possible to reset (set the value to an empty string) the value of a Datepicker component.
I'm using the component inside a Formik form.
The user can reset the form but I can't find how to set the activeDate to an empty string.

static getDerivedStateFromProps(props, state) {
    // Does not allow empty strings here
    if (props.activeDate) {
      const momentDate = Moment(props.activeDate, props.format, true);
      if (momentDate.isValid()) {
        return {
          activeDate: momentDate,
          input: props.activeDate
        };
      }
    }

    return null;
  }

The solution

accept an empty string as activeDate (current value)

static getDerivedStateFromProps(props, state) {
    // Reset input value
    if (props.activeDate === '') {
        return {
          activeDate: null,
          input: props.activeDate
        };
    }
    if (props.activeDate) {
      const momentDate = Moment(props.activeDate, props.format, true);
      if (momentDate.isValid()) {
        return {
          activeDate: momentDate,
          input: props.activeDate
        };
      }
    }

    return null;
  }
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

1 participant