最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How to add endAdornment button in DateTimeField after migrating to @muix-date-pickers v7? - Stack Overflow

programmeradmin3浏览0评论

I recently upgraded from @mui/x-date-pickers v6 to v7, and my DateTimeField component stopped working due to a TypeScript error.

In v6, I was able to add a custom button to open the calendar using endAdornment inside slotProps.textField.InputProps, but in v7, this approach throws a TypeScript error.

Here's my component code:

<DateTimeField
  className={classes.dateTimeField}
  value={value}
  onChange={handleDateTimeFieldChange}
  format={DATE_FORMAT}
  slotProps={{
    textField: {
      error: Boolean(errorMessage),
      InputProps: {
        endAdornment: (
          <IconButton
            className={classes.openCalendarButton}
            onClick={handleOpenCalendar}
            title="Open calendar"
            size="small"
          >
            <CalendarWithClockIcon />
          </IconButton>
        ),
      },
    },
  }}
/>

This code worked fine in v6 but now results in the following TypeScript error in v7:

Type '{ className: string; value: Dayjs; onChange: (newDate: Dayjs) => void; format: string; slotProps: { textField: { error: boolean; InputProps: { endAdornment: Element; }; }; }; }' 
is not assignable to type 'Omit<Omit<FilledTextFieldProps | OutlinedTextFieldProps | StandardTextFieldProps, "select" | "type" | ... 7 more ... | "SelectProps">, keyof UseDateTimeFieldProps<...>>'.
  Types of property 'slotProps' are incompatible.
    Type '{ textField: { error: boolean; InputProps: { endAdornment: React.JSX.Element; }; }; }' 
    is not assignable to type '{ input?: SlotProps<ElementType<FilledInputProps, keyof IntrinsicElements>, {}, BaseTextFieldProps> | undefined; inputLabel?: SlotProps<...> | undefined; htmlInput?: SlotProps<...> | undefined; formHelperText?: SlotProps<...> | undefined; select?: SlotProps<...> | undefined; } | { ...; } | { ...; } | undefined'.ts(2322)

I tried different approaches, but I couldn't find a simple solution.

Question: What is the correct way to add an endAdornment button in DateTimeField in @mui/x-date-pickers v7?

Any help would be greatly appreciated!

I recently upgraded from @mui/x-date-pickers v6 to v7, and my DateTimeField component stopped working due to a TypeScript error.

In v6, I was able to add a custom button to open the calendar using endAdornment inside slotProps.textField.InputProps, but in v7, this approach throws a TypeScript error.

Here's my component code:

<DateTimeField
  className={classes.dateTimeField}
  value={value}
  onChange={handleDateTimeFieldChange}
  format={DATE_FORMAT}
  slotProps={{
    textField: {
      error: Boolean(errorMessage),
      InputProps: {
        endAdornment: (
          <IconButton
            className={classes.openCalendarButton}
            onClick={handleOpenCalendar}
            title="Open calendar"
            size="small"
          >
            <CalendarWithClockIcon />
          </IconButton>
        ),
      },
    },
  }}
/>

This code worked fine in v6 but now results in the following TypeScript error in v7:

Type '{ className: string; value: Dayjs; onChange: (newDate: Dayjs) => void; format: string; slotProps: { textField: { error: boolean; InputProps: { endAdornment: Element; }; }; }; }' 
is not assignable to type 'Omit<Omit<FilledTextFieldProps | OutlinedTextFieldProps | StandardTextFieldProps, "select" | "type" | ... 7 more ... | "SelectProps">, keyof UseDateTimeFieldProps<...>>'.
  Types of property 'slotProps' are incompatible.
    Type '{ textField: { error: boolean; InputProps: { endAdornment: React.JSX.Element; }; }; }' 
    is not assignable to type '{ input?: SlotProps<ElementType<FilledInputProps, keyof IntrinsicElements>, {}, BaseTextFieldProps> | undefined; inputLabel?: SlotProps<...> | undefined; htmlInput?: SlotProps<...> | undefined; formHelperText?: SlotProps<...> | undefined; select?: SlotProps<...> | undefined; } | { ...; } | { ...; } | undefined'.ts(2322)

I tried different approaches, but I couldn't find a simple solution.

Question: What is the correct way to add an endAdornment button in DateTimeField in @mui/x-date-pickers v7?

Any help would be greatly appreciated!

Share Improve this question edited Mar 18 at 0:16 Olivier Tassinari 8,6916 gold badges25 silver badges28 bronze badges asked Mar 14 at 21:46 Калдар КайратКалдар Кайрат 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In @mui/x-date-pickers v7, the slotProps.textField.InputProps prop is deprecated. You can use slots.textfield prop instead. Check the example below

发布评论

评论列表(0)

  1. 暂无评论