update tooltip.md

This commit is contained in:
Rishi Kumar Chawda
2018-11-14 22:20:59 +05:30
committed by GitHub
parent c473dc9e39
commit a8b40079c5

View File

@ -24,6 +24,7 @@ class Tooltip extends React.Component {
}; };
this.style = { this.style = {
tooltip: { tooltip: {
position: 'relative',
backgroundColor: "rgba(0,0,0,0.7)", backgroundColor: "rgba(0,0,0,0.7)",
color: "white", color: "white",
visibility: "hidden", visibility: "hidden",
@ -31,9 +32,17 @@ class Tooltip extends React.Component {
padding: 5, padding: 5,
borderRadius: 5 borderRadius: 5
}, },
tooltipArrow: {
position: 'absolute',
top: '100%',
left: '50%',
borderWidth: 5,
borderStyle: 'solid',
borderColor: "rgba(0,0,0,0.7) transparent transparent",
},
visible: { visible: {
visibility: "visible" visibility: "visible"
} },
}; };
this.showTooltip = this.toggleTooltip.bind(this, true); this.showTooltip = this.toggleTooltip.bind(this, true);
this.hideTooltip = this.toggleTooltip.bind(this, false); this.hideTooltip = this.toggleTooltip.bind(this, false);
@ -46,25 +55,29 @@ class Tooltip extends React.Component {
}; };
render() { render() {
const { children, ...rest } = this.props; const { children, text, ...rest } = this.props;
const { show } = this.state; const { show } = this.state;
const { visible, tooltip } = this.style; const { visible, tooltip, tooltipArrow } = this.style;
const showTooltip = show ? visible : {}; const showTooltip = show ? visible : {};
return ( return (
<div> <div>
<div style={{ ...tooltip, ...showTooltip }}> <div style={{ ...tooltip, ...showTooltip }}>
This is a simple tooltip {text}
<span style={tooltipArrow}/>
</div>
<div {...rest} onMouseEnter={this.showTooltip} onMouseLeave={this.hideTooltip}>
{children}
</div> </div>
{React.cloneElement(children, {
...rest,
onMouseEnter: this.showTooltip,
onMouseLeave: this.hideTooltip
})}
</div> </div>
); );
} }
} }
``` ```
```jsx ```jsx
ReactDOM.render(<Tooltip />, document.getElementById('root')); ReactDOM.render(
<Tooltip text='Simple tooltip'>
<button>Hover me!</button>
</Tooltip>,
document.getElementById('root')
);
``` ```