简介: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、参考文献: