垂直对齐一个按钮和一个下拉菜单。

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

Vertically align a button and a dropdown

问题

I would like to make a row, where there is a title on the left and several buttons and a dropdown on the right.

我想创建一行,在左边有一个标题,在右边有几个按钮和一个下拉菜单。

I use Flexbox to arrange their horizontal placement.

我使用Flexbox来排列它们的水平位置。

Now, the dropdown (e.g., Cat) is not vertically-aligned with the buttons. Notice that if we remove the icons (.e.g, iconProps={{ iconName: "Back" }}), they will be aligned.

现在,下拉菜单(例如,Cat)与按钮垂直对齐。请注意,如果我们删除图标(例如,iconProps={{ iconName: "Back" }}),它们将对齐。

I tried to put style={{ minWidth: "auto", marginTop: "-10px" }} or style={{ minWidth: "auto", paddingTop: "-10px" }} for the dropdown, but it did not work.

我尝试为下拉菜单添加style={{ minWidth: "auto", marginTop: "-10px" }}style={{ minWidth: "auto", paddingTop: "-10px" }},但没有起作用。

Could anyone help?

有人能帮忙吗?

Link to Code Sandbox

英文:

I would like to make a row, where there is a title on the left and several buttons and a dropdown on the right.

I use Flexbox to arrange their horizontal placement.

Now, the dropdown (e.g., Cat) is not vertically-aligned with the buttons. Notice that if we remove the icons (.e.g, iconProps={{ iconName: "Back" }}), they will be aligned.

I tried to put style={{ minWidth: "auto", marginTop: "-10px" }} or style={{ minWidth: "auto", paddingTop: "-10px" }} for the dropdown, but it did not work.

Could anyone help?

https://codesandbox.io/s/peaceful-mopsa-2qr5qu?file=/example.tsx

import { makeStyles, shorthands } from "@fluentui/react-components";
import {
  Dropdown,
  Option,
  OptionGroup,
  DropdownProps
} from "@fluentui/react-components/unstable";
import { CommandBarButton } from "@fluentui/react/lib/Button";
import * as React from "react";
import { initializeIcons } from "@fluentui/react/lib/Icons";

initializeIcons(/* optional base url */);
const useStyles = makeStyles({
  dropdown: {
    // removes default border around the dropdown
    ...shorthands.borderWidth(0),
    // removes the blue bottom border when the dropdown is open
    "::after": {
      borderBottomWidth: 0
    }
  }
});
export const Grouped = (props: Partial<DropdownProps>) => {
  const land = ["Cat", "Dog", "Ferret", "Hamster"];
  const styles = useStyles();
  return (
    <div style={{ display: "flex", justifyContent: "space-between" }}>
      <div style={{ flexBasis: "auto" }}>
        <span style={{ lineHeight: "2.7rem" }}>Title</span>
      </div>
      <div style={{ flexBasis: "auto" }}>
        <CommandBarButton
          styles={{ root: { height: 44 } }}
          iconProps={{ iconName: "Back" }}
          ariaLabel="Back"
          text="Button 1"
          disabled={false}
          checked={false}
        />
        <CommandBarButton
          styles={{ root: { height: 44 } }}
          iconProps={{ iconName: "Up" }}
          text="Button 2"
          disabled={false}
          checked={false}
        />

        <Dropdown
          className={styles.dropdown}
          style={{ minWidth: "auto" }}
          defaultSelectedOptions={["Cat"]}
          {...props}
        >
          <OptionGroup label="Land">
            {land.map((option) => (
              <Option key={option} disabled={option === "Ferret"}>
                {option}
              </Option>
            ))}
          </OptionGroup>
        </Dropdown>
      </div>
    </div>
  );
};

答案1

得分: 1

以下是翻译好的部分:

有一个包装按钮和下拉菜单的 div。将 flex 应用于该 div,然后也应用于 dropdown 将会得到您想要的输出。

// 这里使用 Flex
<div style={{ display: "flex" }}>
  <CommandBarButton
    styles={{ root: { height: 44 } }}
    iconProps={{ iconName: "Back" }}
    ariaLabel="Back"
    text="Button 1"
    disabled={false}
    checked={false}
  />
  <CommandBarButton
    styles={{ root: { height: 44 } }}
    iconProps={{ iconName: "Up" }}
    text="Button 2"
    disabled={false}
    checked={false}
  />

  <Dropdown
    className={styles.dropdown}
    // 这里使用 Flex
    style={{
      minWidth: "auto",
      display: "flex",
    }}
    defaultSelectedOptions={["Cat"]}
    {...props}
  >
    <OptionGroup label="Land">
      {land.map((option) => (
        <Option key={option} disabled={option === "Ferret"}>
          {option}
        </Option>
      ))}
    </OptionGroup>
  </Dropdown>
</div>;

CodeSandbox:https://codesandbox.io/s/elegant-yalow-4uheom?file=/example.tsx:1554-1570

英文:

There is a div wrapping the buttons and the dropdown. Applying flex to that div and then to the dropdown too will give you the output that you want.

// Flex here
&lt;div style={{ display: &quot;flex&quot; }}&gt;
  &lt;CommandBarButton
    styles={{ root: { height: 44 } }}
    iconProps={{ iconName: &quot;Back&quot; }}
    ariaLabel=&quot;Back&quot;
    text=&quot;Button 1&quot;
    disabled={false}
    checked={false}
  /&gt;
  &lt;CommandBarButton
    styles={{ root: { height: 44 } }}
    iconProps={{ iconName: &quot;Up&quot; }}
    text=&quot;Button 2&quot;
    disabled={false}
    checked={false}
  /&gt;

  &lt;Dropdown
    className={styles.dropdown}
    // Flex here
    style={{
      minWidth: &quot;auto&quot;,
      display: &quot;flex&quot;,
    }}
    defaultSelectedOptions={[&quot;Cat&quot;]}
    {...props}
  &gt;
    &lt;OptionGroup label=&quot;Land&quot;&gt;
      {land.map((option) =&gt; (
        &lt;Option key={option} disabled={option === &quot;Ferret&quot;}&gt;
          {option}
        &lt;/Option&gt;
      ))}
    &lt;/OptionGroup&gt;
  &lt;/Dropdown&gt;
&lt;/div&gt;;

CodeSandbox : https://codesandbox.io/s/elegant-yalow-4uheom?file=/example.tsx:1554-1570

huangapple
  • 本文由 发表于 2023年1月6日 11:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026584.html
匿名

发表评论

匿名网友

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

确定