反应按钮活动样式

时间:2018-05-02 03:23:17

标签: javascript reactjs reactstrap

我是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", "$1,000-$2,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", "$2,500-$4,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", "$5,000-$9,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", "$10,000-$34,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", "$35,000-$74,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", ">$74,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", ">$74,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>) 
    }
}

2 个答案:

答案 0 :(得分:0)

您可以为每个按钮指定一个唯一的name道具并创建一个activeButton状态,该状态将采用激活的按钮名称的值:

handleActiveButton(e) {
  e.preventDefault();

  const activeName = e.target.name;
  this.setState({ activeName });
}

然后在按钮的className道具中,您可以执行className={ this.state.activeName === 'button1' ? 'active' : '' }

之类的操作

其中button1是按钮的名称,active是用于显示图片图标的css类。

答案 1 :(得分:0)

我明白了!

通过id

填充activeName的值

谢谢!