Skip to content

Instantly share code, notes, and snippets.

@f15gdsy
Created September 2, 2015 07:20
Show Gist options
  • Save f15gdsy/418d13a1297fa79412fc to your computer and use it in GitHub Desktop.
Save f15gdsy/418d13a1297fa79412fc to your computer and use it in GitHub Desktop.
A <input type='text'> that hides border, and shows it when hover.
'use strict';
import React from 'react';
import _ from 'lodash';
export default React.createClass ({
propTypes: {
// Show border on hover and hide otherwise.
borderOnHover: React.PropTypes.bool
},
getDefaultProps () {
return {
style: {
borderWidth: 1,
borderColor: 'grey'
},
borderOnHover: false,
onMouseEnter: function () {},
onMouseOut: function () {},
};
},
getInitialState () {
return {
hover: false,
};
},
handleOnMouseEnter (event) {
this.setState({
hover: true
});
this.props.onMouseEnter(event);
},
handleOnMouseOut (event) {
this.setState({
hover: false
});
this.props.onMouseOut(event);
},
getBorderColor () {
if (this.props.borderOnHover) {
if (!this.state.hover) {
return 'transparent';
}
}
let {style} = this.props;
return style.borderColor;
},
render () {
let {style, onMouseEnter, onMouseOut, ...others} = this.props;
let borderColor = this.getBorderColor();
return (
<input
style = {_.assign({}, style, {borderColor: borderColor})}
type = 'text'
onMouseEnter = {this.handleOnMouseEnter}
onMouseOut = {this.handleOnMouseOut}
{...others}
/>
);
}
})
@f15gdsy
Copy link
Author

f15gdsy commented Sep 2, 2015

Treat it like a , and use borderOnHover to make the border shows only when hover.
Like:

<FlatTextInput borderOnHover/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment