Customizing the style of the datepicker in Material UI with makeStyles is proving to be

I am having trouble adding a border to the datepicker below.
I tried applying inline styles in Web inspector and it worked. https://i.sstatic.net/Q4KAC.png

However, when I attempt to apply the style using makeStyles, nothing happens.

demo.js

import "date-fns";
import React from "react";
import DateFnsUtils from "@date-io/date-fns";
import {
  MuiPickersUtilsProvider,
  KeyboardTimePicker
} from "@material-ui/pickers";

import { makeStyles } from "@material-ui/core/styles";

export const styles = makeStyles(() => ({
  formControl: {
    border: "1px solid grey"
  }
}));

export default function MaterialUIPickers() {
  const classes = styles();
  const [selectedDate, setSelectedDate] = React.useState(
    new Date("2014-08-18T21:11:54")
  );

  const handleDateChange = (date) => {
    setSelectedDate(date);
  };

  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <KeyboardTimePicker
        margin="normal"
        id="time-picker"
        value={selectedDate}
        onChange={handleDateChange}
        KeyboardButtonProps={{
          "aria-label": "change time"
        }}
        classes={{
          formControl: classes.formControl
        }}
      />
    </MuiPickersUtilsProvider>
  );
}

CodeSandbox:
https://codesandbox.io/s/material-demo-forked-viq2m?file=/demo.js

Answer №1

Upon reviewing the log, it becomes apparent

https://i.sstatic.net/jPTPL.png

Since KeyboardTimePicker is derived from TextField, and TextField's CSS API only recognizes root (source), the recommended solution is to adjust the formControl key to root

      <KeyboardTimePicker
        margin="normal"
        id="time-picker"
        value={selectedDate}
        onChange={handleDateChange}
        KeyboardButtonProps={{
          "aria-label": "change time"
        }}
        classes={{
          root: classes.formControl
        }}
      />

https://codesandbox.io/s/material-demo-forked-mubg1?fontsize=14&hidenavigation=1&theme=dark

Answer №2

One reason for this is that @material-ui/pickers is a distinct development built on MUI.

Date/Time pickers are relatively straightforward controls from a user experience standpoint, which is why many users stick with the default appearance.

This is why there is no API available for component-specific classes to override stylesheets. The only way to customize existing stylesheets is through global material-ui theme overrides.

You can find more information about this in their documentation.

I have created an example with a red border.

Check it out here: https://codesandbox.io/s/material-demo-forked-wgpvf?file=/demo.js

Answer №3

After exploring various options, I stumbled upon a unique solution that differs from the other suggestions in this thread

const useStyles = makeStyles({
  root: {
    "& .MuiInputBase-root": {
      border: "1px solid red",
    }
  }
});
const classes = useStyles();

return (
  <MuiPickersUtilsProvider utils={DateFnsUtils}>
    <KeyboardDatePicker
      className={classes.root}
      {...}
    />
  </MuiPickersUtilsProvider>
);

In my assessment, this approach also stands out as a viable solution.

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

Tips for displaying identical tab content across various tabs using jquery or javascript

For instance, if we have tabs labeled as 1, 2, 3, 4, and 5, and a user has selected tabs 2, 3, and 4, how can we display the same content for those tabs while showing different content for the remaining ones? ...

Managing value state with several Formik forms within a single component

I'm currently in the process of constructing a web page using React and Formik. Within this form page, I have integrated three distinct Formik forms that are conditionally displayed based on a switch statement. The reason behind structuring it this wa ...

Try out a demonstration of react-monaco-editor on StackBlitz

I attempted to implement the react-monaco-editor library in StackBlitz. Here is a brief example: Link to StackBlitz import * as React from 'react'; import MonacoEditor from 'react-monaco-editor'; export default class EditorBasic exten ...

The placeholder text in the matInput field is not being displayed

As a newcomer to Angular, I am facing a challenge that has left me unable to find a solution. Below is the code snippet in question: <mat-form-field> <input matInput placeholder="ZIP" style="color:red;"> </mat-form-field& ...

React - prop's value not available yet when sent to child component

