I'm currently utilizing the react mui package, specifically @mui/x-date-pickers. Could someone kindly provide guidance on how to customize the color of the

I am currently working with MUI in React and using the DatePicker component from the @mui/x-date-pickers library. The version I am using is 6.0.3. I have encountered an issue where, upon selecting the first day, the specified color changes as shown in the image below. When reopening the calendar window, the default color is displayed until clicking on another area, which then changes the color back to the specified one. Is there a solution to this problem that anyone can provide? I have tried searching for a fix but haven't had any luck, so I'm reaching out here for help. Please advise if my search approach is incorrect.

https://i.stack.imgur.com/bIEPe.png https://i.stack.imgur.com/0Ixb0.png https://i.stack.imgur.com/efVCM.png

Below is the code snippet I'm using:

import { LGTheme } from '@libs/color';
import { DatePicker } from '@mui/x-date-pickers';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { koKR } from '@mui/x-date-pickers/locales';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

const DatePickerTest = () => {
    return (
        <LocalizationProvider
            dateAdapter={AdapterDayjs}
            adapterLocale={'ko'}
            localeText={koKR.components.MuiLocalizationProvider.defaultProps.localeText}
            dateFormats={{ monthAndYear: 'YYYY년 MM월' }}
        >
            <DatePicker
                label=""
                format="YYYY-MM-DD"
                slotProps={{
                    textField: {
                        size: 'small',
                    },
                    day: {
                        sx: {
                            ['&[data-mui-date="true"] .Mui-selected']: {
                                // Reset the background color of the selected date
                                backgroundColor: 'blue',
                            },
                            ':not(.Mui-selected)': {
                                backgroundColor: '#fff',
                                borderColor: LGTheme[0],
                            },
                            '&.Mui-selected': {
                                color: '#fff',
                                backgroundColor: LGTheme[0],
                                borderColor: LGTheme[0],
                                ':hover': {
                                    color: '#fff',
                                    backgroundColor: LGTheme[0],
                                    borderColor: LGTheme[0],
                                },
                            },
                            ':hover': {
                                color: '#fff',
                                backgroundColor: LGTheme[0],
                                borderColor: LGTheme[0],
                            },
                        },
                    },
                }}
            />
        </LocalizationProvider>
    );
};

export default DatePickerTest;


Answer №1

If you are encountering an issue with the styling of your date picker, it could be due to the use of the .Mui-selected class which only affects the selected date and not the current date. The current date is highlighted when you open the calendar and might not match the selected date. To target the current date, you should utilize the .MuiPickersDay-root class in your sx prop.

To resolve this problem, include both the .MuiPickersDay-root and .Mui-selected classes in your sx prop like this:

<DatePicker
  slotProps={{
    day: {
      sx: {
        "&.MuiPickersDay-root.Mui-selected": {
          backgroundColor: "red",
        },
      },
    },
  }}
/>

You can view a complete example here.

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

Error: The argument 'IParams' is not compatible with the parameter type 'IParams' in Typescript with NextJS14

I encountered an error in my code while using Prisma with Next.js 14 and TypeScript. The issue arises when trying to load product details via product ID. The error is captured in the screenshot below. Failed to compile. ./app/product/[productId]/page.tsx:1 ...

What happens if I don't associate a function or method in the React class component?

