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

Empty inputField with useInput() is not behaving like unselect Date in DayPicker #2058

Open
Tim4497 opened this issue Feb 17, 2024 · 1 comment
Assignees

Comments

@Tim4497
Copy link

Tim4497 commented Feb 17, 2024

Description

With the useInput Hook, when I unselect the date, the input field stays empty, when I empty the InputField, the selectedDate is reset to the initialDate.

Expected Behavior

Regardless of unselecting a date or deleting the whole date from the input, no date should be selected as long as the required attribute is not set.

Actual Behavior

After blurring the InputField it gets filled with the initial date again. Even if it looks in the Day Picker that the day was unselected by the InputField

(btw. I love how they play together and update each other on selecting/writing dates, great job!)

Steps to Reproduce

In https://playcode.io/react:

import React from 'react';
import { DayPicker, useInput } from 'react-day-picker';
import 'react-day-picker/dist/style.css';

export function App() {

  const { dayPickerProps, inputProps } = useInput({
    defaultSelected: new Date(),
    format: 'dd/MM/yyyy',
  });

  return (
    <div>
      <input {...inputProps} />
      <DayPicker {...dayPickerProps} />
    </div>
  );
}

Possible Solution

Screenshots

If applicable, add screenshots or GIFs to help explain your problem.

Your Environment

  • React version: 18.2
  • Your Timezone: Berlin
  • Browser: all
@Tim4497
Copy link
Author

Tim4497 commented Feb 19, 2024

My solution:

import React from 'react';
import { DayPicker, useInput } from 'react-day-picker';
import 'react-day-picker/dist/style.css';

export function App() {

  const { dayPickerProps, inputProps } = useInput({
    defaultSelected: new Date(),
    format: 'dd/MM/yyyy',
  });
  
const handleEmpty = ( e ) => {
    if ( !e.target.value ) {
      return;
    }
    inputProps.onBlur( e );
};

  return (
    <div>
      <input {...inputProps} onBlur={ handleEmpty } />
      <DayPicker {...dayPickerProps} />
    </div>
  );
}

And this might be the place to add the change to make it happen (useInput.ts:141):

// Special case for _required_ fields: on blur, if the value of the input is not
  // a valid date, reset the calendar and the input value.
  const handleBlur: FocusEventHandler<HTMLInputElement> = (e) => {
    const value = e.target.value;
    if(!required && !value.length) {
      return;
    }
    const day = parseValue(value);
    if (!isValidDate(day)) {
      reset();
    }
  };

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