1

I have a style const variable,

const style = {
  timeInput: {
    width: '3.75rem',
    outline: 'none',
    borderColor: 'transparent',
    fontSize: remCalc(12),
    color: darkestGrey
  },
}

Here is the input field,

<input
  style={style.timeInput} //here
  disabled={disabled}
  ref={instance => (this.dropDown = instance)}
  value={typeof time === 'string' ? time : moment(time).format('LT')}
  onChange={this.onTimeChange.bind(this)}
></input>

How can I add "opacity: '0.4'" css, when disabled is true ?

1 Answer 1

3

You can create a new object with the property added to it. Before the JSX for that input:

let inputStyle = style.timeInput;
if (disabled) {
    inputStyle = {...inputStyle, opacity: "0.4"};
}

then use inputStyle instead of style.timeInput on the element.


If you like, you can avoid the if, but at the (very, very, very small) cost of creating an unnecessary object:

const inputStyle = {...style.timeInput, ...(disabled ? {opacity: "0.4"} : null)};

That works because spreading null is a no-op.

Sign up to request clarification or add additional context in comments.

1 Comment

@Clarity - Doh! Thanks. :-)

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.