Element-React Alert 警告

2020-10-17 14:04 更新

用于頁面中展示重要的提示信息。

基本用法

頁面中的非浮層元素,不會自動消失。

Alert 組件提供四種主題,由type屬性指定,默認值為info。

render() {
  return (
    <div>
      <Alert title="成功提示的文案" type="success" />
      <Alert title="消息提示的文案" type="info" />
      <Alert title="警告提示的文案" type="warning" />
      <Alert title="錯誤提示的文案" type="error" />
    </div>
  )
}

自定義關(guān)閉按鈕

自定義關(guān)閉按鈕為文字或其他符號。

在 Alert 組件中,你可以設(shè)置是否可關(guān)閉,關(guān)閉按鈕的文本以及關(guān)閉時的回調(diào)函數(shù)。closable屬性決定是否可關(guān)閉,接受boolean,默認為true。你可以設(shè)置closeText屬性來代替右側(cè)的關(guān)閉圖標,注意:closeText必須為文本。設(shè)置onClose事件來設(shè)置關(guān)閉時的回調(diào)。

render() {
  return (
    <div>
      <Alert title="不可關(guān)閉的 alert" type="success" closable={false} />
      <Alert title="自定義 close-text" type="info" closeText="知道了" />
      <Alert title="設(shè)置了回調(diào)的 alert" type="warning" onClose={() => alert('Hello World!')}/>
    </div>
  )
}

帶有 icon

表示某種狀態(tài)時提升可讀性。

通過設(shè)置showIcon屬性來顯示 Alert 的 icon,這能更有效地向用戶展示你的顯示意圖。

render() {
  return (
    <div>
      <Alert title="成功提示的文案" type="success" showIcon={true} />
      <Alert title="消息提示的文案" type="info" showIcon={true} />
      <Alert title="警告提示的文案" type="warning" showIcon={true} />
      <Alert title="錯誤提示的文案" type="error" showIcon={true} />
    </div>
  )
}

帶有輔助性文字介紹

包含標題和內(nèi)容,解釋更詳細的警告。

除了必填的title屬性外,你可以設(shè)置description屬性來幫助你更好地介紹,我們稱之為輔助性文字。輔助性文字只能存放單行文本,會自動換行顯示。

render() {
  return (
    <Alert
      type="success"
      title="帶輔助性文字介紹"
      description="這是一句繞口令:黑灰化肥會揮發(fā)發(fā)灰黑化肥揮發(fā);灰黑化肥會揮發(fā)發(fā)黑灰化肥發(fā)揮。 黑灰化肥會揮發(fā)發(fā)灰黑化肥黑灰揮發(fā)化為灰……" />
  )
}

帶有 icon 和輔助性文字介紹

最后,這是一個同時具有 icon 和輔助性文字的樣例。

render() {
  return (
    <div>
      <Alert title="成功提示的文案" type="success" description="文字說明文字說明文字說明文字說明文字說明文字說明"  showIcon={true} />
      <Alert title="消息提示的文案" type="info" description="文字說明文字說明文字說明文字說明文字說明文字說明" showIcon={true} />
      <Alert title="警告提示的文案" type="warning" description="文字說明文字說明文字說明文字說明文字說明文字說明" showIcon={true} />
      <Alert title="錯誤提示的文案" type="error" description="文字說明文字說明文字說明文字說明文字說明文字說明" showIcon={true} />
    </div>
  )
}

Attributes

參數(shù) 說明 類型 可選值 默認值
title 標題,必選參數(shù) string
type 主題 string success/warning/info/error info
description 輔助性文字 string
closable 是否可關(guān)閉 boolean true
closeText 關(guān)閉按鈕自定義文本 string
showIcon 是否顯示圖標 boolean false

Events

事件名稱 說明 回調(diào)參數(shù)
onClose 關(guān)閉alert時觸發(fā)的事件
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號