quantity component

This commit is contained in:
cookfront
2017-03-10 15:09:10 +08:00
parent 4935ca66ac
commit f6b3eb8365
12 changed files with 310 additions and 2 deletions
+8
View File
@@ -0,0 +1,8 @@
## 0.0.2 (2017-01-20)
* 改了bug A
* 加了功能B
## 0.0.1 (2017-01-10)
* 第一版
+26
View File
@@ -0,0 +1,26 @@
# @youzan/<%= name %>
!!! 请在此处填写你的文档最简单描述 !!!
[![version][version-image]][download-url]
[![download][download-image]][download-url]
[version-image]: http://npm.qima-inc.com/badge/v/@youzan/<%= name %>.svg?style=flat-square
[download-image]: http://npm.qima-inc.com/badge/d/@youzan/<%= name %>.svg?style=flat-square
[download-url]: http://npm.qima-inc.com/package/@youzan/<%= name %>
## Demo
## Usage
## API
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------|
| className | 自定义额外类名 | string | '' | '' |
## License
[MIT](https://opensource.org/licenses/MIT)
+3
View File
@@ -0,0 +1,3 @@
import Quantity from './src/quantity';
export default Quantity;
+10
View File
@@ -0,0 +1,10 @@
{
"name": "<%= name %>",
"version": "<%= version %>",
"description": "<%= description %>",
"main": "./lib/index.js",
"author": "<%= author %>",
"license": "<%= license %>",
"devDependencies": {},
"dependencies": {}
}
+104
View File
@@ -0,0 +1,104 @@
<template>
<div class="zan-quantity">
<button
@click="handleChange('minus')"
class="zan-quantity__stepper zan-quantity__minus"
:class="{
'zan-quantity__minus--disabled': isMinusDisabled
}">
</button>
<input type="text" class="zan-quantity__input" :value="currentValue" @input="handleInputChange" :disabled="disabled">
<button
@click="handleChange('plus')"
class="zan-quantity__stepper zan-quantity__plus"
:class="{
'zan-quantity__plus--disabled': isPlusDisabled
}">
</button>
</div>
</template>
<script>
export default {
name: 'zan-quantity',
props: {
min: {
type: [String, Number],
default: 1
},
max: {
type: [String, Number],
default: Infinity
},
value: {},
step: {
type: [String, Number],
default: 1
},
disabled: Boolean,
defaultValue: {
type: [String, Number],
default: 1
}
},
data() {
return {
currentValue: this.value ? +this.value : +this.defaultValue
};
},
computed: {
isMinusDisabled() {
const min = +this.min;
const step = +this.step;
const currentValue = +this.currentValue;
return min === currentValue || (currentValue - step) < min || this.disabled;
},
isPlusDisabled() {
const max = +this.max;
const step = +this.step;
const currentValue = +this.currentValue;
return max === currentValue || (currentValue + step) > max || this.disabled;
}
},
watch: {
currentValue(val) {
this.$emit('input', +val);
},
value(val) {
if (val) {
this.currentValue = +val;
}
}
},
methods: {
handleInputChange(event) {
let val = +event.target.value;
const max = +this.max;
const min = +this.min;
if (val > max) {
val = max;
} else if (val < min) {
val = min;
}
this.currentValue = val;
},
handleChange(type) {
if ((this.isMinusDisabled && type === 'minus') || (this.isPlusDisabled && type === 'plus')) {
return;
}
const step = +this.step;
const currentValue = +this.currentValue;
this.currentValue = type === 'minus' ? (currentValue - step) : (currentValue + step);
this.$emit('change', this.currentValue);
}
}
};
</script>