如何用 1 个常数形成 3 的三元数

How to form a Ternary for 3 with 1 constant

编辑完整场景:我有两个组件和四个按钮!三个按钮的默认值为null。假设 a,b,c,d 是四个不同的按钮。

请注意,组件是有条件地呈现的,因此只有一个组件对用户可见!

这是我的代码:

 <Button
     disabled={(this.state.quantityItemSelected === null || 
     this.state.deliveryOptionSelected === null) && 
     (this.state.timeSlotItemSelected === null) ? true : false} 
 />

默认常量是this.state.quantityItemSelected === null,两者相同。

它在 () 中的条件下工作正常,但是关于第一个,当 this.state.quantityItemSelected 一开始不是 null 然后 this.state.timeSlotItemSelected 不是一个 null 它工作正常,

但是当我反转它时,当 this.state.timeSlotItemSelected 一开始不为 null 时,条件变为 false。

我期望的工作是,当任何事物 null 具有常量时,它应该为真。

请帮忙解决这个问题,让我知道还需要了解什么

For example - if i have a,b,c when a = null and b = null then it should be true and when a = null and c = null it should be true, rest conditions false.

后两个条件需要用括号括起来,用||

 <Button
   disabled={(
     (this.state.quantityItemSelected === null) &&
     (this.state.deliveryOptionSelected === null || this.state.timeSlotItemSelected === null)
   ) ? true : false} 
 />

For example - if i have a,b,c when a = null and b = null then it should be true and when a = null and c = null it should be true, rest conditions false.

你(用伪逻辑符号)是 (!a && !b && c) || (!a && b && !c) 扩展意味着

function calculateConditions(a, b, c) {
  return (a === null && b === null && c !== null) || (a === null && b !== null && c === null);
}

//T - true, F - False. Shortenned to keep the formatting
const table = [
  ["FFF", calculateConditions( null     ,  null     ,  null)],
  ["FFT", calculateConditions( null     ,  null     , "not null")],
  ["FTF", calculateConditions( null     , "not null",  null)],
  ["FTT", calculateConditions( null     , "not null", "not null")],
  ["TFF", calculateConditions("not null",  null     ,  null)],
  ["TFT", calculateConditions("not null",  null     , "not null")],
  ["TTF", calculateConditions("not null", "not null",  null)],
  ["TTT", calculateConditions("not null", "not null", "not null")],
]

for (const [key, value] of table) {
  console.log(key, value)
}

假设'this.state.quantityItemSelected'是'a','this.state.deliveryOptionSelected'是'b','this.state.timeSlotItemSelected'是'c',条件应该是:

this.state.quantityItemSelected === null && 
(this.state.deliveryOptionSelected === null ||
this.state.timeSlotItemSelected === null) ? true : false

或与 a、b 和 c

a === null && (b === null || c === null)  ? true : false

如果零、空字符串等虚假值不是 a、b 和 c 的有效值,您可以通过以下方式改进代码:

disable = !a && (!b || !c)

正在回答已编辑的问题

In the first component, button a & b will be null by default so that, button d will be disabled. when user presses on a & b , a value will be assigned to a & b, so after a & b are assigned , then d should be enabled.

这与之前的场景不同。

disableButtonD = a === null && b === null;  

或更好

disableButtonD = !(a && b);  

还有:

in the second component, button a & c will have a default value of null and when they both got a value onPressing, again button d will be enabled.

disableButtonD = a === null && C === null;  

或更好

disableButtonD = !(a && C);  

鉴于你的限制,你不能用一个句子来表达两种情况。 当然,根据你使用的框架,你可以有一个功能。

Angular 例子:

组件:

disableD(firstValue, secondValue) {
  return !(firstValue && secondValue)
} 

HTML

<!-- first Button -->
<button [disabled]="disableD(a, b)"></button>

<!-- second button-->
<button [disabled]="disableD(a, c)"></button>

If i have a,b,c when a = null and b = null then it should be true and when a = null and c = null it should be true, rest conditions false.

试试下面的方法,会有用的!你可以用这个。

(this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === 
 null) && (this.state.timeSlotItemSelected === null || this.state.quantityItemSelected 
=== null) ? true : false

如果有效,请告诉我!