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

2
  • this.props.updateCards will never === {}. Do you want to check if it's empty? Or if it's some other value? Commented Dec 21, 2021 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 Commented Dec 21, 2021 at 19:41

2 Answers 2

2

you can do this like:

<Input readonly={this.props.updateCards ? true : false} />
Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I meant when props.updateCards is empty, not true.
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)} 
    />
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.