1

How do I make an input box readonly conditionally? I have a state value I want to check and if it is empty, I want the input box prop of readonly to be added to the input box. This is my implementation of this so far:

<Input title={ 'Node Type'} name={ 'nodeType'} inputtype={ 'text'} placeholder={ 'Type node type name'} readonly={this.props.updateCards === {}} />

*edited question to say I want to check if it is empty rather than true based on comment reply

shonacoder
  • 61
  • 6
  • `this.props.updateCards` will never `=== {}`. Do you want to check if it's empty? Or if it's some other value? – Adam Jenkins Dec 21 '21 at 19:27
  • Ah sorry, I meant if it is empty. I phrased my question wrong. It's default state is empty and that's the case when I want the readonly prop added to the input box – shonacoder Dec 21 '21 at 19:41

2 Answers2

2

you can do this like:

<Input readonly={this.props.updateCards ? true : false} />
Zain Khan
  • 1,644
  • 5
  • 31
  • 67
0

If it's empty you can use this like below. For an implementation of what isEmpty would look like, see: this question

return (
    <Input
      title={ 'Node Type'} 
      name={ 'nodeType'} 
      inputtype={ 'text'}
      placeholder={ 'Type node type name'} 
      readonly={isEmpty(this.props.updateCards)} 
    />
)
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100