如何在 Fluent UI 对话框组件中使按钮居中?
How to center button in Fluent UI Dialog component?
Fluent UI 中的对话框组件有 DialogFooter。我想在该页脚内放置一个按钮,我希望它位于中心。如何实现?我无法将其从默认位置移动。
示例代码(取自[官方文档][https://developer.microsoft.com/en-us/fluentui#/controls/web/dialog#IDialogStyles],稍作修改。最重要的部分应该在代码的底部:
import * as React from 'react';
import { Dialog, DialogFooter } from 'office-ui-fabric-react/lib/Dialog';
import { DefaultButton, PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { ChoiceGroup } from 'office-ui-fabric-react/lib/ChoiceGroup';
import { useBoolean } from '@uifabric/react-hooks';
const modelProps = {
isBlocking: true,
topOffsetFixed: true,
};
const options = [
{
key: 'A',
iconProps: { iconName: 'CalendarDay' },
text: 'Day',
},
{
key: 'B',
iconProps: { iconName: 'CalendarWeek' },
text: 'Week',
},
{
key: 'C',
iconProps: { iconName: 'Calendar' },
text: 'Month',
},
];
export const DialogTopOffsetFixedExample: React.FunctionComponent = () => {
const [hideDialog, { toggle: toggleHideDialog }] = useBoolean(true);
const [optionSelected, setOptionSelected] = React.useState('A');
const onChange = (ev: React.FormEvent<HTMLInputElement>, option: any): void => {
setOptionSelected(option.key);
};
return (
<>
<DefaultButton secondaryText="Opens the Sample Dialog" onClick={toggleHideDialog} text="Open Dialog" />
<Dialog hidden={hideDialog} onDismiss={toggleHideDialog} modalProps={modelProps}>
<ChoiceGroup
label="Pick one icon"
options={options}
// eslint-disable-next-line react/jsx-no-bind
onChange={onChange}
required
/>
{optionSelected === 'A' && (
<div>
<h1>Description</h1>
<div>
{' '}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.{' '}
</div>
</div>
)}
{optionSelected === 'B' && (
<div>
<h1>Description</h1>
<div>
{' '}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat.{' '}
</div>
</div>
)}
{optionSelected === 'C' && (
<div>
<h1>Description</h1>
</div>
)}
<DialogFooter>
<PrimaryButton onClick={toggleHideDialog} text="Save" />
</DialogFooter>
</Dialog>
</>
);
};
看起来像这样:
如何将按钮移动到我画蓝色“圆圈”的中心?
您可以在 DialogFooter
中放置一个 flex 容器,然后开始将按钮居中放置。
这是一个例子:
<DialogFooter>
<div style={{display: "flex", justifyContent: "center"}}>
<PrimaryButton onClick={toggleHideDialog} text="Save" />
</div>
</DialogFooter>
Fluent UI 中的对话框组件有 DialogFooter。我想在该页脚内放置一个按钮,我希望它位于中心。如何实现?我无法将其从默认位置移动。
示例代码(取自[官方文档][https://developer.microsoft.com/en-us/fluentui#/controls/web/dialog#IDialogStyles],稍作修改。最重要的部分应该在代码的底部:
import * as React from 'react';
import { Dialog, DialogFooter } from 'office-ui-fabric-react/lib/Dialog';
import { DefaultButton, PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { ChoiceGroup } from 'office-ui-fabric-react/lib/ChoiceGroup';
import { useBoolean } from '@uifabric/react-hooks';
const modelProps = {
isBlocking: true,
topOffsetFixed: true,
};
const options = [
{
key: 'A',
iconProps: { iconName: 'CalendarDay' },
text: 'Day',
},
{
key: 'B',
iconProps: { iconName: 'CalendarWeek' },
text: 'Week',
},
{
key: 'C',
iconProps: { iconName: 'Calendar' },
text: 'Month',
},
];
export const DialogTopOffsetFixedExample: React.FunctionComponent = () => {
const [hideDialog, { toggle: toggleHideDialog }] = useBoolean(true);
const [optionSelected, setOptionSelected] = React.useState('A');
const onChange = (ev: React.FormEvent<HTMLInputElement>, option: any): void => {
setOptionSelected(option.key);
};
return (
<>
<DefaultButton secondaryText="Opens the Sample Dialog" onClick={toggleHideDialog} text="Open Dialog" />
<Dialog hidden={hideDialog} onDismiss={toggleHideDialog} modalProps={modelProps}>
<ChoiceGroup
label="Pick one icon"
options={options}
// eslint-disable-next-line react/jsx-no-bind
onChange={onChange}
required
/>
{optionSelected === 'A' && (
<div>
<h1>Description</h1>
<div>
{' '}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.{' '}
</div>
</div>
)}
{optionSelected === 'B' && (
<div>
<h1>Description</h1>
<div>
{' '}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat.{' '}
</div>
</div>
)}
{optionSelected === 'C' && (
<div>
<h1>Description</h1>
</div>
)}
<DialogFooter>
<PrimaryButton onClick={toggleHideDialog} text="Save" />
</DialogFooter>
</Dialog>
</>
);
};
看起来像这样:
如何将按钮移动到我画蓝色“圆圈”的中心?
您可以在 DialogFooter
中放置一个 flex 容器,然后开始将按钮居中放置。
这是一个例子:
<DialogFooter>
<div style={{display: "flex", justifyContent: "center"}}>
<PrimaryButton onClick={toggleHideDialog} text="Save" />
</div>
</DialogFooter>