diff --git a/example/pages/toptips/index.js b/example/pages/toptips/index.js
index 62926a82..79f6d0e0 100644
--- a/example/pages/toptips/index.js
+++ b/example/pages/toptips/index.js
@@ -1,9 +1,44 @@
-var Zan = require('../../dist/index');
+const Toptips = require('../../dist/toptips/index');
-Page(Object.assign({}, Zan.TopTips, {
- data: {},
+Page({
+ data: {
+ content: '测试toptips',
+ duration: 2000,
+ $zanui: {
+ toptips: {
+ show: false
+ }
+ }
+ },
showTopTips() {
- this.showZanTopTips('toptips的内容');
+ this.setData({
+ $zanui: {
+ toptips: {
+ show: true
+ }
+ }
+ });
+
+ setTimeout(() => {
+ this.setData({
+ $zanui: {
+ toptips: {
+ show: false
+ }
+ }
+ });
+ },this.data.duration);
+ },
+
+ showTopTips2() {
+ Toptips('测试内容');
+ },
+
+ showTopTips3() {
+ Toptips({
+ duration: 1000,
+ content: '测试时间1秒'
+ })
}
-}));
+});
diff --git a/example/pages/toptips/index.json b/example/pages/toptips/index.json
index 3f45367d..a2099cd9 100644
--- a/example/pages/toptips/index.json
+++ b/example/pages/toptips/index.json
@@ -1,3 +1,6 @@
{
- "navigationBarTitleText": "Toptips 顶部提示"
+ "navigationBarTitleText": "Toptips 顶部提示",
+ "usingComponents": {
+ "zan-toptips": "../../dist/toptips/index"
+ }
}
diff --git a/example/pages/toptips/index.wxml b/example/pages/toptips/index.wxml
index bc84428d..8e40027f 100644
--- a/example/pages/toptips/index.wxml
+++ b/example/pages/toptips/index.wxml
@@ -6,9 +6,25 @@
+
+
+
+
+
+
+
+
-
+
diff --git a/packages/noticebar/index.js b/packages/noticebar/index.js
index 4b0a8ca2..7c0c67f2 100644
--- a/packages/noticebar/index.js
+++ b/packages/noticebar/index.js
@@ -66,6 +66,11 @@ Component({
}
},
+ detached() {
+ const { timer } = this.data;
+ timer && clearTimeout(timer);
+ },
+
ready() {
this._init();
},
@@ -105,7 +110,6 @@ Component({
if (scrollable && wrapWidth < width) {
const elapse = width / speed * 1000;
- console.log(`delay: ${delay}`)
const animation = wx.createAnimation({
duration: elapse,
timeingFunction: 'linear',
@@ -162,4 +166,4 @@ Component({
});
}
}
-})
\ No newline at end of file
+})
diff --git a/packages/tab/index.js b/packages/tab/index.js
index 37c9b6ab..23568383 100644
--- a/packages/tab/index.js
+++ b/packages/tab/index.js
@@ -20,31 +20,16 @@ Component({
},
selectedId: {
type: [String, Number],
- value: '',
- observer(newVal) {
- this.setData({
- currentTab: newVal
- });
- }
+ value: ''
}
},
- data: {
- currentTab: ''
- },
-
- attached() {
- this.setData({
- currentTab: this.data.selectedId
- });
- },
-
methods: {
_handleZanTabChange(e) {
const selectedId = e.currentTarget.dataset.itemId;
this.setData({
- currentTab: selectedId
+ selectedId
});
console.info('[zan:tab:change] selectedId:', selectedId);
diff --git a/packages/tab/index.wxml b/packages/tab/index.wxml
index 60474ed1..1a52111e 100644
--- a/packages/tab/index.wxml
+++ b/packages/tab/index.wxml
@@ -10,7 +10,7 @@
>
@@ -21,7 +21,7 @@
>
@@ -33,7 +33,7 @@
diff --git a/packages/toptips/README.md b/packages/toptips/README.md
index 73fe777b..a0f04d69 100644
--- a/packages/toptips/README.md
+++ b/packages/toptips/README.md
@@ -1,29 +1,129 @@
## TopTips 顶部提示
### 使用指南
-在 app.wxss 中引入组件库所有样式
-```css
-@import "path/to/zanui-weapp/dist/index.wxss";
+在 index.json 中引入组件
+```json
+{
+ "usingComponents": {
+ "zan-toptips": "path/to/zanui-weapp/dist/toptips/index"
+ }
+}
```
-在需要使用的页面里引入组件库模板和脚本
-```html
-
+在 index.js 中声明组件数据
-
-
-```
+**toptips提供了声明式和命令式2种调用方式,但是由于小程序本身限制,会有一定使用的要求**
```js
-const { TopTips, extend } = require('path/to/zanui-weapp/dist/index');
+// 使用声明式调用的方式, 必须在Page中声明 $zanui对象, 结构如下
+// 同时在其他触发toptips显示的函数中,需要手动改变对应的数值
+Page({
+ data: {
+ duration: 1000,
+ content: 'xxx',
+ $zanui: {
+ toptips: {
+ show: false
+ }
+ }
+ }
+})
+
+// 使用命令式调用的方式,必须在 wxml 模板中声明组件id,
+// 默认我们使用了 zan-toptips, 如果使用者要更换,可以手动传入
+
+const Toptips = require('path/to/zanui-weapp/dist/toptips/index');
+Page({
+ customCallback() {
+ Toptips('只穿文案展示');
+ }
+})
-// 在 Page 中混入 TopTips 里面声明的方法
-Page(extend({}, TopTips, {
- // ...
-}));
```
+
### 代码演示
-在 js 中直接调用 this.showZanTopTips 即可
+
+### 声明式调用
+使用声明式调用
```js
-this.showZanTopTips('toptips的内容');
+Page({
+ data: {
+ duration: 1000,
+ content: 'xxx',
+ $zanui: {
+ toptips: {
+ show: false
+ }
+ }
+ },
+
+ customCallback() {
+ this.setData({
+ $zanui: {
+ toptips: {
+ show: true
+ }
+ }
+ });
+
+ setTimeout(() => {
+ this.setData({
+ $zanui: {
+ toptips: {
+ show: false
+ }
+ }
+ })
+ }, this.data.duration);
+ }
+})
```
+```html
+
+```
+
+### 命令式调用
+```js
+Page({
+ customCallback() {
+ Toptips('我只改文案')
+ }
+})
+```
+```html
+
+```
+
+### 修改组件id
+```js
+Page({
+ customCallback() {
+ Toptips({
+ content: '传入其他参数',
+ selector: '#other-id',
+ duration: 5000
+ })
+ }
+})
+```
+```html
+
+```
+
+| 参数 | 说明 | 类型 | 默认值 | 可选值 |
+|-----------|-----------|-----------|-------------|-------------|
+| content | 展示文案 | String | - | |
+| duration | 弹层持续时间 | Number | 3000 | |
+| isShow | 弹层是否展示 | Boolean | false | |
+| color | 字体颜色 | String | `#fff` | |
+| backgroundColor | 提示背景色 | String | `#e64340` |
\ No newline at end of file
diff --git a/packages/toptips/index.js b/packages/toptips/index.js
index c1d1cea5..b34ca615 100644
--- a/packages/toptips/index.js
+++ b/packages/toptips/index.js
@@ -1,39 +1,74 @@
-module.exports = {
- showZanTopTips(content = '', options = {}) {
- let zanTopTips = this.data.zanTopTips || {};
- // 如果已经有一个计时器在了,就清理掉先
- if (zanTopTips.timer) {
- clearTimeout(zanTopTips.timer);
- zanTopTips.timer = 0;
+const FONT_COLOR = '#fff';
+const BG_COLOR = '#e64340';
+
+Component({
+ properties: {
+ content: String,
+ color: {
+ type: String,
+ value: FONT_COLOR
+ },
+ backgroundColor: {
+ type: String,
+ value: BG_COLOR
+ },
+ isShow: {
+ type: Boolean,
+ value: false
+ },
+ duration: {
+ type: Number,
+ value: 3000
}
+ },
- if (typeof options === 'number') {
- options = {
- duration: options
- };
- }
+ methods: {
+ show() {
+ const duration = this.data.duration;
- // options参数默认参数扩展
- options = Object.assign({
- duration: 3000
- }, options);
-
- // 设置定时器,定时关闭topTips
- let timer = setTimeout(() => {
+ this._timer && clearTimeout(this._timer);
this.setData({
- 'zanTopTips.show': false,
- 'zanTopTips.timer': 0
+ isShow: true
});
- }, options.duration);
- // 展示出topTips
- this.setData({
- zanTopTips: {
- show: true,
- content,
- options,
- timer
+ if (duration > 0 && duration !== Infinity) {
+ this._timer = setTimeout(() => {
+ this.hide();
+ }, duration);
}
- });
+ },
+
+ hide() {
+ this._timer = clearTimeout(this._timer);
+
+ this.setData({
+ isShow: false
+ });
+ }
}
-};
+});
+
+function Toptips(options = {}) {
+ const pages = getCurrentPages();
+ const ctx = pages[pages.length - 1];
+ const defaultOptions = {
+ selector: '#zan-toptips',
+ duration: 3000
+ };
+
+ options = Object.assign(defaultOptions,parseParam(options));
+
+ const $toptips = ctx.selectComponent(options.selector);
+ delete options.selector;
+
+ $toptips.setData({
+ ...options
+ });
+ $toptips && $toptips.show();
+}
+
+function parseParam(params) {
+ return typeof params === 'object' ? params : { content: params };
+}
+
+module.exports = Toptips;
\ No newline at end of file
diff --git a/packages/toptips/index.json b/packages/toptips/index.json
new file mode 100644
index 00000000..32640e0d
--- /dev/null
+++ b/packages/toptips/index.json
@@ -0,0 +1,3 @@
+{
+ "component": true
+}
\ No newline at end of file
diff --git a/packages/toptips/index.wxml b/packages/toptips/index.wxml
index fe089460..e293b276 100644
--- a/packages/toptips/index.wxml
+++ b/packages/toptips/index.wxml
@@ -1,3 +1 @@
-
- {{ zanTopTips.content }}
-
+{{ content }}