Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Blur problem with custom input #739

Closed
sibstark opened this issue Jun 19, 2018 · 1 comment
Closed

Blur problem with custom input #739

sibstark opened this issue Jun 19, 2018 · 1 comment

Comments

@sibstark
Copy link

Hello. I use react-day-picker with formik

        <DayPickerInput
          ref={ref => (this.datePicker = ref)}
          dayPickerProps={this.props}
          inputProps={{
            ...field,
            onBlur: this.onBlur,
            disabled,
            mask,
            clearable,
            onClear: this.onClear
          }}
          onDayChange={this.onDayChange}
          value={field.value}
          formatDate={formatDate}
          parseDate={parseDate}
          placeholder={placeholder}
          format={format}
          keepFocus={false}
        />

and if I use DayPickerInput without component prop then after "TAB" click it works fine, after DayPickerInput component, coursour jumps to next form item.
test2

But if I use component prop with custom input

        <DayPickerInput
          ref={ref => (this.datePicker = ref)}
          dayPickerProps={this.props}
          inputProps={{
            ...field,
            onBlur: this.onBlur,
            disabled,
            mask,
            clearable,
            onClear: this.onClear
          }}
          onDayChange={this.onDayChange}
          value={field.value}
          formatDate={formatDate}
          parseDate={parseDate}
          placeholder={placeholder}
          format={format}
          component={(mask || clearable) && SimpleInput}
          keepFocus={false}
        />

Custom input

            <input
              value={mask ? conformToMask(value, mask, { guide: false }).conformedValue : (value || "")}
              placeholder={placeholder ? placeholder : ""}
              disabled={disabled}
              type={type || "text"}
              name={name || ""}
              autoFocus={autoFocus}
              ref={ref => this.getRef(ref)}
              accept={accept || ""}
              onChange={this.onChange}
              onFocus={onFocus && onFocus}
              onKeyUp={onKeyUp && onKeyUp}
              onClick={onClick && onClick}
              onBlur={onBlur && onBlur}
            />

It works bad, DayPickerInput calendar doesnt close after keyboard "Tab" click, it jumps to navigation between months and next. (see gif below)

test1

How to fix it?

@jwm0
Copy link

jwm0 commented Aug 20, 2018

The docs state that in order to keep the input focused:

the component class must have a focus method

Your custom input class needs a focus method that once called sets the focus on the element.

If you are using React 16.3+ then here's what you need to do:

  1. Create a ref in a constructor:
    this.customInput = React.createRef()
  2. Assign it to your input
    <input ref={this.customInput} />
  3. Create a focus method like so:
    focus = () => this.customInput.current.focus()

If you are using an earlier version just do the same with lamba function
<input ref={node => this.customInput = node} />

Also, keepFocus prop should be set to true (it is by default)

@gpbl gpbl closed this as completed Feb 10, 2021
Repository owner locked and limited conversation to collaborators Feb 10, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants