Tips for dynamically changing the style (color/fill) of material-ui elements when generating them within a map function

I am struggling to dynamically change the style (colors) of elements in list items generated from a map function that includes rgb-color values. While using classes can work, trying to dynamically set the style based on the data/color provided in the object array proves to be a challenge. The attempts below display the color as fill="rgb(48, 183, 0)", but the predefined classes still take precedence over the dynamically added style.

  <Select
        multiple
        value={filterList[index]}
        renderValue={(selected) => selected.join(", ")}
        onChange={(event) => {
           filterList[index] = event.target.value;
           onChange(filterList[index], index, column);
        }}   >

       { processStatusColorsArr.map(({ id, name, color} ) =>(
            MenuItem key={id} value={name} style={{ fill: `${color}%`, color: `${color}%` }}  >
               <Checkbox   
                   fill={color}  style={{ fill: `${color}%`, color: `${color}%` }}  />
               <ListItemText primary={name}
                   color= {color}  />

               <IconButton color={color}
                   fill={color}
                   style={{ fill: `${color}%`, color: `${color}%` }}    />

           </MenuItem>
       ))}
 </Select>

I have also attempted the following:

{render_filter_process_status_colors(color)} instead of '<IconButton ..../>'

with the function defined as:

const render_filter_process_status_colors = (value, tableMeta, updateValue) =>  {
if(value === undefined) return;

return (
        <div>
            <FontAwesomeIcon icon={"square"} style={value} size={"lg"} fixedWidth/>
        </div>
    );
}

but it has not yielded the desired outcome.

Thank you

Answer №1

Utilizing the following function

{generate_colored_icon(color)}

as defined below

const generate_colored_icon = (value, meta, updateValue) =>  {
if(value === undefined) return;
const iconColor = {color: value}
return (
        <div>
            <FontAwesomeIcon icon={"square"} style={iconColor} size={"lg"} fixedWidth/>
        </div>
); }

I am able to display an icon in the specified color from my array of RGB colors.

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

Ways to position a button at the bottom of a div and beneath an image

Hey there, I have a little issue that seems to be causing me trouble. My brain just can't seem to come up with the solution. Here's the HTML code: #div { position: fixed; top: 10%; left: 20%; right: 20%; bottom: 10%; ...

The dropdown menu disappears when I hover over the tab, making it impossible for me to navigate it in the react app

My navigation component includes a Games tab with a dropdown menu that appears when you hover over it. However, I'm facing an issue where the dropdown menu closes before the user can transition from hovering over the games tab to the actual dropdown m ...

Guide on making a color legend with JavaScript hover effects

On my website, there is a dynamic element that displays a table when values are present and hides it when there are no values. The values in the table are color-coded to represent different definitions. I wanted to add hover text with a color key for these ...

Dynamic content cannot have classes added to them using jQuery

When adding content dynamically, it is necessary to use an event handler for a parent element using on(). However, I am facing an issue where the class added with addClass on dynamically generated content disappears immediately. Let's take a look at ...

How can I ensure that buttons in a row utilize the full 100% of available space?

I have a row of buttons in my HTML code like this: HTML : <div class="buttons"> <div><input type="button"/></div> <div><input type="button"/></div> <div><input type="button"/></div& ...

Tips on customizing the appearance of React rendering components

<div> <h3>{this.props.product.name}</h3> <h3>{this.props.product.code}</h3> {this.renderColors()} <article> <div da ...

The slideUp and slideDown functions are not functioning properly

Whenever a user clicks on image-exp, I want description-exp to slide down while other description-exp slides up. I am currently using a Bootstrap template available at this link. To demonstrate the issue, I created a JSFiddle that can be accessed here. H ...

What is the reason the .clearfix class fails to properly clear a floated element?

Check out this post: Understanding clearfix The most helpful response indicates that the clearfix should be applied directly to the last floating element: To incorporate a clearfix, you simply need to include HTML code: <div class="clearfix"> ...

What is the best way to add a transition to a list element so that it expands in size without impacting its background image or the surrounding list items?

Lately, I've been working on revamping a flat design website and my focus has mainly been on the navbar. The original plan was to incorporate simple icons on a colored background that would darken when hovered over by the user's mouse. To achieve ...

Troubles with Angular elements not aligning correctly when using the "display: block" property

When using an angular element for a directive with "display: block", it may not automatically take up 100% of the width of the parent container. In order to ensure that it does, the width must be explicitly set to "100%" in the CSS. Despite setting "width: ...

Steps for setting a background image for a div

I am facing an issue with 3 horizontally aligned images and a background image. The problem is that when I set the background image for the aligned images, it flows over them instead of staying behind. I want the 3 aligned images to be on top of the backgr ...

Deleting a division with the help of media queries

As a challenge, I am attempting to eliminate the .navbar-brand>img element once the screen size exceeds 768px by using media queries. Unfortunately, my efforts have not been successful so far. Although the styling applied to the img is successfully remo ...

Centered alignment with floating divs of abbreviated length

My HTML file has a structure similar to the following: <div style="float:right"> A line of text, floating to the right </div> <div style="text-align:center"> And here, several lines of<br /> text which should be centered. </div& ...

Dropdown button split aligned with its first button

Comparing the alignment of split button dropdowns in Bootstrap 3 and Bootstrap 4, I find that Bootstrap 3 aligns them to the left of the first button, while Bootstrap 4 only aligns them according to the second button. https://i.stack.imgur.com/gAt78.png h ...

Using Jquery variables for calculations and they do not work with numerical values

Hey there, I'm new to this so bear with me. I have a jQuery question that's puzzling me... I'm attempting to set the line height using the following method: var line_height = $('.container').css("height"); console.log(line_height ...

Resizing the container div to perfectly fit the displayed React component

Is there a way to render a component with its style and position defined by specific cases without having to create a container that takes up the entire body? I want the styles of the container to match those of the component, without the need for the cont ...

Is there a way to disable certain CSS features in Chrome, like CSS grid, for testing purposes?

I'm exploring alternative layouts for my app in case CSS grid is not supported. Is there a way to disable features like CSS grid in Chrome or Canary? ...

Display Text Aligned in the Middle Beside Website's Logo

Newbie to HTML and CSS here! I'm attempting to achieve a design similar to this: https://i.sstatic.net/HyiP3.jpg Below is my code snippet: <div id="topbar"> <div class="image"> <img src="images/ghwlogo.png"> </ ...

Selection of text within a block resembling a bar of buttons

design.css body { background-color: #666666; margin-left:500px; margin-right:500px; margin-top:10px; margin-bottom:20px; font-family:Verdana, Geneva, Tahoma, sans-serif; font-size:12px; color:white; } div.header { bac ...

Refresh the content of an iframe (local HTML) by clicking on buttons

When a user clicks on a button, I am loading an HTML file into a DIV (iframe). The issue arises when the user clicks another button and wants to reload the iframe with a different HTML file. This is the current code snippet: <script type="text/javasc ...