Is there a way to modify the color of a checkbox within MUI?

I'm currently customizing the checkbox color in Material UI while using FormIk for data submission. I aim to change the default checkbox color to red, but I'm unsure about how to achieve this.

  <FormGroup>
           <Box display="flex" flexWrap="wrap">
                        {daysOptions.map((day, i) => (
                          <Box key={i}>
                            <Field type="checkbox" name="availability" label={day} value={day} as={Checkbox} />
                          </Box>
                        ))}
                      </Box>
                    </FormGroup>

https://i.stack.imgur.com/oLWYk.png

Answer №1

Using withstyles

  1. Start by importing Checkbox (the checkbox itself) and FormControlLabel (for the name of the checkbox) from Material UI library.
import Checkbox from "@material-ui/core/Checkbox";
import FormControlLabel from "@material-ui/core/FormControlLabel";
  1. Next, import withstyles to customize the color, specifically using the color red.
import { withStyles } from '@material-ui/core/styles';
import { red } from "@material-ui/core/colors";
  1. Customize the styling using withstyles as shown below:
const RedCheckbox = withStyles({
  root: {
    color: red[900],
    '&$checked': {
      color: red[200],
    },
  },
  checked: {},
})((props) => <Checkbox color="default" {...props} />);
  1. Insert the customized Redcheckbox component inside the FormControlLabel element.
<FormControlLabel
      control={
        <RedCheckbox
          checked={state.checkedA}
          onChange={handleChange}
          name="checkedA"
        />
      }
      label="Monday"
 />

For a complete code example, check out this Demo. (Be sure to open the demo.js file)

If you're not satisfied with the default colors offered, you can easily customize the color palette. Refer to this link for more information.

Feel free to leave a comment if you require any assistance or further clarification.

Link to the MUI checkbox component -> https://v4.mui.com/components/checkboxes/#checkbox

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

NextJS 13 introduces Redux integration with the approuter pattern, streamlining the process of reducing the use of client-side code

Recently started using NextJS and came across various tutorials, including one from Vercel themselves - https://github.com/vercel/next.js/tree/canary/examples/with-redux The standard approach they suggest is- Setting up a Redux Provider "use cl ...

Customize React Bootstrap Slider Attributes on the Fly

Is it possible to dynamically set attributes for the React Bootstrap Slider control available at https://github.com/brownieboy/react-bootstrap-slider? For instance, can the control be disabled after the slideStop event? ...

Element positioning in Material UI is fluid and responsive across all devices, with the ability to place elements at

As a novice in layout design, I am currently developing a web application for both mobile and desktop using React and Material UI. My challenge lies in placing the app navigation at the bottom of the screen. The issue arises because the location of the app ...

In the latest version of Bootstrap, the carousel has been redesigned to smoothly transition across the entire page, providing a

I am currently in the process of learning Bootstrap 5 and decided to create a simple carousel by referring to the official documentation on Bootstrap 5 Carousel. Following their instructions, I copied their code and integrated it into my template. Howeve ...

Why is there a CSS reference failure in Express and how can it be resolved?

Below, you'll find a simple express server I've put together: // check out my basic Express server var express = require("express"); var app = express(); var bodyparser = require("body-parser"); app.use("/public/", express.static("./public/")); ...

Exploring the Power of JQuery Load and CSS styling

Currently, I am dynamically loading text into a div. However, I am encountering an issue where the content below this div is constantly shifting depending on the loaded text. Is there a way to restrict the space allocated to the div with the dynamic conte ...

Caution: It is important for every child in a list to have a distinctive "key" prop value. How can a shared key be used for multiple items?

When I click on a header from my list, an image should appear. However, I'm encountering an error that needs to be resolved. I've been struggling to find a solution for adding unique keys to multiple elements in order to eliminate the error. Des ...

Is there a way to modify the scroll ion-content's color scheme?

Is there a way to change the scroll color in Ionic to green, specifically for ion-content? Any suggestions on how I can achieve this? Below is my HTML code: <ion-header> <ion-toolbar> <ion-buttons slot="start"> ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

Encountering difficulties while trying to set up the Stripe npm package

I'm encountering a problem while attempting to add the Stripe NPM Package to my React project using npm. The terminal is showing this error message. PS C:\Users\adity\OneDrive\Desktop\stripe-payments> npm i @stripe/react-st ...

Linking HTML files across different directories

Here is the current structure of my folders: de/ index.html en/ index.html hu/ index.html img/ image.png style/ stylesheet.css I am wondering how I can link to the css file in the style folder and the image in the img folder directly ...

Tips for implementing styling in a material table within a reactjs application

Is there a different way to set the display property to inline-block since cellStyle doesn't seem to recognize it? How can I adjust the width of a specific column, let's say with the name title, without affecting the width of all other co ...

Removing error messages upon form reset initiated by an API request

Is there a way to clear error messages that appear beneath a text field when resetting form fields with values from an api call? In my Formik form, I have three fields that are loaded from an api database call along with a Reset button that reloads these ...

I'm looking for some help with creating a visualization using JavaScript or Python. Can anyone offer some guidance?

// Defining the dimensions and margins of the graph var width = 460 var height = 460 // Appending the svg object to the body of the page var svg = d3.select("#my_dataviz") .append("svg") .attr("width", width) .attr("height", height) // Reading ...

Updating state array within reducer leads to duplicate execution of calculations

const updateCartReducer = (state, action)=>{ console.log('running the updater function') const existingItemIndex = state.items.findIndex( (item) => item.restaurant === action.item.restaurant && ...

Angular 2 Form Error: Control Not Found

I'm facing an issue with Angular 2 Forms where adding more than one control seems to be getting ignored. Despite following numerous guides on how to properly implement this, none of the suggested methods seem to work in my case. In my template, I hav ...

NextJs not processing Bootstrap form submissions

I’m struggling to figure out why my form isn’t submitting when I click the submit button. The backend seems fine because Postman successfully sends the information to the database. However, nothing happens when I try to submit the form. My tech stack ...

Adjust the CSS styling to ensure that the div or iframe expands to fit the entire height of the

Having trouble making a map embedded via an iframe on a WordPress page display responsively at full height independent of the device? Check out: . To clarify, by full height, I mean that the bottom of the map should not extend beyond the screen, eliminati ...

Is there a method in JavaScript to prevent href="#" from causing a page refresh? This pertains to nyroModal

Is there a way to prevent <herf="#"> from causing a page refresh? I am currently working on improving an older .NET web project that utilizes nyroModal jQuery for displaying lightboxes. However, when I attempt to close the lightbox, nyroMo ...

Adding vibrant colors to the expansion panel within Material UI to enhance its visual appeal

How can I customize the color of the expansion panel in material ui when it is open? Is there a way to override this in material ui? Feel free to check out the code example at this link ...