2
votes

How can I change the material UI Date picker icon?

I can't see it anywhere in the code or the API sections of the docs.

Here's a link to their docs: https://material-ui.com/components/pickers/

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

export default function MaterialUIPickers() {
  // The first commit of Material-UI
  const [selectedDate, setSelectedDate] = React.useState(new Date('2014-08-18T21:11:54'));

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

  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <Grid container justify="space-around">
        <KeyboardDatePicker
          disableToolbar
          variant="inline"
          format="MM/dd/yyyy"
          margin="normal"
          id="date-picker-inline"
          label="Date picker inline"
          value={selectedDate}
          onChange={handleDateChange}
          KeyboardButtonProps={{
            'aria-label': 'change date',
          }}
        />
      </Grid>
    </MuiPickersUtilsProvider>
  );
}

Everything else is working properly, I just need to edit the image to a different icon.

3
These are the docs you want to checkout for that component material-ui-pickers.dev/api/KeyboardDatePickerapachuilo
Thanks, I'll check this out!Matt Brody

3 Answers

5
votes

First include the Material icon font into your project, via Google Web Fonts:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

then you need to import the Icon component like so

import Icon from "@material-ui/core/Icon";

Then include the 'keyboardIcon' property in your KeyboardDatePicker component by wrapping the icon name (font ligature) with the Icon component like so:

<KeyboardDatePicker
      disableToolbar
      variant="inline"
      format="MM/dd/yyyy"
      margin="normal"
      id="date-picker-inline"
      label="Date picker inline"
      value={selectedDate}
      onChange={handleDateChange}
      KeyboardButtonProps={{
        'aria-label': 'change date',
      }}
      keyboardIcon={<Icon>add_circle</Icon>}
    />

If you're using Font Awesome instead then you have to supply the class name using the Icon component's className property:

<KeyboardDatePicker
      disableToolbar
      variant="inline"
      format="MM/dd/yyyy"
      margin="normal"
      id="date-picker-inline"
      label="Date picker inline"
      value={selectedDate}
      onChange={handleDateChange}
      KeyboardButtonProps={{
        'aria-label': 'change date',
      }}
      keyboardIcon={keyboardIcon={<Icon className="fa fa-plus-circle" />}}
    />
1
votes

My customize shared ui import {Datepicker} from '@dwp/ui'

 <Datepicker
            fullWidth
            classes={classes}
            format="dd/MM/yyyy"
            minDateMessage=""
            maxDateMessage=""
            value={selectedDate}
            coloricon={$white}
            InputProps={{
              classes: {
                underline: classes.underline,
                disabled: classes.disabled,
              },
            }}
            onChangeDate={(value) => handleDateChange(value)}
            disabled
          />

source of shared datepicker from `@dwp/ui

export function Datepicker(props) {
  const {
    minDate = new Date(),
    onChangeDate,
    value,
    classes,
    coloricon,
    InputProps,
    disabled,
  } = props
  const onChange = (payload) => onChangeDate(payload)

  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils} locale={localeMap['au']}>
      <KeyboardDatePicker
        variant="inline"
        minDate={minDate}
        format="dd/MM/yyyy"
        margin="normal"
        placeholder="10/10/2018"
        onChange={(payload) => {
          onChange(payload)
        }}
        KeyboardButtonProps={{
          'aria-label': 'change date',
        }}
        value={value || minDate}
        keyboardIcon={
          <A.QueryBuilderIcon
            coloricon={disabled ? `rgba(255, 255, 255, 0.36)` : coloricon}
          />
        }
        className={classes.underline}
        InputProps={InputProps}
        disabled={disabled}
      />
    </MuiPickersUtilsProvider>
  )
}

A.QueryBuilderIcon = styled(QueryBuilderIcon)`
  color: ${(props) => props.coloricon};
`
0
votes

You can use keyboardIcon props in <KeyboardDatePicker component

<KeyboardDatePicker
                margin="normal"
                id="date-picker-dialog"
                format="MM/dd/yyyy"
                value={selectedDate}
                onChange={handleDateChange}
                KeyboardButtonProps={{
                    'aria-label': 'change date',
                }}
                keyboardIcon={<img src="https://.../calendar.png" alt="calendar" width="33px" height="33px"/>}
            />

You can also use anouther tag inside this keyboardIcon property (not only <img />):

  • <svg>
  • <Icon> (from material ui)
  • <div>
  • etc...