Mui DatePicker v6:通过插槽更改默认图标及其位置

huangapple go评论107阅读模式
英文:

Mui DatePicker v6: How to change default icon and its position through slots

问题

我需要这个:
Mui DatePicker v6:通过插槽更改默认图标及其位置

而不是这个:
Mui DatePicker v6:通过插槽更改默认图标及其位置

但现在日期选择器(@mui/x-date-pickers 6.x)已经切换到插槽,网络上几乎没有示例。

英文:

I need this:
Mui DatePicker v6:通过插槽更改默认图标及其位置

instead of this:
Mui DatePicker v6:通过插槽更改默认图标及其位置

But now the date picker(@mui/x-date-pickers 6.x) has switched to slots, and there are very few examples on the Web.

答案1

得分: 4

以下是翻译好的代码部分:

Codesandbox: https://codesandbox.io/s/mui-datepicker-change-icon-and-its-position-ylzn4n?file=/demo.tsx

为字段添加startAdornment的代码

import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import BugReportIcon from "@mui/icons-material/BugReport";

//...

<DatePicker
  label="mui datapicker"
  slotProps={{
    textField: {
      InputProps: { startAdornment: <BugReportIcon /> }
    }
  }}
/>

更改默认日期选择器图标的代码

<DatePicker
  slots={{
    inputAdornment: BugReportIcon
  }}
/>

更改图标及其位置的代码

<DatePicker
  slots={{
    inputAdornment: BugReportIcon
  }}
  slotProps={{
    inputAdornment: {
      position: 'start'
    }
  }}
/>
英文:

Codesandbox: https://codesandbox.io/s/mui-datepicker-change-icon-and-its-position-ylzn4n?file=/demo.tsx

Code for adding startAdornment for your field:

import { DatePicker } from &#39;@mui/x-date-pickers/DatePicker&#39;
import BugReportIcon from &quot;@mui/icons-material/BugReport&quot;;

//...

&lt;DatePicker
  label=&quot;mui datapicker&quot;
  slotProps={{
    textField: {
      InputProps: { startAdornment: &lt;BugReportIcon /&gt; }
    }
  }}
/&gt;

Code for changing the default date picker icon:

   &lt;DatePicker
     slots={{
       inputAdornment: BugReportIcon
     }}
   /&gt;

Code for change icon and its position:

&lt;DatePicker
  slots={{
    inputAdornment: BugReportIcon
  }}
  slotProps={{
    inputAdornment: {
      position: &#39;start&#39;
    }
  }}
/&gt;

答案2

得分: 0

以下是翻译好的部分:

以下对我有用

    const [open, setOpen] = React.useState(false);

    const getInputAdornment = () => {
        return (
            <InputAdornment position="start">
                <IconButton onClick={() => setOpen(true)}>
                    <BugReportIcon />
                </IconButton>
            </InputAdornment>
        );
    };
    
    <DatePicker
        onClose={() => setOpen(false)}
        open={open}
        slots={{ inputAdornment: getInputAdornment }}
    />
英文:

the following worked for me:

const [open, setOpen] = React.useState(false);

const getInputAdornment = () =&gt; {
    return (
        &lt;InputAdornment position=&quot;start&quot;&gt;
            &lt;IconButton onClick={() =&gt; setOpen(true)}&gt;
                &lt;BugReportIcon /&gt;
            &lt;/IconButton&gt;
        &lt;/InputAdornment&gt;
    );
};

&lt;DatePicker
    onClose={() =&gt; setOpen(false)}
    open={open}
    slots={{ inputAdornment: getInputAdornment }}
/&gt;

huangapple
  • 本文由 发表于 2023年6月19日 09:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定