模擬系統(tǒng)的消息提示框而實現的一套模態(tài)對話框組件,用于消息提示、成功提示、錯誤提示、詢問信息。
當用戶進行操作時會被觸發(fā),該對話框中斷用戶操作,直到用戶確認知曉后才可關閉。
調用alert
方法即可打開消息提示,它模擬了系統(tǒng)的 alert
,無法通過按下 ESC 或點擊框外關閉。此例中接收了兩個參數,message
和title
。值得一提的是,窗口被關閉后,它默認會返回一個Promise
對象便于進行后續(xù)操作的處理。
render() {
return <Button type="text" onClick={this.onClick.bind(this)}>點擊打開 Message Box</Button>
}
onClick() {
MessageBox.alert('這是一段內容', '標題名稱');
}
提示用戶確認其已經觸發(fā)的動作,并詢問是否進行此操作時會用到此對話框。
調用confirm
方法即可打開消息提示,它模擬了系統(tǒng)的 confirm
。Message Box 組件也擁有極高的定制性,我們可以傳入options
作為第三個參數,它是一個字面量對象。type
字段表明消息類型,可以為success
,error
,info
和warning
,無效的設置將會被忽略。注意,第二個參數title
必須定義為String
類型,如果是Object
,會被理解為options
。在這里我們用了 Promise 來處理后續(xù)響應。
render() {
return <Button type="text" onClick={this.onClick.bind(this)}>點擊打開 Message Box</Button>
}
onClick() {
MessageBox.confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', {
type: 'warning'
}).then(() => {
Message({
type: 'success',
message: '刪除成功!'
});
}).catch(() => {
Message({
type: 'info',
message: '已取消刪除'
});
});
}
當用戶進行操作時會被觸發(fā),中斷用戶操作,提示用戶進行輸入的對話框。
調用prompt
方法即可打開消息提示,它模擬了系統(tǒng)的 prompt
??梢杂?code>inputPattern字段自己規(guī)定匹配模式,或者用inputValidator
規(guī)定校驗函數,可以返回Boolean
或String
,Boolean
為false
或字符串時均表示校驗未通過,String
相當于定義了inputErrorMessage
字段。此外,可以用inputPlaceholder
字段來定義輸入框的占位符。
render() {
return <Button type="text" onClick={this.onClick.bind(this)}>點擊打開 Message Box</Button>
}
onClick() {
MessageBox.prompt('請輸入郵箱', '提示', {
inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
inputErrorMessage: '郵箱格式不正確'
}).then(({ value }) => {
Message({
type: 'success',
message: '你的郵箱是: ' + value
});
}).catch(() => {
Message({
type: 'info',
message: '取消輸入'
});
});
}
可自定義配置不同內容。
以上三個方法都是對msgbox
方法的再包裝。本例直接調用msgbox
方法,使用了showCancelButton
字段,用于顯示取消按鈕。另外可使用cancelButtonClass
為其添加自定義樣式,使用cancelButtonText
來自定義按鈕文本。Confirm 按鈕也具有相同的字段,在文末的字段說明中有完整的字段列表。
render() {
return <Button type="text" onClick={this.onClick.bind(this)}>點擊打開 Message Box</Button>
}
onClick() {
MessageBox.msgbox({
title: '消息',
message: '這是一段內容, 這是一段內容, 這是一段內容, 這是一段內容, 這是一段內容, 這是一段內容, 這是一段內容',
showCancelButton: true
}).then(action => {
Message({
type: 'info',
message: 'action: ' + action
});
})
}
單獨引入 MessageBox
:
import { MessageBox } from 'element-react';
對應于上述四個全局方法的調用方法依次為:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt。
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
title | MessageBox 標題 | string | — | — |
customClass | 對話框外層容器的類名 | string | — | - |
message | MessageBox 消息正文內容 | string/ReactElement | — | — |
type | 消息類型,用于顯示圖標 | string | success/info/ warning/error | — |
lockScroll | 是否在 MessageBox 出現時將 body 滾動鎖定 | boolean | — | true |
showClose | 是否顯示關閉按鈕 | boolean | — | true |
showCancelButton | 是否顯示取消按鈕 | boolean | — | false(以 confirm 和 prompt 方式調用時為 true) |
showConfirmButton | 是否顯示確定按鈕 | boolean | — | true |
cancelButtonText | 取消按鈕的文本內容 | string | — | 取消 |
confirmButtonText | 確定按鈕的文本內容 | string | — | 確定 |
cancelButtonClass | 取消按鈕的自定義類名 | string | — | — |
confirmButtonClass | 確定按鈕的自定義類名 | string | — | — |
showInput | 是否顯示輸入框 | boolean | — | false(以 prompt 方式調用時為 true) |
inputPlaceholder | 輸入框的占位符 | string | — | — |
inputType | 輸入框的類型 | string | — | text |
inputValue | 輸入框的初始文本 | string | — | — |
inputPattern | 輸入框的校驗表達式 | regexp | — | — |
inputValidator | 輸入框的校驗函數??梢苑祷夭紶栔祷蜃址?,若返回一個字符串, 則返回結果會被賦值給 inputErrorMessage | function | — | — |
inputErrorMessage | 校驗未通過時的提示文本 | string | — | 輸入的數據不合法! |
更多建議: