react-bootstrap-datetimepicker icon indicating copy to clipboard operation
react-bootstrap-datetimepicker copied to clipboard

Is there a way to display calendar on input focus?

Open Yuripetusko opened this issue 9 years ago • 3 comments

Is there a way to display calendar on input focus? Maybe some exposed methods?

Yuripetusko avatar Oct 01 '15 13:10 Yuripetusko

I have a hacky solution to this issue. Its almost 2am here so rigorous testing can wait until morning.

Basically, you can use the inputProps on DateTimeField to make use of onFocus. Whenever the input gets focus, this function will be called. I am sure it can be tidied up/simplified, but at the moment I get the parentElement of e.target and click on the first span in its children. Hopefully that makes sense, here is the code:

_handleBlur: function() {
  console.log('The input was blurred');
},
_handleFocus: function(e) {
  var parent = e.target.parentElement;
  var children = parent.childNodes;
  for (var i = 0; i < children.length; i++) {
    if (children[i].tagName.toLowerCase() === 'span') {
      children[i].click();
      return;
    }
  }
},

render: function() {

  var inputProps = {
    placeholder: 'DD/MM/YYYY',
    onBlur: e => this._handleBlur(e),
    onFocus: e => this._handleFocus(e)
  };

  return (
    <DateTimeField inputProps={inputProps} />
  );
}

01taylop avatar Oct 07 '15 00:10 01taylop

A shorter version:

_handleFocus: function(e) {
  e.target.nextSibling.click();
},

The onBlur is not necessary, because the underlying component already handles this event and hides the calendar.

kadishmal avatar Mar 17 '16 03:03 kadishmal

This issue could be closed now. Both solutions work smoothly. You should create a merge request with this feature.

martinbkure avatar Mar 31 '16 07:03 martinbkure