Take a look at this straightforward code snippet that updates a count using two buttons with different values. import "./App.css"; import React, { Component } from "react"; class App extends React.Component { // Initializing state ...

Even with employing Cors alongside Axios, I continue to encounter the following issue: The requested resource does not have the 'Access-Control-Allow-Origin' header

When working with a MEAN stack app, I had no issues with CORS. However, upon transitioning to the MERN stack, I encountered an error related to CORS despite having it implemented in the backend: Access to XMLHttpRequest at 'http://localhost:5000/api/ ...

Getting the canonical URL for dynamic pages in NextJS. How can it be done?

Previous to nextjs 9.4, I relied on the next-absolute-url package to provide the origin in getInitialProps. With Next.js 9.5 and the implementation of Automatic Optimization, using getServerSideProps and getStaticProps is preferred. Unfortunately, the nex ...

What is the best way to link my backend (nodejs) with my React application, both of which are operating on Gitpod

I am attempting to link my backend to the React app using the proxy in package.json, but an error keeps popping up on the front page of the React app saying "invalid host header". Initially, I tried using "proxy": "http://localhost:5000", and then I attem ...

Having trouble restricting the choices to only 2 items when using react hooks

My React-hooks code currently does not enforce a limit of 2 selections for items, and I am unable to display a validation message "Maximum items has been selected". What could be causing this issue? Check out the code here. import React, { useRef, useEffe ...

Customize material-ui themes using useStyles / jss

Is it possible to customize the Material-UI theme using styles without relying on !important? const customTheme = createMuiTheme({ overrides: { MuiInputBase: { input: { background: '#dd7711', padding: 10, }, ...

Module '@tanstack/react-table' cannot be located even though it has been successfully installed

Currently, I am tackling a TypeScript React project and encountering an issue while attempting to import ColumnDef from @tanstack/react-table in my columns.tsx file. import { ColumnDef } from "@tanstack/react-table"; export type Payment = { id ...

Master the Art of Scrollbar Control in Angular!

I am currently developing a chat web application that functions similar to gchat. One of the key features I'm trying to implement is an alert notification when the scrollbar is in the middle of the div, indicating a new message. If the scrollbar is at ...

The table is not properly displaying within the parent scrolled div due to issues with the fixed positioning

Apologies for any language barrier. I am encountering an issue with a PHP page that includes a table meant to be displayed as position:fixed; however, it consistently appears above the scrollbars and does not stay within the parent div. View screenshot he ...

Tips for including multiple spaces within a cell of a table

I've come across an issue while working on a table and attempting to insert text with multiple spaces. For example, I want to add the text HELLO<1stSPACE><2ndSPACE>WORLD. However, when I enter it as HELLO WORLD, it only displays with a single s ...

Applying Material UI class in React: Troubleshooting an error with your hook call

Recently, I have started using React and encountered an issue with a hook call. I understand the root cause of the problem but unsure how to resolve it without starting from scratch. Here is the snippet of the code: import { Fragment, PureComponent } from ...

Create a fresh Howler Instance within Action Creators and trigger actions based on events

I am currently working on developing a React.js + Redux audio player that utilizes the Howler.js Library to handle audio from an API. The functionality involves dispatching actions from control buttons within the player and other components like a track li ...

Can CSS be used to automatically focus on a div element?

Can CSS be used to set autofocus on a div element? <div class="form-group" style="margin-left:10px"> <label for="organizationContainer" class="nmc-label">@Res.GroupStrings.CreateNewGroupOrganization</label> <div id="organiza ...

Exploring different data fetching methods in NextJS - examining the multitude of options available and why they are detailed in the documentation (prior to Next.JS 13.4)

After delving into the world of data fetching methods in NextJS, I have explored several options. Within the documentation, there are around 4 to 5 key terms related to Data Fetching: getStaticProps getStaticPaths getServerSideProps Incremental Static Re ...

How can we incorporate a Material-UI skeleton to display API response data effectively?

I have incorporated the material-ui skeleton (Shimmer effect) to display data fetched from an API. { accountData.usersAccountData.map((userData, index) => ( // ...UI code ) ) } The output is shown below : It can be observed that I obtained 3 objec ...

Adjust the size of the container DIV based on the content within the child DIV

I have been searching for a solution to my question without success. Here is the issue: I have a DIV that acts as a container for a Post in my project. Inside this post are classes for the poster avatar, name, timestamp, text, and more. The problem arise ...

What is the process of activating the select all capability in the material-ui/SelectField component?

Link to Documentation I am currently referring to the provided documentation in order to incorporate the Select-field component. However, I have not been able to locate information on how to implement a "select all" feature within this component. Import s ...

I'm encountering an issue with my react-dropdown-select component where the item

I am encountering an issue while trying to parse the selected dropdown item into a Prop. The error message I receive is: TypeError: Cannot read property 'name' of undefined I have tried creating a new Prop and parsing it through there, but it di ...

Troubleshooting: Difficulty with jQuery's resize event handler not functioning properly with

I'm currently facing an issue where I want a divider to adjust its size when another divider changes size due to new content being loaded into it. However, even after binding the resize event handler to the divider that loads the content, the event do ...