I am trying to create a list which has following features.
- On hover change background color of the listItem.
- On Click change background color of a listItem.
- Toggle background color between clicked elements.[i.e. Only one element in the list can have clicked property]
I have executed onhover 1 and 2 features, but I am not able to implement the 3rd feature. Please help me to solve this problem.
Thanks in advance.
/** @jsx React.DOM */
'use strict'
var React = require('react')
var ListItem = React.createClass({
getInitialState: function() {
return {hover_flag: false, click_flag: false}
},
hoverEvent: function() {
this.setState({hover_flag: !this.state.hover_flag})
},
clickEvent: function(){
this.setState({click_flag: true})
},
render: function() {
var liStyle = {
/* some more class properties */
background: '#cc181e',
}
if(this.state.hover_flag || this.state.click_flag) {
liStyle['background'] = '#880000'
}
if(this.state.click_flag) {
liStyle['background'] = '#880000'
}
return (
<li onClick={this.clickEvent} onMouseEnter={this.hoverEvent} onMouseLeave={this.hoverEvent} style={liStyle} key={this.props.name}>{this.props.name}</li>
)
}
})
module.exports = React.createClass({
render: function() {
var ulStyle = {
padding: '0px',
margin: '20px',
}
var link = {
textDecoration: 'none',
color: 'white',
cursor: 'pointer'
}
var list = this.props.data.map(function(data) {
/*List of li elements */
return <ListItem name={data.name} />
})
return (
<ul style={ulStyle}>
{list}
</ul>
)
}
});