简体   繁体   English

切换所选列表项的背景颜色react.js

[英]toggle background color of selected list item react.js

what i want to do it when i click on one of the list items i want the class selected to be applied and when i click again i want it to be removed, so basically a toggle function, but it happens to all 3 list items, so if i click on one, all get selected, but i want it so only one can be selected at a time 当我单击列表项之一时我想要执行的操作,我希望选择所选的类别,当我再次单击时,我希望将其删除,因此基本上是一个切换功能,但它碰巧出现在所有三个列表项中,因此,如果我单击一个,则全部选中,但是我想要这样,一次只能选择一个

import React from 'react';



export default class Order extends React.Component {
constructor() {
    super()
    this.state = {
      selected: false,
    };
  }

 toggleChoice() {
    const selected = !this.state.selected;
    this.setState({selected});
  }
  render() {
    const { selected } = this.state;
    const selectedCircle = selected ? " selected":"";
    return (
        <div class="container" id="order-layout">
            <div class="row">

                    <div class="card-panel white">
                      <div class="center">
                        <h5>Your Order</h5>
                        <p class="margin-top-30 bold">Choose Pizza size in cm</p>
                        <ul class="margin-top-30">
                            <li ><div onClick={this.toggleChoice.bind(this)} class={"circle-20 hovered-circle" + selectedCircle}>20</div></li>
                            <li ><div onClick={this.toggleChoice.bind(this)} class={"circle-30 hovered-circle" + selectedCircle}>30</div></li>
                            <li ><div onClick={this.toggleChoice.bind(this)} class={"circle-40 hovered-circle" + selectedCircle}>40</div></li>
                        </ul> 

                      </div>
                    </div>

            </div>
        </div>
    );
  }
}

In your case selectedCircle has the same value for all list items. 在您的情况下, selectedCircle对于所有列表项都具有相同的值。 If you click on one of list items selectedCircle changes for the other ones too. 如果你点击列表中的项目之一selectedCircle对于其他的人太多变化。

Thus, you need for every list item the different piece of state. 因此,您需要为每个列表项提供不同的状态。

You can try something like this : 您可以尝试这样的事情:

First change your state to : 首先将您的状态更改为:

this.state = {
  selectedCircle: {},
};

Your toggleChoice function needs to know on which list item you clicked. 您的toggleChoice函数需要知道您单击了哪个列表项。 You can change it to something like this : 您可以将其更改为以下内容:

toggleChoice(name, event) {
   let selected = this.state.selectedCircle;
   selected = {};
   let selectedCircles = selected[name] == "selected" ? "" : "selected";
   selected[name] = selectedCircles;
   this.setState({selectedCircle: selected});
}

Then in your render you need to add to every list item the appropriate piece of state. 然后,在渲染中,您需要向每个列表项添加适当的状态。 Change your render function to something like : 将渲染功能更改为:

render() {

return (
    <div className="container" id="order-layout">
        <div className="row">

                <div className="card-panel white">
                  <div className="center">
                    <h5>Your Order</h5>
                    <p className="margin-top-30 bold">Choose Pizza size in cm</p>
                    <ul className="margin-top-30">
                        <li ><div onClick={this.toggleChoice.bind(this, "first")} className={"circle-20 hovered-circle " + this.state.selectedCircle["first"]}>20</div></li>
                        <li ><div onClick={this.toggleChoice.bind(this, "second")} className={"circle-30 hovered-circle " + this.state.selectedCircle["second"]}>30</div></li>
                        <li ><div onClick={this.toggleChoice.bind(this, "third")} className={"circle-40 hovered-circle " + this.state.selectedCircle["third"]}>40</div></li>
                    </ul> 

                  </div>
                </div>

        </div>
    </div>
);

And voilà, that should work. 而且,应该起作用。 Here is jsfiddle . 这是jsfiddle

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM