React:如何有条件地样式化下拉选项?

huangapple go评论72阅读模式
英文:

React: How to conditionally style dropdown options?

问题

尝试使用以下代码解决这个问题:

handleOptions = () => {
    let array = this.props.options.map((option, i) => (
        <a key={i} className={`${classes.dropdownoptions} ${this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : ''}`}>
            {option}
        </a>
    ));

    return array;
}

这个代码会检查 this.props.notPressAble 数组中的索引,如果索引在 notPressAble 中,就会为相应的元素添加 classes.dropdownoptionsnotpressable 类名。

英文:

Im trying to change the className of choosen objects in the mapped array with another array(this.props.notPressAble). This is because i want some of the objects in the array to have another css style

handleOptions = () =&gt;{
    let array = this.props.options.map((option, i) =&gt; 
    &lt;a 
    key={i} 
    className={classes.dropdownoptions}&gt;
    {option}&lt;/a&gt;)

    for(let x = 0; x &lt; this.props.options.length; x++)
    {
        if(this.props.notPressAble !== undefined)
        {
            for(let y = 0; y &lt; this.props.notPressAble.length; y++)
            {
                if(x == this.props.notPressAble[y])
                {
                    array[x].props.className = classes.dropdownoptionsnotpressable
                }
            }
        }
    }

    return array
}

The code below is where i implement the class for readers too get a better understanding of my problem

 &lt;SideBarContainers name=&quot;Planering&quot; notPressAble={[0, 6, 11]}
 options={[
     &quot;Aktiviteter:&quot;,
     &quot;+ Ny Aktivitet&quot;,
     &quot;Visa Alla &#214;ppna&quot;,
     &quot;Visa F&#246;rsenat&quot;,
     &quot;Visa Alla St&#228;ngda&quot;,
     &quot;Visa Alla Kategoriserat&quot;,
     &quot;Att g&#246;ra:&quot;,
     &quot;+ Ny Att g&#246;ra&quot;,
     &quot;Visa Alla &#214;ppna&quot;,
     &quot;Visa Alla St&#228;ngda&quot;,
     &quot;Visa Alla Kategoriserat&quot;,
     &quot;Personal planering:&quot;,
     &quot;+ Ny post&quot;,
     &quot;Visa Alla enkel&quot;

]} />

The problem im having is that array[x].props.className is a readOnly value that cant be changed. Is there another way of doing this?

答案1

得分: 0

You can translate the code-related portion as follows:

你为什么要使用 for 循环来遍历选项,然后判断是否不可点击?你可以直接使用 includes 函数来决定应用哪个类。

handleOptions = () => {
  let array = this.props.options.map((option, i) => {
    const cls = this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : classes.dropdownoptions;
    return (
      <a key={i} className={cls}>
        {option}
      </a>
    );
  });

  return array;
};

如果你仍然想继续使用你当前的代码,那么你需要克隆你想要更改的元素,然后进行如下更改。

//array[x].props.className = classes.dropdownoptionsnotpressable
array[x] = React.cloneElement(array[x], { className: classes.dropdownoptionsnotpressable });

请注意,这只是代码的翻译,不包括问题的回答。

英文:

Why are you using for loops to loop over options and then notPressAble. You could just use includes function and decide which class to apply.

handleOptions = () =&gt; {
    let array = this.props.options.map((option, i) =&gt; {
      const cls = this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : classes.dropdownoptions
      return (
        &lt;a key={i} className={cls}&gt;
          {option}
        &lt;/a&gt;
      )
    });

    return array;
  };

If you still want to go with your current code then you need to clone the element you want to change and then make the changes like below.

//array[x].props.className = classes.dropdownoptionsnotpressable
array[x] = React.cloneElement(array[x], {className: classes.dropdownoptionsnotpressable});

huangapple
  • 本文由 发表于 2023年5月22日 19:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定