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

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

React: How to conditionally style dropdown options?

问题

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

  1. handleOptions = () => {
  2. let array = this.props.options.map((option, i) => (
  3. <a key={i} className={`${classes.dropdownoptions} ${this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : ''}`}>
  4. {option}
  5. </a>
  6. ));
  7. return array;
  8. }

这个代码会检查 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

  1. handleOptions = () =&gt;{
  2. let array = this.props.options.map((option, i) =&gt;
  3. &lt;a
  4. key={i}
  5. className={classes.dropdownoptions}&gt;
  6. {option}&lt;/a&gt;)
  7. for(let x = 0; x &lt; this.props.options.length; x++)
  8. {
  9. if(this.props.notPressAble !== undefined)
  10. {
  11. for(let y = 0; y &lt; this.props.notPressAble.length; y++)
  12. {
  13. if(x == this.props.notPressAble[y])
  14. {
  15. array[x].props.className = classes.dropdownoptionsnotpressable
  16. }
  17. }
  18. }
  19. }
  20. return array
  21. }

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

  1. &lt;SideBarContainers name=&quot;Planering&quot; notPressAble={[0, 6, 11]}
  2. options={[
  3. &quot;Aktiviteter:&quot;,
  4. &quot;+ Ny Aktivitet&quot;,
  5. &quot;Visa Alla &#214;ppna&quot;,
  6. &quot;Visa F&#246;rsenat&quot;,
  7. &quot;Visa Alla St&#228;ngda&quot;,
  8. &quot;Visa Alla Kategoriserat&quot;,
  9. &quot;Att g&#246;ra:&quot;,
  10. &quot;+ Ny Att g&#246;ra&quot;,
  11. &quot;Visa Alla &#214;ppna&quot;,
  12. &quot;Visa Alla St&#228;ngda&quot;,
  13. &quot;Visa Alla Kategoriserat&quot;,
  14. &quot;Personal planering:&quot;,
  15. &quot;+ Ny post&quot;,
  16. &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 函数来决定应用哪个类。

  1. handleOptions = () => {
  2. let array = this.props.options.map((option, i) => {
  3. const cls = this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : classes.dropdownoptions;
  4. return (
  5. <a key={i} className={cls}>
  6. {option}
  7. </a>
  8. );
  9. });
  10. return array;
  11. };

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

  1. //array[x].props.className = classes.dropdownoptionsnotpressable
  2. 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.

  1. handleOptions = () =&gt; {
  2. let array = this.props.options.map((option, i) =&gt; {
  3. const cls = this.props.notPressAble.includes(i) ? classes.dropdownoptionsnotpressable : classes.dropdownoptions
  4. return (
  5. &lt;a key={i} className={cls}&gt;
  6. {option}
  7. &lt;/a&gt;
  8. )
  9. });
  10. return array;
  11. };

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.

  1. //array[x].props.className = classes.dropdownoptionsnotpressable
  2. 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:

确定