簡介:css
效果圖:react
編寫組件的基本思路:git
1、 組件拆分
該組件能夠分爲三個部分.github
2、肯定使用方法
modal 參數:web
參數 | 說明 | 類型 |
---|---|---|
title | 元素的標題 | String |
content | 元素的內容 | String |
cancelText | 取消按鈕的文本 | String |
closeOnClickOverlay | 點擊浮層的時候時候自動關閉 | Boolean |
confirmText | 確認按鈕的文本 | String |
isOpened | 是否顯示模態框 | Boolean |
modal 事件:小程序
事件名稱 | 說明
onClose | 觸發關閉時的事件 |
onCancel | 點擊取消按鈕觸發的樣式 |
onConfirm | 點擊確認按鈕觸發的事件 |
數據結構sass
this.state = { modal:{ isOpened:false, title:'標題', content:'內容', cancelText:'取消', confirmText:'確認', closeOnClickOverlay:false } }
3、分步驟實施
實現 UI 功能.數據結構
實現modal 的結構app
<View className='mp-modal mp-modal--active'> <View className="mp-modal__overlay"> </View> <View className="mp-modal__container"> <View className="mp-modal__title">標題</View> <View className="mp-modal__content">內容</View> <View className="mp-modal__footer"> <View className="mp-modal__action"> <Button>取消</Button> <Button>確認</Button> </View> </View> </View> </View>
實現modal 的 css 樣式.(說重要的幾個點)webapp
通用的遮罩層.
{ top: 0; left: 0; width: 100%; height: 100%; position: absolute; background-color: rgba($color: #000, $alpha: 0.3); }
{ position: $pos; top: 50%; left: 50%; transform: translate(-50%, -50%); }
react 部分功能實現.
核心點有2個:
* 一、在子組件生命週期 componentWillReceiveProps 中監聽父組件狀態的變化決定是否渲染子組件.
componentWillReceiveProps(nextProps){ const {_isOpened} = this.state; if(_isOpened != nextProps.isOpened){ this.setState({ _isOpened:nextProps.isOpened }); } }
二、子組件接收父組件傳遞過來的屬性和事件.
``` <Modal title={this.modal.title} content={this.modal.content} isOpened={this.modal.isOpened} cancelText={this.modal.cancelText} confirmText={this.modal.confirmText} closeOnClickOverlay={this.modal.closeOnClickOverlay} onClose={this.onClose} onConfirm={this.onConfirm} onCancel={this.onCancel} /> ```
4、技術總結:
5、參考文獻: