Skip to content

Commit

Permalink
Merge pull request #18 from lucaszhu2zgf/component
Browse files Browse the repository at this point in the history
新增小程序组件方式的调用
  • Loading branch information
lucaszhu2zgf authored Jun 13, 2020
2 parents 97ed579 + ec24e1f commit c8c8c02
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ src
webpack.config.js
dist/progress.js
.vscode
.babelrc
.babelrc
.github
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,36 @@
$ npm install mp-progress --save
```

## 用法
## 组件方式的调用(简单易用)
在对应页面引入组件,初始化data并在wxml中使用组件
```
{
"navigationBarTitleText": "首页",
"usingComponents": {
"mpProgress": "mp-progress/dist/component/mp-progress"
}
}
...
// 参数使用方式相同,canvasSize默认是400*400
data = {
config: {
canvasSize: {
width: 400,
height: 400
},
percent: 100,
barStyle: [{width: 20, fillStyle: '#f0f0f0'}, {width: 20, animate: true, fillStyle: [{position: 0, color: '#56B37F'}, {position: 1, color: '#c0e674'}]}],
needDot: true,
dotStyle: [{r: 20, fillStyle: '#ffffff', shadow: 'rgba(0,0,0,.15)'}, {r: 10, fillStyle: '#56B37F'}]
},
percentage: 0
}
...
<mpProgress config="{{config}}" percentage="{{percentage}}"></mpProgress>
```
该方式遵从双向绑定数据的原则,当页面改变percentage,mp-progress会自动更新视图

## API方式调用(适合需要在mp-progress上层构建新组件的需求)

传入的单位是`rpx`,即如果canvas宽度为400rpx,则传入400,后续会自动计算真实尺寸
```
Expand Down
43 changes: 43 additions & 0 deletions dist/component/mp-progress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import MpProgress from "../progress.min.js";

Component({
options: {
addGlobalClass: true,
},
properties: {
config: {
type: Object,
value: {}
},
percentage: {
type: Number,
value: 0
}
},
data: {
customOptions: {
canvasSize: {
width: 400,
height: 400
},
percent: 100
},
canvasId: `mp_progress_${new Date().getTime()}`
},
attached() {
const customOptions = Object.assign({}, this.data.customOptions, this.data.config);
this.setData({customOptions});
},
ready() {
this._mpprogress = new MpProgress(Object.assign({}, this.data.customOptions, {canvasId: this.data.canvasId, target: this}));
this._mpprogress.draw(this.data.percentage || 0);
},
observers: {
'percentage': function (percentage) {
if (this._mpprogress) {
// 第一次进来的时候还没有初始化完成
this._mpprogress.draw(percentage);
}
},
}
});
4 changes: 4 additions & 0 deletions dist/component/mp-progress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}
1 change: 1 addition & 0 deletions dist/component/mp-progress.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<canvas class="mp-progress" type="2d" id="{{canvasId}}" style="width: {{customOptions.canvasSize.width}}rpx; height: {{customOptions.canvasSize.height}}rpx"></canvas>
2 changes: 1 addition & 1 deletion dist/progress.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! mp-progress.js v1.2.8 https://www.npmjs.com/package/mp-progress */
/*! mp-progress.js v1.2.10 https://www.npmjs.com/package/mp-progress */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down
2 changes: 1 addition & 1 deletion dist/progress.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "mp-progress",
"version": "1.2.8",
"version": "1.2.10",
"description": "circle progress for wechat miniprogram",
"main": "dist/progress.min.js",
"miniprogram": "dist",
"scripts": {
"debug": "webpack",
"watch": "webpack --watch",
Expand Down

0 comments on commit c8c8c02

Please sign in to comment.