Adjusting label color when input is deactivated in a React application using TypeScript

Is there a way to change the label color in my view when the input is disabled using Typescript or CSS?

This is the interface I am working with:

interface image

Here is the code snippet:

<tr>
      <td className="name">Critère d'agrégation</td>
      <td className="value column-2">
        {aggregationDomain.map(codedValue => 
             <label >
             <input type="radio" name="AggregationSelection" 
             value={codedValue.code} checked={props.reportConfig.aggregation === 
             codedValue.code} onChange={updateAggregation} disabled= 
             {!config?.aggregationEnabled.includes(codedValue.code)} 
             />

                 {codedValue.name}
             </label>
        )}
     </td>
 </tr> 

I am currently working with React (typescript) and would appreciate any help on this matter. Thank you!

Answer №1

You may want to consider reordering your code in order to separate input from the label

<tr>
  <td className="name">Critère d'agrégation</td>
  <td className="value column-2">
    {aggregationDomain.map(codedValue => 
       <>
         
         <input type="radio" name="AggregationSelection" 
         value={codedValue.code} checked={props.reportConfig.aggregation === 
         codedValue.code} onChange={updateAggregation} disabled= 
         {!config?.aggregationEnabled.includes(codedValue.code)} 
         />

         <label >{codedValue.name}</label>
       </>
    )}
 </td>

For instance, you can use this example as a reference for styling purposes. It's not specific to react, simply copy the css and apply it to your code

input[type='radio']:disabled+label {
  color: #ccc;
}
<div>
  <input type='radio' name='demo' id='option1' />
  <label for='option1'>Option 1</label>
</div>
<div>
  <input type='radio' name='demo' id='option2' disabled />
  <label for='option2'>Option 2 (Disabled)</label>
</div>
<div>
  <input type='radio' name='demo' id='option3' />
  <label for='option3'>Option 3</label>
</div>

Answer №2

apply the disabled condition from the label tag to control the color

  <label color={!config?.aggregationEnabled.includes(codedValue.code) ? 'red': 'black'}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enabling onClick based on conditions

I'm struggling to disable a Link onClick when a certain condition is not met. Attempted to move the logic outside of render using a function, but the button always ends up disabled. Tried CSS to disable click, only to realize that tab and enter can ...

Inject a cookie into the Axios interceptor for the request handler

I am in the process of setting up Axios to always include a request header Authorization with a value from the user's cookie. Here is my code: import axios, { AxiosRequestConfig, AxiosResponse} from 'axios'; import {useCookies} from "react-c ...

Leveraging icons with Bootstrap 4.5

I am currently exploring how to incorporate Bootstrap 4.5 icons using CSS. Do you have any examples of code that could guide me on how to achieve this? I am specifically interested in understanding the required CSS declarations that would allow me to use t ...

What is the best way to utilize the sx prop in Material UI for applying styles specifically when the component is active or selected?

In the code snippet below, I have set up the useState hook so that when a ListItem is clicked on, the selected state becomes true. My goal is to apply specific styling when an item is selected using the sx prop instead of creating a styled component for su ...

New feature in NextAuth.js: Credentials authentication with reset password functionality included

Currently, I am working on a login system using next/auth with credentials logging system and an invitation-only feature. Is there a way to include a Reset Password link on the generated /api/auth/signin page? ...

Can the style of certain words within a paragraph be altered using CSS only, without the need for any HTML tags?

Is it feasible to alter the appearance of certain words within a paragraph using CSS solely, without applying any HTML tags to those particular words? Awaiting a positive response. Thank you! ...

Android: Substituting < p > and < br / > elements

My current situation involves receiving JSON data from a website and displaying it in a TextView. I have successfully achieved this, however, my boss requires the text to be plain with no paragraphs or empty lines. The text is retrieved from the website&a ...

- Lorem ipsum dolor sit amet, consectetur adipiscing elit

I am looking to create a list with lower-roman indexes that are enclosed in parentheses (ii), where the content of each item is indented. Here is an example: (i) lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut metus nec urna ...

What is the process for refreshing the state in the handleReducer function?

When one state changes, I want to clear another state using useEffect. I am struggling with this because I am new to programming and finding it difficult to clear two states. The complexity of the problem is increasing as well. const initialState = { ...

React's useState Hook: Modifying Values

I just started learning about React and React hooks, and I'm trying to figure out how to reset the value in useState back to default when new filters are selected. const [apartments, setApartments] = React.useState([]) const [page, setPage] = React.us ...

Vercel is encountering difficulty locating a module or type declaration during the construction of a Next.js application

Currently, I'm facing an issue while trying to deploy a Next.js app to Vercel. The package react-swipeable appears to have its own type declarations but the build fails with the following error: Running "npm run build" ... > next build ... Failed t ...

Building React Typescript Components with Froala Editor Plugins

Attempting to integrate a custom plugin into a Froala Editor within my React application using the package react-froala-wysiwyg. Following a tutorial on incorporating a custom popup/plugin found here. Encountering an issue due to TypeScript incompatibility ...

Resizing rectangular images with CSS/Bootstrap into perfect squares

Imagine there is a rectangular image with dimensions of 1400px (height) x 700px (width). I am looking to turn it into a square while maintaining its original proportions and having white sides added. I specifically do not want to crop the image - I want it ...

How can I connect an HTML page to an MS-Access database?

Hello everyone, I am looking to connect an HTML page to an Access database. If anyone has the HTML code for searching that database, I would greatly appreciate it. Thank you! I am using Access 2010. ...

Tips for Preserving the HTML Page State After Making jQuery Changes

Hi there! I am currently working on developing a card game using HTML5, CSS3, and Javascript. This game will communicate with a server built on node.js, facilitated by socket.io for data transmission. One of the key features I am trying to implement is th ...

Red X appearing in IE9 for HTML5 Video

It's strange that my video works on some browsers but not others, even though I'm using mp4, ogv, and webm formats. Even weirder is that some videos work while others don't. To illustrate the issue, I've created a jsfiddle for you to c ...

Can you customize the buttons in the operation panel of an antd image preview window?

I am looking to add a button that allows users to download or share the image being previewed. Is there a way to achieve this functionality? I have researched and read through the documentation but have not found a solution yet. ...

What is the most efficient method for updating URLs in CSS files before the website goes live?

The project I am currently working on has been worked on by various coders with different backgrounds. One of my recent achievements was writing a clean Capistrano recipe to minimize CSS and JS files. Now, the challenge ahead is figuring out how to identif ...

I find it difficult to customize columns in grid template areas

I am having trouble adjusting the width of my header's columns to be 24vw, 24vw, 4vw, 24vw, and 24vw respectively. Despite specifying these widths, the browser is rendering all columns with equal width. Specifically, I am unable to change the width of ...

What is causing the error message 'Unexpected use of 'location' no-restricted-globals'?

When working on my reactjs code, I encountered the following issue: const { children, location: { pathname }, } = this.props; let path = location.pathname; I am also utilizing the react router module in this component. Anyone have suggestions on how ...