简体   繁体   English

单击后如何从选择菜单中将特定选项显示为灰色?

[英]How to grey out a specific option from a select menu upon click?

I have a list of questions and I want a specific option to be greyed out when it has been clicked on. 我有一个问题列表,我希望在单击某个特定选项后将其显示为灰色。 At the moment is greys out the whole select list when I click on it (which is not what I want). 现在,当我单击它时,整个选择列表显示为灰色(这不是我想要的)。 How to achieve this? 如何实现呢?

my dropdownmenu with select option: 我的带有选择选项的下拉菜单:

    export function DropDownMenu(props) {

        let selectOption;

        if (props.questionOverviewList[props.index]) {
            selectOption = 
    props.questionOverviewList[props.index].map((item) =>
                <OptionList key={item._id} index={props.index} 
    question= . 
  {item.question}/>);
        }

        return <div className="panel panel-default">
            <div className="input-group mb-3">
                <div className="input-group-prepend">
                    <CategoryLabel title={props.categoryTitle}/>
                </div>
                <select className="custom-select" onClick={(e) => 
    e.target.disabled = true} 
                        onChange={(e) => . 
   props.selectedQuestionByTeamLeader(e.target.value)}>
                    <option></option>
                    {selectOption}
                </select>
            </div>
        </div>

    }

My option component: 我的选择部分:

    export default function OptionList(props) {

        return <option>{props.question}</option>

    }

Note that the option component is re-usable. 请注意,选项组件可重复使用。

Here is possible solution which stores selected item _id into DropDownMenu state. 这是将所选项目_id存储到DropDownMenu状态的可能解决方案。 You have to get back _id from the child option component by browsing HTML Select element options: 您必须通过浏览HTML选择元素选项从子选项组件中获取_id:

class DropDownMenu extends React.Component {

  constructor(props) {
    super(props);
    this.state = { selectedItemId: undefined };
  }

  optionSelected(selectElement) {
    let selectedOption;
    for (let option of selectElement.target.children) {
      if (option.value === selectElement.target.value) {
        selectedOption = option;
        break;
      }
    }

    this.setState({ selectedItemId: selectedOption.getAttribute('_id') });
    this.props.selectedQuestionByTeamLeader(selectedOption);
  }

  render() {
    const selectOption = this.props.questionOverviewList[this.props.index].map((item) => {
      console.log(this.state.selectedItemId === `${item._id}`);
      return <OptionList key={item._id} _id={item._id} index={this.props.index} question={item.question}
        grayedOut={this.state.selectedItemId === `${item._id}`} />
    });

    return <select className="custom-select" onChange={(e) => this.optionSelected(e)}>
      <option></option>
      {selectOption}
    </select>;
  }
}

Then disabled attribut based on OptionList grayedOut prop: 然后根据OptionList grayedOut属性禁用属性:

function OptionList(props) {
  return <option disabled={props.grayedOut} _id={props._id}>{props.question}</option>
}

This JSFiddle shows it's working! 这个JSFiddle显示它正在工作!

https://jsfiddle.net/poyea78q/ https://jsfiddle.net/poyea78q/

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

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