React 和 Material UI:如何在 Select 和 InputLabel 之间包含一个 space?

React and Material UI: How to include a space between Select and InputLabel?

我正在使用 ReactMaterial UI 制作网络应用程序。我正在使用 Select 构建一个多 select 组件。它工作正常。请参阅下面的代码。

export const AvailableStations = () => {
    const [personName, setPersonName] = React.useState([]);
    const handleChangeMultiple = (event) => {
        const { options } = event.target;
        const value = [];
        for (let i = 0, l = options.length; i < l; i += 1) {
            if (options[i].selected) {
                value.push(options[i].value);
            }
        }
        setPersonName(value);
    };

    return (
        <div>
            <Box textAlign='center'>
            <FormControl sx={{ m: 0}}>
                <InputLabel m={1} shrink htmlFor="select-multiple-native">
                    Native
                </InputLabel>
                <Select sx={{minWidth: 170, maxWidth: 170}}
                    multiple
                    native
                    value={personName}
                    // @ts-ignore Typings are not considering `native`
                    onChange={handleChangeMultiple}
                    label="Native"
                    inputProps={{
                        id: 'select-multiple-native',
                    }}
                >
                    {names.map((name) => (
                        <option key={name} value={name}>
                            {name}
                        </option>
                    ))}
                </Select>
            </FormControl>
            </Box>
        </div>
    );
}

问题是 Select 内容与 InputLabel 重叠。请参见下图。

谁能帮我把它们分开?

您可以通过调整 select 上的 padding-top 并在 sx 属性中添加一些内容来解决此问题:

        <Select
          sx={{
            minWidth: 170,
            maxWidth: 170,
            pt: 1,
            "& .MuiNativeSelect-select": { pt: "8.5px" }
          }}
          multiple
          native

这会将 padding-top 的一个间距单位添加到 Select 的根元素。其中的下一个元素默认 padding-top 为 16.5px:

在外层元素上增加了padding-top的8px(一个间距单位)以避免选项文字与标签重叠后,默认的padding-top需要减少8px(16.5px - 8px = 8.5px) 以避免导致第一个选项上面的 space 大于默认值。

这是一个完整的工作示例:

import * as React from "react";
import InputLabel from "@mui/material/InputLabel";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";

const names = [
  "Oliver Hansen",
  "Van Henry",
  "April Tucker",
  "Ralph Hubbard",
  "Omar Alexander",
  "Carlos Abbott",
  "Miriam Wagner",
  "Bradley Wilkerson",
  "Virginia Andrews",
  "Kelly Snyder"
];

export default function MultipleSelectNative() {
  const [personName, setPersonName] = React.useState<string[]>([]);
  const handleChangeMultiple = (
    event: React.ChangeEvent<HTMLSelectElement>
  ) => {
    const { options } = event.target;
    const value: string[] = [];
    for (let i = 0, l = options.length; i < l; i += 1) {
      if (options[i].selected) {
        value.push(options[i].value);
      }
    }
    setPersonName(value);
  };

  return (
    <div>
      <FormControl sx={{ m: 1, minWidth: 120, maxWidth: 300 }}>
        <InputLabel shrink htmlFor="select-multiple-native">
          Native
        </InputLabel>
        <Select
          sx={{
            minWidth: 170,
            maxWidth: 170,
            pt: 1,
            "& .MuiNativeSelect-select": { pt: "8.5px" }
          }}
          multiple
          native
          value={personName}
          // @ts-ignore Typings are not considering `native`
          onChange={handleChangeMultiple}
          label="Native"
          inputProps={{
            id: "select-multiple-native"
          }}
        >
          {names.map((name) => (
            <option key={name} value={name}>
              {name}
            </option>
          ))}
        </Select>
      </FormControl>
    </div>
  );
}