On my webpage, I have a section dedicated to displaying detailed information about a meeting. Users can edit this information by clicking on a button which opens up a modal containing the meeting object passed as follows: <AddMeetingModal show={isEd ...

Is there a way to incorporate a fonticon following an input field?

Here's the code snippet I'm working with: <div class="form-group"> <label for="site-title">Site title<span style="color: red;">*</span></label> <input class="form-control" type="text" value="" id="site-title" na ...

Using Vue to send information to Firebase

I could really use some assistance. Despite my best efforts over the past few days, and despite watching numerous tutorials and consulting various methods in Firebase documentation, I am still unable to make any progress. My current project involves build ...

After a postback, the Javascript function removes the displayed div

I am facing an issue with a page containing a button that uses JavaScript to display the content of a div. I also have an ASP.net validator control that triggers a postback. When I debug the JavaScript in Firebug, the div remains visible; however, if I all ...

Is it possible to consolidate all the CSS for HTML emails within the head section of the document?

Currently, I am starting with a MailChimp template as a foundation for my email design project. However, I've decided not to utilize MailChimp for sending the emails. Upon examining the MailChimp template, I noticed that there are approximately 330 l ...

What is the process of submitting a webhook request using a Google Docs extension?

Is it possible to develop a Google Docs add-on that can send an AJAX call to a webhook? I attempted the following code, but encountered an error. Error ReferenceError: "$" is not defined. (line 5, file "") Code function myFunction() { var arr = &ap ...

Integrating PayPal into your Node.js application with Express.js

Having successfully integrated PayPal into my website, I am now faced with the challenge of inserting the total in the PayPal syntax section of my code. In app.js, routes are created for my page on the website. In cart.js, the cart is created along with a ...

Problem encountered when transferring JSON data to PHP for file writing

After spending numerous hours researching potential solutions, I still can't seem to fix the issue at hand. Admittedly, I am fairly new to PHP, so it's possible that I am overlooking a small detail. The problem lies in the fact that I have a form ...

Ensure the date is displayed in the format of dd-mm-yyyy when using the input type=date

Here is the code I am currently using : <input type="date" class="form-control" id="training_date" name="training_date" placeholder=" select" value="" onfocus="(this.type='date')" onfocusout="(this.type='date')" max=<?php echo ...

Unable to display array values (using jQuery)

Help! I am having trouble rendering all my values. I have names (buttons) and when I click on them, I want to render the array behind it. Using .append() works, but the issue is that when I click again, it doesn't refresh and gets stacked. I am thinki ...

Creating a responsive modal in React.js: A step-by-step guide

Currently, I am working on a straightforward modal in React and am in the process of making it responsive. My goal is to have the modal display with fixed height and width on desktop, with the background unscrollable while the modal itself is scrollable. O ...

Using CSHTML, CSS, and Bootstrap to create nested tabs within tabs

I have a set of two tabs: boys and girls. Within each tab, there are dropdown options for different age groups. My goal is to display the selected age group under either the 'boys' or 'girls' tab. For example, if I choose 17-18, I wa ...

Adjusting the overflow of a parent div based on the position of the div within it by scrolling

I'm trying to create a page with 3 different sections: <div class="container" id="main-container"> <div class="section" id="profile"> Hello World </div> <div class="section" id="projects"> Hello World 2 ...

How can I center an anchor tag in the middle of the screen using Tailwind?

It's frustrating that I can't seem to find a solution to my current issue. I intended for this anchor tag to be centered on the screen, however, it stubbornly remains stuck to the left side. <div class="background"> <Navbar/ ...

An effective method for creating responsive layouts for custom-shaped HTML elements in a board game

Can anyone guide me on aligning spaces responsively on a board in HTML using Angular 1.x? I've tried using vw/vh's and %s, but consistency breaks on different view sizes. Any straightforward suggestions to address this issue? Avoiding multiple c ...

Ajax appends a single row, not an entire array

My ajax function is retrieving an array of data from the backend, but when I try to display it in the blade, only one row appears instead of multiple rows. List of Problems Only 1 row is appended by ajax The select option is empty when the results return, ...