1

I want to conditionally set a Form.Input depending on selectedProfile if it is not null, it will set the Input as readOnly but if not then it won't. this is my code and of course it didn't work. It would be easier if it's like readOnly = true but it's not

  render() {
  let readOnly = _.isNil(selectedProfile) ? "" : 'readOnly';

  return (
   <Form>
          <Form.Input
                fluid
                required
                label="First name"
                placeholder="Jose"
                name="firstName"
                value={ _.isNil(selectedProfile) ? this.state.firstName : selectedProfile.first_name }
                onChange={this.handleChange}
                {readOnly}
              />
   </Form>

for more info on readOnly, here's semantic ui react's docs about it.

3 Answers 3

4

You have to really provide a prop with true or false.

render() {
  return (
      <Form>
          <Form.Input
                fluid
                required
                label="First name"
                placeholder="Jose"
                name="firstName"
                value={ _.isNil(selectedProfile) ? this.state.firstName : selectedProfile.first_name }
                onChange={this.handleChange}
                // with this you are really passing true or false
                readOnly={!_.isNil(selectedProfile)}
              />
      </Form>
  }
Sign up to request clarification or add additional context in comments.

2 Comments

ohhhh so even if I can use readOnly alone without true or false by default, readOnly={!_.isNil(selectedProfile)} does work when I need to do it conditionally. I have learned something valuable today. thanks
you should assign _.isNil(selectedProfile) to a var and reuse it
1

You have to write it like this if readOnly value comes from a variable:

render() {
  let readOnly = !_.isNil(selectedProfile);

  return (
   <Form>
          <Form.Input
                fluid
                required
                label="First name"
                placeholder="Jose"
                name="firstName"
                value={ _.isNil(selectedProfile) ? this.state.firstName : selectedProfile.first_name }
                onChange={this.handleChange}
                readOnly={readOnly}
              />
   </Form>

Comments

1

Your code is not valid. A component expects property=value. In your case, you just put property. Also, readOnly expects a boolean, not a string. Change your code as following:

render() {
  return (
   <Form>
     <Form.Input
       fluid
       required
       label="First name"
       placeholder="Jose"
       name="firstName"
       value={ _.isNil(selectedProfile) ? this.state.firstName : selectedProfile.first_name }
       onChange={this.handleChange}
       readOnly={!_.isNil(selectedProfile)}
     />
   </Form>
}

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.