英文:
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?
有人能帮忙吗?
英文:
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
<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 here
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论