react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

Better ref support

Open davidroeca opened this issue 5 years ago • 6 comments

Is your feature request related to a problem? Please describe.

There is not an easy way for to provide separate ref to the input component. The root of the issue boils down to this code.

Related issues:

  • https://github.com/Hacker0x01/react-datepicker/issues/2397
  • https://github.com/Hacker0x01/react-datepicker/issues/2165
  • https://github.com/Hacker0x01/react-datepicker/issues/862
  • https://github.com/Hacker0x01/react-datepicker/issues/1468

Describe the solution you'd like

Allow the user to provide an input ref to react-datepicker (and if customInputRef is provided, then the ref will be assigned to the customInput's customInputRef prop). We would need to handle the following cases:

  • If no ref is provided, then define the callback ref as is already the case
  • If a callback ref is provided, then decorate the callback with another callback that assigns this.input = input
  • If a ref object is provided, then consider doing one of the following (not sure which is better):
    • Define a callback like this:
    const ref = (input) => {
      this.props.ref.current = input // just a thought -- may require tweaks
      this.input = input
    }
    
    • Or set this.input = this.props.ref.current somewhere, maybe a lifecycle method

Describe alternatives you've considered

It seems like the customInputRef prop provides a way to basically prevent this ref from being created (or to leverage this callback ref in a custom way), though I'd expect its main use would be for component libraries that define a separate name for the input ref such as inputRef. As a current work-around, I can create a customInput that takes a throwawayRef prop (and does nothing with it), then define customInputRef="throwawayRef". I don't believe this is an anticipated use case but it works for me.

Additional context

I'm using react-hook-form, and it leverages refs to allow the library to focus on elements that have errors in form submission. This ref is useful for the library to focus(). There are definitely work-arounds, but I think better ref support is the cleaner solution and would probably benefit other use cases as well.

davidroeca avatar Apr 08 '21 17:04 davidroeca

EDIT: This was a reply to a comment that has since been deleted - the person was having trouble with their component running the onChangeRaw event


:thinking: I haven't directly relied on onChangeRaw in my implementation, but having read through the source code, it looks like that fires during onChange and onSelect. Are you passing onChange to your custom input? You'll need to follow the approach here, but I would also recommend passing all of the props that you can down to the input component:

const CustomInput = forwardRef(({
  value,
  onClick,
  // Or whatever you call your custom throwaway ref
  throwAwayRef: _,
  onChange,
  ...props
}, ref) => (
  <MySpecialInputComponent
    ref={ref}
    value={value}
    onClick={onClick}
    onChange={onChange}
    // You may need to hand-pick more of these rather than doing a full spread 
    {...props}
  />
))
  

davidroeca avatar May 26 '21 16:05 davidroeca

@davidroeca Thanks for this help. I deleted my comment because it turns out the issue had nothing to do with my ref handling but a third-party library I was using for input masking, and I had no idea you would be so prompt. Regardless, thanks for taking the time to provide this as I and I'm sure many others will benefit from your findings

neilchaudhuri avatar May 26 '21 22:05 neilchaudhuri

No worries at all! Glad you were able to find the root of the problem

davidroeca avatar May 27 '21 00:05 davidroeca

I'm here because of the same reason as @davidroeca. Thank you for the issue! Actually, refs should be specified for all the input fields inside a form, in the opposite case react-hook-form validation stops working properly with mode: onChange. The following code works:

const ParentComponent = forwardRef((props, ref) => {
  ...
  <Datepicker
    ref={ref}
    ...otherProps
  />
});

though, for the most part, thanks to a value property on the Datepicker class (feels like a well-worked accident rather than intention), because ref is a legacy class component. It would be great if we could get input ref directly.

malinin01 avatar Jun 03 '21 17:06 malinin01

for object ref, can simply pass it to input then directly access it

<input ref={this.props.ref} />
...
this.props.ref.current.focus();

this.props.ref.current = input may not be ideal as current is marked as readonly in typescript.

knilink avatar Nov 22 '21 04:11 knilink

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 12 '22 16:06 stale[bot]