Dialog 弹窗
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
使用指南
对话框
何时使用
对话框是用于在不离开主路径的情况下,提供用户快速执行简单的操作、确认用户信息或反馈提示的辅助窗口。
API
弹窗
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式类名的品牌前缀 | String | 'next-' |
className | 自定义类名 | String | - |
style | 自定义内联样式 | Object | - |
title | 对话框的标题 | any | - |
footer | 传入底部的内容 | Boolean/String/ReactNode | - |
footerAlign | 底部按钮的对齐方式可选值:'left', 'center', 'right' | Enum | 'right' |
visible | 控制对话框是否可见 | Boolean | - |
hasMask | 是否需要mask | Boolean | true |
closable | 'esc, mask, close', 详见closable | String/Boolean | 'esc,close' |
shouldUpdatePosition | 是否强制更新dialog的位置,在isFullScreen 为true且align为cc cc 的时候无效 | Boolean | - |
align | 浮层自定义位置 | String/Number | 'cc cc' |
animation | 配置动画的播放方式 | Object/Boolean | { in: 'fadeInDown', out: 'fadeOutUp' } |
onClose | 在点击关闭按钮的时候触发的函数签名:Function() => void | Function | - |
afterClose | 浮层关闭后触发的事件, 如果有动画,则在动画结束后触发签名:Function() => void | Function | - |
onOk | 在点击Ok按钮的时候触发的函数签名:Function() => void | Function | () => {} |
onCancel | 在点击Cancel按钮的时候触发的函数签名:Function() => void | Function | () => {} |
minMargin | 当dialog过高的时候距离viewport的最小边距,在isFullScreen 下无效。 | Number | 40 |
autoFocus | 当dialog弹出的时候是否自动获取焦点 | Boolean | true |
locale | 自定义国际化文案对象属性:ok: {String} 确认按钮文案cancel: {String} 取消按钮文案 | Object | - |
language | 自定义国际化语言可选值:'en-us', 'zh-cn', 'zh-tw' | Enum | - |
isFullScreen | 是否是启用使用CSS定位模式的对话框, 在该模式下面无需通过shouldUpdatePosition 来进行重新定位。 | Boolean | false |
代码示例
通过footerAlign
调整底部按钮的对齐方式
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
class App extends React.Component {
state = {
footerAlign: "left",
visible: false
};
map = ["left", "right", "center"];
onClose = () => {
this.setState({
visible: false
});
};
onClick = () => {
let { footerAlign } = this.state,
index = this.map.indexOf(footerAlign),
next = index + 1;
if (next >= this.map.length) {
next = 0;
}
this.setState({
footerAlign: this.map[next]
});
};
onOpen = () => {
this.setState({
visible: true
});
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
onClose={this.onClose}
title="Welcome to Alibaba.com"
footerAlign={this.state.footerAlign}
>
<Button onClick={this.onClick} type="primary">
Modify footerAlign
</Button>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</Dialog>
</span>
);
}
}
ReactDOM.render(<App />, mountNode);
第一个对话框
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
class App extends React.Component {
state = {
visible: false
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
closable="esc,mask,close"
onCancel={this.onClose}
onClose={this.onClose}
title="Alibaba.com"
>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false
});
};
}
ReactDOM.render(<App />, mountNode);
默认的footer为两个按钮,你可以自定义footer的内容
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
class App extends React.Component {
state = {
visible: false
};
render() {
const footer = (
<a onClick={this.onClose} href="javascript:;">
Close
</a>
);
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open dialog
</Button>
<Dialog
visible={this.state.visible}
footer={footer}
onClose={this.onClose}
title="Alibaba.com"
>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false
});
};
}
ReactDOM.render(<App />, mountNode);
设置isFullScreen为true的时候,无需通过shouldUpdatePosition进行重新定位。
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
const content = <p>View messages from buyers & suppliers</p>;
const largeContent = [];
for (let i = 0; i < 10; i++) {
largeContent.push(
<span key={i}>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</span>
);
}
class App extends React.Component {
state = {
visible: false,
content
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open Dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
isFullScreen
onClose={this.onClose}
title="Alibaba.com"
>
<Button type="primary" onClick={this.modifyContent}>
Modify content.
</Button>
{this.state.content}
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false,
content
});
};
modifyContent = () => {
this.setState({
content: largeContent
});
};
}
ReactDOM.render(<App />, mountNode);
内容很多的情况,可以通过minMargin控制边距
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
const content = [];
for (let i = 0; i < 10; i++) {
content.push(
<span key={i}>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</span>
);
}
class App extends React.Component {
state = {
visible: false
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
shouldUpdatePosition
minMargin={50}
onClose={this.onClose}
title="Alibaba.com"
>
{content}
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false,
content
});
};
}
ReactDOM.render(<App />, mountNode);
示例演示了在便捷调用和JSX模式下的语言切换方式
查看源码在线预览
import { Dialog, Button, LocaleProvider } from "@icedesign/base";
const popupConfirm = () => {
Dialog.confirm({
content: "confirm",
locale: {
ok: "确认",
cancel: "取消"
}
});
};
class App extends React.Component {
state = {
visible: false
};
render() {
return (
<span>
<Button type="primary" onClick={this.onOpen}>
Use en-us Open
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
onClose={this.onClose}
title="Alibaba.com"
>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</Dialog>
</span>
);
}
onOpen = () => {
LocaleProvider.set("en-us");
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false
});
};
}
ReactDOM.render(
<span>
<Button onClick={popupConfirm}>Confirm</Button>
<App />
</span>,
mountNode
);
多个对话框展示。
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
class App extends React.Component {
state = {
visible: false
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open Dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
onClose={this.onClose}
title="Alibaba.com"
>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
<App />
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false
});
};
}
ReactDOM.render(<App />, mountNode);
在使用alert
和confirm
的时候,返回promise,可以延迟执行函数
查看源码在线预览
import { Button, Dialog } from "@icedesign/base";
const popupConfirm = () => {
Dialog.confirm({
content: "confirm",
onOk: () => {
return new Promise(resolve => {
setTimeout(resolve, 2000);
});
}
});
};
ReactDOM.render(
<span>
<Button onClick={popupConfirm} type="primary">
confirm
</Button>
</span>,
mountNode
);
Dialog提供alert
和confirm
两种方式进行便捷调用
查看源码在线预览
import { Dialog, Button, LocaleProvider } from "@icedesign/base";
const popupAlert = () => {
Dialog.alert({
content: "Alert content",
closable: false,
title: "Alert",
onOk: () => {
Dialog.alert({ content: "alert content" });
}
});
};
const popupConfirm = () => {
Dialog.confirm({
content: "Confirm content",
title: "Confirm"
});
};
const setLocale = () => {
LocaleProvider.set("en-us");
};
const popupCustomize = () => {
const dialog = Dialog.alert({
needWrapper: false,
content: "Alert content",
title: "Alert",
footer: (
<Button type="primary" onClick={() => dialog.hide()}>
Another button
</Button>
)
});
};
ReactDOM.render(
<span>
<Button onClick={setLocale}>Set Locale</Button>
<Button onClick={popupAlert}>Alert</Button>
<Button onClick={popupConfirm}>Confirm</Button>
<Button onClick={popupCustomize}>Customize content & button</Button>
</span>,
mountNode
);
通过shouldUpdatePosition更新对话框的位置
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
const content = <p>View messages from buyers & suppliers</p>;
class App extends React.Component {
state = {
visible: false,
content
};
render() {
return (
<span>
<Button onClick={this.onOpen} type="primary">
Open Dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose}
onCancel={this.onClose}
shouldUpdatePosition
onClose={this.onClose}
title="Alibaba.com"
>
<Button type="primary" onClick={this.modifyContent}>
Modify content.
</Button>
{this.state.content}
</Dialog>
</span>
);
}
onOpen = () => {
this.setState({
visible: true
});
};
onClose = () => {
this.setState({
visible: false,
content
});
};
modifyContent = () => {
this.setState({
content: (
<div>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
</div>
)
});
};
}
ReactDOM.render(<App />, mountNode);
通过style设置dialog的宽度或者top等样式
查看源码在线预览
import { Dialog, Button } from "@icedesign/base";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false,
align: "cc cc",
style: {
width: "80%"
}
};
}
render() {
return (
<span>
<Button onClick={this.open.bind(this)} type="primary">
Open dialog
</Button>
<Dialog
visible={this.state.visible}
onOk={this.onClose.bind(this)}
onCancel={this.onClose.bind(this)}
onClose={this.onClose.bind(this)}
title="Welcome to Alibaba.com"
style={this.state.style}
align={this.state.align}
>
<h3>Your one-stop communication tool!</h3>
<ul>
<li>View messages from buyers & suppliers</li>
<li>Negotiate the details of your order</li>
</ul>
<Button onClick={this.setPosition.bind(this)}>Set position</Button>
</Dialog>
</span>
);
}
onClose() {
this.setState({
visible: false
});
}
open() {
this.setState({
visible: true
});
}
setPosition() {
this.setState({
align: false,
style: {
top: "10px"
}
});
}
}
ReactDOM.render(<App />, mountNode);