简体   繁体   中英

How to add suffix icon to material-ui-pickers input field

This is my code:

<Box p={6}>
  <Grid container spacing={2}>
    <Grid item xs={6}>
      <TimePicker autoOk label={t('checkIn')} value={time1} onChange={handlecheckIn} clearable />
    </Grid>
    <Grid item xs={6}>
      <TimePicker autoOk label={t('checkOut')} value={time2} onChange={handleCheckOut} clearable />
    </Grid>

This is what i have now:

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

And i would like to get something like this, with arrow at the right end of time picker:

带向下箭头的时间选择器

And this is form after clicking Check In or Check Out:

在此处输入图像描述

1.Use the TimePicker props TextFieldComponent to customize the TextField props (not component)

2.Use TextField props InputProps to customize the input component with InputAdornment and normal endAdornment (suffix)

3.Pass default props with {...props} which is necessary for the default styles.

4.Notice we can pass the open status as a params in the nested arrow function.

import React, { useState } from "react";
import DateFnsUtils from "@date-io/date-fns"; // choose your lib
import { TimePicker, MuiPickersUtilsProvider } from "@material-ui/pickers";
import { TextField, InputAdornment } from "@material-ui/core";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";

const CustomizedTextField = open => props => {
  return (
    <TextField
      id="standard-basic"
      label="Standard"
      {...props}
      InputProps={{
        endAdornment: (
          <InputAdornment position="end">
            {open ? <ExpandMore /> : <ExpandLess />}
          </InputAdornment>
        )
      }}
    />
  );
};

function App() {
  const [selectedDate, handleDateChange] = useState(new Date());
  const [open, setOpen] = React.useState(false);
  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <TimePicker
        value={selectedDate}
        onChange={handleDateChange}
        open={open}
        onOpen={() => setOpen(true)}
        onClose={() => setOpen(false)}
        TextFieldComponent={CustomizedTextField(open)}
      />
    </MuiPickersUtilsProvider>
  );
}

export default App;

编辑 falling-smoke-wyyg2

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM