Created
April 26, 2017 14:49
-
-
Save marcsolanadal/5e12ce94582538adfb5a610dc3c368c3 to your computer and use it in GitHub Desktop.
onblur-onmousedown-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>dropdown-with-events</title> | |
<link rel="stylesheet" type="text/css" href="styles.css" media="screen" /> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux-thunk/2.2.0/redux-thunk.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script> | |
<script src="https://unpkg.com/react@15.5.4/dist/react.js"></script> | |
<script src="https://unpkg.com/react-dom@15.5.4/dist/react-dom.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/5.0.3/react-redux.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.4/ReactRouter.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux-form/6.6.3/redux-form.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/node-uuid/1.4.8/uuid.min.js"></script> | |
<script type="text/babel" src="main.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Component } = React | |
const { createStore, applyMiddleware, combineReducers, compose } = Redux | |
const { Provider, connect } = ReactRedux | |
const { Router, Route, browserHistory, IndexRoute, Link } = ReactRouter | |
const { Field, reduxForm } = ReduxForm | |
const formReducer = ReduxForm.reducer | |
class App extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
focused: false, | |
avoidBlur: false | |
} | |
this.handleClick = this.handleClick.bind(this) | |
this.handleBlur = this.handleBlur.bind(this) | |
this.handleFocus = this.handleFocus.bind(this) | |
} | |
handleClick() { | |
if (this.state.focused) { | |
this.setState({ focused: false, avoidBlur: true }) | |
console.log('close and submit') | |
} else { | |
this.setState({ focused: true, avoidBlur: true }) | |
//this.textInput.focus() | |
setTimeout(() => { this.textInput.focus() }, 0) | |
} | |
} | |
handleBlur() { | |
if (!this.state.avoidBlur) { | |
this.setState({ focused: false }) | |
console.log('onblur') | |
} | |
} | |
handleFocus() { | |
this.setState({ focused: true, avoidBlur: false }) | |
console.log('onfocus') | |
} | |
render() { | |
console.log('render') | |
const inputClass = (this.state.focused) ? 'input__opened' : 'input__closed' | |
return ( | |
<div className='container'> | |
<input className={inputClass} | |
type='text' | |
ref={(input) => { this.textInput = input; }} | |
onFocus={this.handleFocus} | |
onBlur={this.handleBlur} | |
value={inputClass} | |
/> | |
<div className='button' onMouseDown={this.handleClick}> | |
Click Me! | |
</div> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root') | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.container { | |
display: flex; | |
} | |
.input__opened { | |
background-color: white; | |
} | |
.input__closed { | |
background-color: lightgray; | |
} | |
.button { | |
background-color: lime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment