React 按钮活动样式
React button active style
我是 React 和 UI 开发的新手,请多多包涵。
我有一个按钮列表,我试图在按钮处于活动状态时设置不同的图像图标,当我单击另一个按钮时,第二个按钮应该处于活动状态,第一个按钮应该处于活动状态,依此类推。
这是我的代码:
export class ABC extends Component {
configFile = require('../../resources/stubs/config.json')
constructor(props) {
super(props);
this.state = {
cSelected: [],
isHidden: true,
level: this.configFile.Level,
amount: this.configFile.amount,
des1: this.configFile.desc1,
des2: this.configFile.desc2,
des3: this.configFile.desc3,
}
this.onRadioBtnClick = this.onRadioBtnClick.bind(this);
};
onRadioBtnClick(cSelected, aSelected, d1, d2, d3) {
this.setState({
cSelected,
aSelected,
d1,
d2,
d3
});
}
render(){
return (
<div>
<ButtonGroup>
<Button className="circleIcon"
onClick={() => this.onRadioBtnClick(this.state.level, this.state.amount, this.state.des1, this.state.des2, this.state.des3, )}
active={this.state.cSelected === 1}><img src={incirclePurple}
alt="profile-tab-icon" />|<span className="numberCircle">1</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle2", ",000-,499", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 2}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">2</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle3", ",500-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 3}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">3</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle4", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 4}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">4</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle5", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 5}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">5</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle6", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 6}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">6</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("President's Circle", ">,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 7}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">7</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Chairman's Circle", ">,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 8}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">8</span></Button>
</ButtonGroup>
<div>
<p>{this.state.cSelected}{this.state.aSelected}</p>
<ul>
<li>{this.state.d1}</li>
<li>{this.state.d2}</li>
<li>{this.state.d3}</li>
</ul>
</div>
</div>)
}
}
您可以为每个按钮提供一个唯一的 name
道具,并创建一个 activeButton
状态,该状态将采用激活按钮名称的值:
handleActiveButton(e) {
e.preventDefault();
const activeName = e.target.name;
this.setState({ activeName });
}
然后在按钮的 className
属性中,你可以做类似 className={ this.state.activeName === 'button1' ? 'active' : '' }
的事情
其中 button1
是按钮的名称,active
是您的 css class 以显示您的图像图标。
我明白了!!
通过id填充activeName的值
谢谢!!
我是 React 和 UI 开发的新手,请多多包涵。 我有一个按钮列表,我试图在按钮处于活动状态时设置不同的图像图标,当我单击另一个按钮时,第二个按钮应该处于活动状态,第一个按钮应该处于活动状态,依此类推。
这是我的代码:
export class ABC extends Component {
configFile = require('../../resources/stubs/config.json')
constructor(props) {
super(props);
this.state = {
cSelected: [],
isHidden: true,
level: this.configFile.Level,
amount: this.configFile.amount,
des1: this.configFile.desc1,
des2: this.configFile.desc2,
des3: this.configFile.desc3,
}
this.onRadioBtnClick = this.onRadioBtnClick.bind(this);
};
onRadioBtnClick(cSelected, aSelected, d1, d2, d3) {
this.setState({
cSelected,
aSelected,
d1,
d2,
d3
});
}
render(){
return (
<div>
<ButtonGroup>
<Button className="circleIcon"
onClick={() => this.onRadioBtnClick(this.state.level, this.state.amount, this.state.des1, this.state.des2, this.state.des3, )}
active={this.state.cSelected === 1}><img src={incirclePurple}
alt="profile-tab-icon" />|<span className="numberCircle">1</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle2", ",000-,499", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 2}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">2</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle3", ",500-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 3}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">3</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle4", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 4}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">4</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle5", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 5}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">5</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Circle6", ",000-,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 6}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">6</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("President's Circle", ">,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 7}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">7</span></Button>
<Button className="circleIcon" onClick={() => this.onRadioBtnClick("Chairman's Circle", ">,999", this.state.des1, this.state.des2, this.state.des3)} active={this.state.cSelected === 8}><img src={incirclePurple} alt="profile-tab-icon" />|<span className="numberCircle">8</span></Button>
</ButtonGroup>
<div>
<p>{this.state.cSelected}{this.state.aSelected}</p>
<ul>
<li>{this.state.d1}</li>
<li>{this.state.d2}</li>
<li>{this.state.d3}</li>
</ul>
</div>
</div>)
}
}
您可以为每个按钮提供一个唯一的 name
道具,并创建一个 activeButton
状态,该状态将采用激活按钮名称的值:
handleActiveButton(e) {
e.preventDefault();
const activeName = e.target.name;
this.setState({ activeName });
}
然后在按钮的 className
属性中,你可以做类似 className={ this.state.activeName === 'button1' ? 'active' : '' }
其中 button1
是按钮的名称,active
是您的 css class 以显示您的图像图标。
我明白了!!
通过id填充activeName的值
谢谢!!