如何在MUI React的TimePicker组件中设置高度、宽度和边框半径?

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

How to set height, width and border-radius in TimePicker component - MUI React

问题

我试图改变TimePicker中输入的高度、宽度和边框半径等属性,但没有成功。有谁知道如何使用sx或slotProps来进行更改吗?这是我的当前代码。

<TimePicker
  ampm={false}
  views={['hours', 'minutes', 'seconds']}
  sx={{
    backgroundColor: "#333335",
    font: "white",
    color: "#FFFF",
    textDecoration: "bold",
    input: {
      color: "#FFFF",
      fontSize: "1.4rem",
    },
    "& .MuiTimePicker-root": {
      backgroundColor: "#222223",
    },
    "& .MuiTimePicker-input": {
      color: "#FFFF",
      fontSize: "1.2rem",
    },
  }}
  slotProps={{
    popper: {
      sx: {
        "& .MuiList-root": {
          backgroundColor: "#333335",
        },
        "& .MuiMenuItem-root": {
          "&.Mui-selected": {
            backgroundColor: "#04395E",
            color: "white"
          },
          color: "white"
        },
        "& .MuiDialogActions-root": {
          backgroundColor: "#333335",
        },
        "& .MuiSvgIcon-root": {
          "&.MuiSvgIcon-fontSizeMedium": {
            backgroundColor: "#04395E",
            color: "white"
          },
          color: "white"
        },
      },
    },
  }}
  value={selectedTime}
  onChange={(newTime) => setSelectedTime(newTime)}
  timeSteps={{ hours: 1, minutes: 1, seconds: 1 }}
/>

如何在MUI React的TimePicker组件中设置高度、宽度和边框半径?

英文:

I'm trying to change some properties like height, width and border-radius of the input in TimePicker, but with no success. Does anyone knows how to do change it using sx or slotProps? Here is my current code.

&lt;TimePicker
          ampm={false}
          views={[&#39;hours&#39;, &#39;minutes&#39;, &#39;seconds&#39;]}
          sx={{
            backgroundColor: &quot;#333335&quot;,
            font: &quot;white&quot;,
            color: &quot;#FFFF&quot;,
            textDecoration: &quot;bold&quot;,
            input: {
              color: &quot;#FFFF&quot;,
              fontSize: &quot;1.4rem&quot;,
            },
              &quot;&amp; .MuiTimePicker-root&quot;: {
              backgroundColor: &quot;#222223&quot;,
            },
            &quot;&amp; .MuiTimePicker-input&quot;: {
              color: &quot;#FFFF&quot;,
              fontSize: &quot;1.2rem&quot;,
            },
          }}
          slotProps={{
            popper: {
              sx: {
                &quot;&amp; .MuiList-root&quot;: {
                  backgroundColor: &quot;#333335&quot;,
                },
                &quot;&amp; .MuiMenuItem-root&quot;: {
                  &quot;&amp;.Mui-selected&quot;: {
                    backgroundColor: &quot;#04395E&quot;,
                    color: &quot;white&quot;
                  },
                  color: &quot;white&quot;
                },
                &quot;&amp; .MuiDialogActions-root&quot;: {
                  backgroundColor: &quot;#333335&quot;,
                },
                &quot;&amp; .MuiSvgIcon-root&quot;: {
                  &quot;&amp;.MuiSvgIcon-fontSizeMedium&quot;: {
                    backgroundColor: &quot;#04395E&quot;,
                    color: &quot;white&quot;
                  },
                  color: &quot;white&quot;
                },
              },
            },
          }}
          value={selectedTime}
          onChange={(newTime: any) =&gt; setSelectedTime(newTime)}
          timeSteps={{hours: 1, minutes: 1, seconds: 1}}
/&gt;

如何在MUI React的TimePicker组件中设置高度、宽度和边框半径?

答案1

得分: 1

你需要自定义slotProps内的textfield以编辑高度、宽度和边框半径。您可以检查下面的代码 👇

import OutlinedInputClasses from '@mui/material/OutlinedInput/outlinedInputClasses';

<TimePicker
  label="Basic time picker"
  slotProps={{
    textField: {
      sx: {
        borderRadius: 6,
        fieldset: { borderRadius: 6 },
        /* 如果您的 TextField 变体更改为 "filled",请使用 FilledInputClasses,否则使用以下内容 */
        [`.${OutlinedInputClasses.root}`]: {
          height: 80,
          width: 300,
        },
        /* 如果您更改了默认高度,您需要调整输入标签的位置 */
        '& .MuiInputLabel-root': { lineHeight: 3 },
      },
    },
  }}
/>
 注意确保保持 `borderRadius`  `fieldset: {borderRadius: _}` 具有相同的值

希望能帮助到您

<details>
<summary>英文:</summary>

You need to customize `textfield` inside `slotProps` to edit height, width and borderRadius. You can check the code below &#128071;

    import OutlinedInputClasses from &#39;@mui/material/OutlinedInput/outlinedInputClasses&#39;

    &lt;TimePicker
      label=&quot;Basic time picker&quot;
      slotProps={{
        textField: {
          sx: {
            borderRadius: 6,
            fieldset: { borderRadius: 6 },
          /* Use FilledInputClasses if your TextField variant changed to &quot;filled&quot; otherwise use below */
          [`.${OutlinedInputClasses.root}`]: {
             height: 80,
             width: 300,
          },
          /* If you change the default height, you need to adjust the Input Label position as well */
          &#39;&amp; .MuiInputLabel-root&#39;: { lineHeight: 3 },
         },
        },
      }}
    /&gt;
 Note: Make sure keep the `borderRadius` and `fieldset: {borderRadius: _}` to have the same values.

I hope it helps.

</details>



huangapple
  • 本文由 发表于 2023年8月4日 04:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831563.html
匿名

发表评论

匿名网友

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

确定