Message 信息提示

如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @alifd/next@latest -S

开发指南

API

Message

参数说明类型默认值
size反馈大小可选值:'medium', 'large'Enum'medium'
type反馈类型可选值:'success', 'warning', 'error', 'notice', 'help', 'loading'Enum'success'
shape反馈外观可选值:'inline', 'addon', 'toast'Enum'inline'
title标题ReactNode-
children内容ReactNode-
defaultVisible默认是否显示Booleantrue
visible当前是否显示Boolean-
iconType显示的图标类型,会覆盖内部设置的IconTypeString-
closeable显示关闭按钮Booleanfalse
onClose关闭按钮的回调签名:Function() => voidFunction() => {}
afterClose关闭之后调用的函数签名:Function() => voidFunction() => {}
animation是否开启展开收起动画Booleantrue
<!— api-extra-start —>### Message.show#Message.show(props) 提供一个单例的调用方式,配置参数如下(继承 Overlay 的配置):
参数说明类型默认值
type反馈类型String'success'
title反馈标题ReactNode-
content反馈内容ReactNode-
duration显示持续时间,0表示一直存在,以毫秒为单位Number3000
align对齐方式,参考OverlayString'tc tc'
offset对齐之后的偏移位置Array0, 0
hasMask是否带有遮罩Booleanfalse
closeable显示关闭按钮Booleanfalse
afterClose关闭事件的回调函数Function-
overlayProps透传到弹层的属性对象Object-

示例:

  1. Message.show({
  2. type: 'error',
  3. title: '错误',
  4. content: '请联系相关人员反馈!',
  5. hasMask: true
  6. });

Message.hide

Message.hide() 提供关闭反馈弹层的快捷方法。

Message.

Message.show({type: type, title: 'xxx'}); 的便捷调用方法。

示例:

  1. Message.success('反馈内容');
  2. // 或者
  3. Message.success({
  4. title: '反馈内容',
  5. duration: 1000
  6. });

<!— api-extra-end —>

ARIA and KeyBoard

说明: 此组件需要结合其他组件使用,才会有提示。参考上文无障碍

代码示例

信息类型

通过设置type调整信息类型

Message 信息提示 - 图1

查看源码在线预览

  1. import { Message } from '@alifd/next';
  2. ReactDOM.render(
  3. <div className="message-type-demo">
  4. <Message title="Success" type="success">
  5. Content Content Content Content
  6. </Message>
  7. <Message title="Warning" type="warning">
  8. Content Content Content Content
  9. </Message>
  10. <Message title="Error" type="error">
  11. Content Content Content Content
  12. </Message>
  13. <Message title="Notice" type="notice">
  14. Content Content Content Content
  15. </Message>
  16. <Message title="Help" type="help">
  17. Content Content Content Content
  18. </Message>
  19. <Message title="Loading" type="loading">
  20. Content Content Content Content
  21. </Message>
  22. </div>, mountNode);
  1. .message-type-demo .next-message {
  2. margin-bottom: 10px;
  3. }

信息外观

通过设置shape调整信息外观

Message 信息提示 - 图2

查看源码在线预览

  1. import { Message, Radio } from '@alifd/next';
  2. const types = ['success', 'warning', 'error', 'notice', 'help', 'loading'];
  3. const list = [
  4. {
  5. value: 'inline',
  6. label: 'inline'
  7. }, {
  8. value: 'addon',
  9. label: 'addon'
  10. }, {
  11. value: 'toast',
  12. label: 'toast'
  13. }
  14. ];
  15. class Demo extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. shape: 'inline'
  20. };
  21. this.handleSelect = this.handleSelect.bind(this);
  22. }
  23. handleSelect(shape) {
  24. this.setState({ shape });
  25. }
  26. render() {
  27. const { shape } = this.state;
  28. return (
  29. <div className="message-shape-demo">
  30. <span className="demo-label">Select Shape:</span>
  31. <Radio.Group defaultValue="inline" dataSource={list} value={this.state.shape} onChange={this.handleSelect} />
  32. {types.map(type => (
  33. <Message key={type} title={type} type={type} shape={shape}>
  34. Content Content Content Content
  35. </Message>
  36. ))}
  37. </div>
  38. );
  39. }
  40. }
  41. ReactDOM.render(<Demo />, mountNode);
  1. .message-shape-demo .demo-label {
  2. display: inline-block;
  3. vertical-align: top;
  4. height: 22px;
  5. line-height: 22px;
  6. }
  7. .message-shape-demo .next-message-title {
  8. text-transform: capitalize;
  9. }
  10. .message-shape-demo .next-message {
  11. margin-top: 10px;
  12. }
  13. .message-shape-demo .next-message.next-message-toast {
  14. position: relative !important;
  15. }

信息尺寸

通过size设置尺寸

Message 信息提示 - 图3

