Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onFocus and onBlur does not render in react

I have the following code

<ElementsBasket   name="nextActionDate"   data={this.props.newLead.get("actions").get("nextActionDate")} >   <div className="form-group">     <span className="glyphicon glyphicon-calendar">Calendar </span>     <span className="glyphicon glyphicon-triangle-bottom"></span>      <input       type="text"       className="form-control"       onFocus={(this.type = "date")}       onBlur={(this.type = "text")}       placeholder="Enter your date here."       value={this.props.newLead.get("actions").get("nextActionDate")}       onChange={onFieldChanged.bind(this, "actions.nextActionDate")}     />   </div> </ElementsBasket>; 

In the input tag, I am trying to have a placeholder text appear in the input field by default, and when clicked I would like the type to be changed as date. And the problem seems to be when clicked inspect element on the Chrome. It would not show onFocus and onBlur.

P/S: Even the onClick seems to have the same problem

like image 466
gates Avatar asked Jul 06 '15 13:07

gates


People also ask

Why is onBlur not fired?

If the element that has the onBlur effect and tabindex is created onClick of another element, it does not automatically gets focus when it appears. Thus, you may need to focus it using element. focus() after creating the element.

How does onBlur work in React?

What is onBlur event in React. React onBlur behaves just like the native JavaScript version of blur. Every time you get out of focus from the input field, the event will trigger. It doesn't matter if the value has changed or not, every time you get out of focus.


1 Answers

Is this what you are looking for?

http://codepen.io/comakai/pen/WvzRKp?editors=001

var Foo = React.createClass({   getInitialState: function () {      return {       type: 'text'     };   },   onFocus: function () {      this.setState({       type: 'date'     });   },   onBlur: function () {      this.setState({       type: 'text'     });   },   render: function () {      return(       <input          type={ this.state.type }          onFocus={ this.onFocus }          onBlur={ this.onBlur }          placeholder="Enter your date here."       />     )   } });  React.render(<Foo/>, document.body); 

As I've commented above, the render method triggers the first time and after that on every state change (and if shouldComponentRender returns true in case if it's implemented):

https://facebook.github.io/react/docs/component-specs.html

like image 112
coma Avatar answered Sep 17 '22 17:09

coma