[refactor] Actionsheet: 升级到自定义组件 (#175)

* fix: temp commit

* fix: temp commit

* fix: add custom actionsheet
This commit is contained in:
WyTiny 2018-04-02 22:24:01 +08:00 committed by Yao
parent 6484101db4
commit 9480e01b9b
11 changed files with 213 additions and 176 deletions

View File

@ -1,27 +1,22 @@
const { Actionsheet, extend } = require('../../dist/index');
Page(extend({}, Actionsheet, {
Page({
data: {
baseActionsheet: {
show: false,
cancelText: '关闭 Action',
closeOnClickOverlay: true,
componentId: 'baseActionsheet',
actions: [{
name: '选项1',
subname: '选项描述语1',
className: 'action-class',
loading: false
}, {
name: '选项2',
subname: '选项描述语2',
className: 'action-class',
loading: false
}, {
name: '去分享',
openType: 'share'
}]
}
show: false,
cancelWithMask: true,
actions: [{
name: '选项1',
subname: '选项描述语1',
className: 'action-class',
loading: false
}, {
name: '选项2',
subname: '选项描述语2',
className: 'action-class',
loading: false
}, {
name: '去分享',
openType: 'share'
}],
cancelText: '关闭 Action'
},
onShareAppMessage() {
@ -31,36 +26,32 @@ Page(extend({}, Actionsheet, {
};
},
toggleActionsheet() {
openActionsheet() {
this.setData({
'baseActionsheet.show': true
'show': true
});
},
handleZanActionsheetCancel({ componentId }) {
closeActionSheet() {
this.setData({
[`${componentId}.show`]: false
'show': false
});
},
handleZanActionsheetClick({ componentId, index }) {
console.log(`item index ${index} clicked`);
clickAction({ detail }) {
// 如果是分享按钮被点击, 不处理关闭
const { index } = detail;
if (index === 2) {
return;
}
this.setData({
[`${componentId}.actions[${index}].loading`]: true
[`actions[${index}].loading`]: true
});
setTimeout(() => {
this.setData({
[`${componentId}.show`]: false,
[`${componentId}.actions[${index}].loading`]: false
[`show`]: false,
[`actions[${index}].loading`]: false
});
}, 1500);
}
}));
});

View File

@ -1,3 +1,7 @@
{
"navigationBarTitleText": "Actionsheet 行动按钮"
"navigationBarTitleText": "Actionsheet 行动按钮",
"usingComponents": {
"zan-actionsheet": "../../dist/actionsheet/index",
"zan-btn": "../../dist/btn/index"
}
}

View File

@ -1,15 +1,17 @@
<import src="/dist/actionsheet/index.wxml" />
<view class="container">
<view class="doc-title zan-hairline--bottom zan-hairline--bottom">ACTIONSHEET</view>
<view class="zan-btns" style="margin-top: 30vh;">
<button class="zan-btn" bindtap="toggleActionsheet">
<button class="zan-btn" bindtap="openActionsheet">
Actionsheet
</button>
<zan-actionsheet
show="{{ show }}"
actions="{{ actions }}"
cancel-text="{{ cancelText }}"
cancel-with-mask="{{ cancelWithMask }}"
bind:cancel="closeActionSheet"
bind:actionclick="clickAction"
mask-class="tiny"
/>
</view>
<template is="zan-actionsheet" data="{{ ...baseActionsheet }}"></template>
</view>

View File

@ -0,0 +1,3 @@
.tiny {
background: rgba(30, 30, 40, 0.7) !important;
}

View File

@ -1,65 +1,80 @@
## Actionsheet 行动按钮
### 使用指南
在 app.wxss 中引入组件库所有样式
```css
@import "path/to/zanui-weapp/dist/index.wxss";
```
在需要使用的页面里引入组件库模板和脚本
```html
<import src="/dist/actionsheet/index.wxml" />
<!-- 直接使用 zan-actionsheet 模板,并且直接传入参数配置 -->
<template is="zan-actionsheet" data="{{ ...actionsheet }}"></template>
```
```js
const { Actionsheet, extend } = require('path/to/zanui-weapp/dist/index');
// 在 Page 中混入 Actionsheet 里面声明的方法
Page(extend({}, Actionsheet, {
data: {
actionsheet: {
show: false,
actions: []
}
在 index.json 中引入组件
```json
{
"usingComponents": {
"zan-actionsheet": "path/to/zanui-weapp/dist/actionsheet/index"
}
// ...
}));
}
```
### 代码演示
#### 基础功能
在 js 中设置传入的 show 为 true即可展示出 actionsheet。actionsheet 会根据传入的 actions 展示按钮。
```js
this.setData({
'actionsheet.show': true
})
### 使用指南
```html
<button bindtap="openActionSheet">Open ActionSheet</button>
<view class="actionsheet-container">
<!-- 监听自定义事件 cancel 和 actionclick绑定回调函数 -->
<zan-actionsheet
show="{{ show }}"
actions="{{ actions }}"
cancel-text="{{ cancelText }}"
cancel-with-mask="{{ cancelWithMask }}"
bind:cancel="closeActionSheet"
bind:actionclick="handleActionClick"
>
</zan-actionsheet>
</view>
```
当行动按钮被点击或者弹层被关掉时,都可以在可以在页面中注册 `handleZanActionsheetClick``handleZanActionsheetCancel` 方法来监听。
```js
// 在 Page 中混入 Actionsheet 里面声明的方法
Page({
// 当行动按钮被关闭时,触发该函数
// componentId 即为在模板中传入的 componentId
// 用于在一个页面上使用多个 actionsheet 时,进行区分
handleZanActionsheetCancel({ componentId }) {
data: {
show: false,
cancelWithMask: true,
actions: [{
name: '选项1',
subname: '选项描述语1',
loading: false
}, {
name: '选项2',
subname: '选项描述语2',
loading: false
}, {
name: '去分享',
openType: 'share'
}],
cancelText: '关闭 Action'
},
// 当行动按钮中有一个被点击时触发
// index 代表被点击按钮在传入参数 actions 中的位置
handleZanActionsheetClick({ componentId, index }) {
openActionSheet() {
this.setData({
'show': true
});
},
closeActionSheet() {
this.setData({
'show': false
});
},
handleActionClick({ detail }) {
// 获取被点击的按钮 index
const { index } = detail;
}
});
```
#### `Actionsheet` 支持的具体参数如下
#### `Actionsheet` 支持的具体参数如下( 传入时使用分隔线写法
| 参数 | 说明 | 类型 | 默认值 | 必须 |
|-----------|-----------|-----------|-------------|-------------|
| show | 用来表示是否展示行动按钮 | Boolean | false | |
| actions | 指定弹层里的按钮 | Array | [] | |
| cancelText | 行动按钮底部取消按钮的文案,不传则不显示取消按钮 | String | | |
| closeOnClickOverlay | 是否在点击背景时,关闭行动按钮 | Boolean | false | |
| componentId | 用于区分行动按钮之间的唯一名称 | String | | |
| actions | 行动按钮的按钮显示配置 | Array | [] | |
| cancelWithMask | 是否在点击背景时,关闭行动按钮 | Boolean | false | |
| mask-class | 用于控制蒙层样式的外部类 | String | | |
| container-class | 用于控制容器样式的外部类 | String | | |
actions 的具体数据结构
```js
@ -69,8 +84,6 @@ actions 的具体数据结构
name: '选项1',
// 按钮描述文案,不传就不显示
subname: '选项描述语1',
// 按钮特殊类,可以通过传入这个,为按钮增加特殊样式
className: 'action-class',
// 按钮是否显示为 loading
loading: false,
// 按钮的微信开放能力

View File

@ -1,41 +1,36 @@
const { extractComponentId } = require('../common/helper');
module.exports = {
_handleZanActionsheetMaskClick({ currentTarget = {} }) {
const dataset = currentTarget.dataset || {};
const { componentId, closeOnClickOverlay } = dataset;
// 判断是否在点击背景时需要关闭弹层
if (!closeOnClickOverlay) {
return;
Component({
externalClasses: ['mask-class', 'container-class'],
properties: {
actions: {
type: Array,
value: []
},
show: {
type: Boolean,
value: false
},
cancelWithMask: {
type: Boolean,
value: true
},
cancelText: {
type: String,
value: ''
}
resolveCancelClick.call(this, { componentId });
},
_handleZanActionsheetCancelBtnClick(e) {
const componentId = extractComponentId(e);
resolveCancelClick.call(this, { componentId });
},
_handleZanActionsheetBtnClick({ currentTarget = {} }) {
const dataset = currentTarget.dataset || {};
const { componentId, index } = dataset;
if (this.handleZanActionsheetClick) {
this.handleZanActionsheetClick({ componentId, index });
} else {
console.warn('页面缺少 handleZanActionsheetClick 回调函数');
methods: {
onMaskClick() {
if (this.data.cancelWithMask) {
this.cancelClick();
}
},
cancelClick() {
this.triggerEvent('cancel');
},
handleBtnClick({ currentTarget = {} }) {
const dataset = currentTarget.dataset || {};
const { index } = dataset;
this.triggerEvent('actionclick', { index });
}
}
};
function resolveCancelClick({ componentId }) {
console.info('[zan:actionsheet:cancel]');
if (this.handleZanActionsheetCancel) {
this.handleZanActionsheetCancel({ componentId });
} else {
console.warn('页面缺少 handleZanActionsheetCancel 回调函数');
}
}
});

View File

@ -0,0 +1,6 @@
{
"component": true,
"usingComponents": {
"zan-btn": "../btn/index"
}
}

View File

@ -30,17 +30,31 @@
visibility: hidden;
}
.zan-actionsheet__btn.zan-btn {
height: 50px;
line-height: 50px;
margin-bottom: 0;
&::after {
border-width: 0;
border-bottom-width: 1px;
}
.zan-actionsheet__btn {
margin-bottom: 0 !important;
}
.zan-actionsheet__footer .zan-actionsheet__btn {
background: #fff;
}
.zan-actionsheet__btn-content {
display: flex;
flex-direction: row;
justify-content: center;
}
.zan-actionsheet__subname {
color: $gray-dark;
}
.zan-actionsheet__subname,
.zan-actionsheet__name {
height: 45px;
line-height: 45px;
}
.zan-actionsheet__btn.zan-btn:last-child {
&::after {
border-bottom-width: 0;
@ -50,7 +64,6 @@
.zan-actionsheet__subname {
margin-left: 2px;
font-size: 12px;
color: $gray-darker;
}
.zan-actionsheet__footer {
@ -58,7 +71,7 @@
}
/* btn-loading 状态 */
.zan-actionsheet__btn.zan-btn--loading .zan-actionsheet__subname {
.zan-actionsheet__btn--loading .zan-actionsheet__subname {
color: transparent;
}

View File

@ -1,40 +1,39 @@
<template name="zan-actionsheet">
<view class="zan-actionsheet {{ show ? 'zan-actionsheet--show' : '' }}">
<view
class="zan-actionsheet__mask"
catchtap="_handleZanActionsheetMaskClick"
data-close-on-click-overlay="{{ closeOnClickOverlay }}"
data-component-id="{{ componentId }}"></view>
<view class="zan-actionsheet__container">
<!-- 实际按钮显示 -->
<button
wx:for="{{ actions }}"
wx:for-index="index"
wx:for-item="item"
wx:key="{{ index }}-{{ item.name }}"
catchtap="_handleZanActionsheetBtnClick"
data-component-id="{{ componentId }}"
data-index="{{ index }}"
open-type="{{ item.openType }}"
class="zan-btn zan-actionsheet__btn {{ item.loading ? 'zan-btn--loading' : '' }} {{ item.className }}"
>
<text>{{ item.name }}</text>
<text
<view class="zan-actionsheet {{ show ? 'zan-actionsheet--show' : '' }}">
<view
class="mask-class zan-actionsheet__mask"
bindtap="onMaskClick"
></view>
<view class="container-class zan-actionsheet__container">
<!-- 选项按钮 -->
<zan-btn
wx:for="{{ actions }}"
wx:key="{{ index }}-{{ item.name }}"
bind:btnclick="handleBtnClick"
data-index="{{ index }}"
open-type="{{ item.openType }}"
custom-class="zan-actionsheet__btn"
loading="{{ item.loading }}"
>
<!-- 自定义组件控制 slot 样式有问题,故在 slot 容器上传入 loading 信息 -->
<view class="zan-actionsheet__btn-content {{ item.loading ? 'zan-actionsheet__btn--loading' : '' }}">
<view class="zan-actionsheet__name">{{ item.name }}</view>
<view
wx:if="{{ item.subname }}"
class="zan-actionsheet__subname">{{ item.subname }}</text>
</button>
<!-- 关闭按钮 -->
<view
wx:if="{{ cancelText }}"
class="zan-actionsheet__footer"
>
<button
class="zan-btn zan-actionsheet__btn"
catchtap="_handleZanActionsheetCancelBtnClick"
data-component-id="{{ componentId }}"
>{{ cancelText }}</button>
class="zan-actionsheet__subname">
{{ item.subname }}
</view>
</view>
</zan-btn>
<!-- 关闭按钮 -->
<view
wx:if="{{ cancelText }}"
class="zan-actionsheet__footer"
>
<zan-btn
custom-class="zan-actionsheet__btn"
catchtap="cancelClick"
>{{ cancelText }}</zan-btn>
</view>
</view>
</template>
</view>

View File

@ -22,5 +22,14 @@ Component({
type: Boolean,
value: false,
},
openType: {
type: String,
value: ''
}
},
methods: {
handleTap() {
this.triggerEvent('btnclick');
}
}
});

View File

@ -1,7 +1,9 @@
<button
class="custom-class zan-btn {{size ? 'zan-btn--'+size : ''}} {{size === 'mini' ? 'zan-btn--plain' : ''}} {{plain ? 'zan-btn--plain' : ''}} {{type ? 'zan-btn--'+type : ''}} {{loading ? 'zan-btn--loading' : ''}} {{disabled ? 'zan-btn--disabled' : ''}}"
disabled="{{disabled}}"
disabled="{{ disabled }}"
open-type="{{ openType }}"
bindtap="handleTap"
>
<slot></slot>
</button>