查看源码在线预览

  1. import { Message, Radio } from '@alifd/next';
  2. const types = ['success', 'warning', 'error', 'notice', 'help', 'loading'];
  3. const list = [
  4. {
  5. value: 'medium',
  6. label: 'medium'
  7. }, {
  8. value: 'large',
  9. label: 'large'
  10. }
  11. ];
  12. class Demo extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. size: 'medium'
  17. };
  18. this.handleSelect = this.handleSelect.bind(this);
  19. }
  20. handleSelect(size) {
  21. this.setState({ size });
  22. }
  23. render() {
  24. const { size } = this.state;
  25. return (
  26. <div className="message-size-demo">
  27. <span className="demo-label">Select Size:</span>
  28. <Radio.Group defaultValue="medium" dataSource={list} value={this.state.size} onChange={this.handleSelect} />
  29. {types.map(type => (
  30. <Message key={type} title={type} type={type} size={size}>
  31. Content Content Content Content
  32. </Message>
  33. ))}
  34. </div>
  35. );
  36. }
  37. }
  38. ReactDOM.render(<Demo />, mountNode);
  1. .message-size-demo .demo-label {
  2. display: inline-block;
  3. vertical-align: top;
  4. height: 28px;
  5. line-height: 28px;
  6. }
  7. .message-size-demo .next-message-title {
  8. text-transform: capitalize;
  9. }
  10. .message-size-demo .next-message {
  11. margin-top: 10px;
  12. }

可关闭组件

通过增加closeable属性可以控制提示框是否可关闭。

Message 信息提示 - 图4

查看源码在线预览

  1. import { Message } from '@alifd/next';
  2. const onClose = () => console.log('onClose triggered!');
  3. const afterClose = () => console.log('afterClose triggered!');
  4. ReactDOM.render(
  5. <div>
  6. <Message title="title" closeable onClose={onClose} afterClose={afterClose}>
  7. Content Content Content Content
  8. </Message>
  9. </div>, mountNode);

受控显示隐藏

Message 信息提示 - 图5

查看源码在线预览

  1. import { Message, Button } from '@alifd/next';
  2. class App extends React.Component {
  3. state = {
  4. visible: true
  5. }
  6. handleChange = () => {
  7. this.setState({
  8. visible: !this.state.visible
  9. });
  10. }
  11. handleClose = () => {
  12. this.setState({
  13. visible: false
  14. });
  15. }
  16. render() {
  17. const { visible } = this.state;
  18. return (
  19. <div className="control-demo">
  20. <Button onClick={this.handleChange}>Toggle Visible</Button>
  21. <Message type="warning" visible={visible} title="Warning" closeable onClose={this.handleClose}>
  22. This item already has the label "travel". You can add a new label.
  23. </Message>
  24. </div>
  25. );
  26. }
  27. }
  28. ReactDOM.render(<App />, mountNode);
  1. .control-demo .next-btn.next-medium {
  2. margin-bottom: 10px;
  3. }

弹窗用法

可以通过Message.showMessage.hide方法来方便的显示或隐藏反馈弹窗。

Message 信息提示 - 图6

查看源码在线预览

  1. import { Message, Button } from '@alifd/next';
  2. const show = () => {
  3. Message.show({
  4. type: 'loading',
  5. content: 'Will be closed after 3 seconds or manually click on the close button',
  6. afterClose: () => console.log('Closed the toast')
  7. });
  8. };
  9. const hide = () => Message.hide();
  10. ReactDOM.render(
  11. <div className="message-toast-demo">
  12. <Button onClick={show}>Show</Button>
  13. <Button onClick={hide}>Hide</Button>
  14. </div>, mountNode);
  1. .message-toast-demo .next-btn.next-medium {
  2. margin-right: 10px;
  3. }

弹窗便捷方法

可以通过Message.success等静态方法来方便的显示指定类型的信息弹窗。

Message 信息提示 - 图7

查看源码在线预览

  1. import { Message, Button } from '@alifd/next';
  2. const showSuccess = () => Message.success('success');
  3. const showWarning = () => Message.warning('warning');
  4. const showError = () => Message.error('error');
  5. const showNotice = () => Message.notice('notice');
  6. const showHelp = () => Message.help('help');
  7. const showLoading = () => Message.loading('loading');
  8. ReactDOM.render(
  9. <div className="message-toast-quick-demo">
  10. <Button onClick={showSuccess}>success</Button>
  11. <Button onClick={showWarning}>warning</Button>
  12. <Button onClick={showError}>error</Button>
  13. <Button onClick={showNotice}>notice</Button>
  14. <Button onClick={showHelp}>help</Button>
  15. <Button onClick={showLoading}>loading</Button>
  16. </div>, mountNode);
  1. .message-toast-quick-demo .next-btn.next-medium {
  2. margin-right: 10px;
  3. margin-bottom: 10px;
  4. }

无障碍

通过Enter键点击button时,自动聚焦到Message上读取信息。请参考ARIA and KeyBoard

Message 信息提示 - 图8

查看源码在线预览

  1. import { Message, Button } from '@alifd/next';
  2. const showSuccess = () => Message.success('success');
  3. ReactDOM.render(
  4. <div className="message-toast-quick-demo">
  5. <Button onClick={showSuccess}>success</Button>
  6. </div>, mountNode);

相关区块

Message 信息提示 - 图9

暂无相关区块