Browse Source

fix(stepper): add decimalPlaces prop.

famanoder 6 years ago
parent
commit
7274082a6f
3 changed files with 25 additions and 13 deletions
  1. 3 3
      src/packages/stepper/demo.vue
  2. 1 0
      src/packages/stepper/doc.md
  3. 21 10
      src/packages/stepper/stepper.vue

+ 3 - 3
src/packages/stepper/demo.vue

@@ -4,7 +4,7 @@
     <div>
       <nut-cell>
         <span slot="title">
-          <nut-stepper @focus="focus" :blur="blur" :value.sync="val1" :max="12"></nut-stepper>
+          <nut-stepper @focus="focus" :blur="blur" :value.sync="val1" :max="12" :step="0.1" :decimalPlaces="1" @add="add"></nut-stepper>
         </span>
         <span slot="desc">
           value: {{val1}} <button @click="reduce" class="demo-btn">-</button> <button @click="add" class="demo-btn">+</button> 
@@ -59,7 +59,7 @@
 export default {
   data() {
     return {
-      val1: 0,
+      val1: 1,
       val2: 12,
       val3: 5,
       val4: 0,
@@ -76,7 +76,7 @@ export default {
     },
     add(v) {
       console.log(v)
-      this.val1 = Number(this.val1) + 1;
+      // this.val1 = Number(this.val1) + 1;
     },
     reduce() {
       this.val1 = Math.max(Number(this.val1) - 1, 0);

+ 1 - 0
src/packages/stepper/doc.md

@@ -65,6 +65,7 @@
 | readonly | 是否只读 | Boolean | false
 | transition | 是否需要过渡效果 | Boolean | true
 | simple | 是否显示简单版 | Boolean | true
+| decimalPlaces | 设置保留的小数位 | Number | 0
 
 ## Event
 

+ 21 - 10
src/packages/stepper/stepper.vue

@@ -80,7 +80,11 @@ export default {
         value: {
             type: [String, Number],
             required: true
-        }
+        },
+        decimalPlaces: {
+            type: Number,
+            default: 0
+        } 
     },
     data() {
         return {
@@ -105,11 +109,14 @@ export default {
         }
     },
     watch: {
-        value(v, ov) {
-            if(v > this.max) v = this.max;
-            if(v < this.minNum) v = this.minNum;
-            this.num = v;
-            this.$emit('change', this.num);
+        value: {
+            handler(v, ov) {
+                if(v > this.max) v = this.max;
+                if(v < this.minNum) v = this.minNum;
+                this.num = v > 0? this.fixedDecimalPlaces(v): v;
+                this.$emit('change', this.num);
+            },
+            immediate: true
         }
     },
     computed: {
@@ -161,12 +168,16 @@ export default {
             this.$emit('update:value', this.num);
             this.$emit('change', this.num);
         },
+        fixedDecimalPlaces(v) {
+            return Number(v).toFixed(this.decimalPlaces);
+            // .replace(/(\d+\.[^0]*)0+$/, '$1').replace(/\.$/, '')
+        },
         add() {
             this.num = Number(this.num);
             if(this.num <= this.max - this.step && this.max > this.minNum) {
-                let [n1, n2] = this.num.toString().split('.');
+                let [n1, n2] = this.fixedDecimalPlaces(this.num + Number(this.step)).split('.');
                 let fixedLen = n2? n2.length: 0;
-                this.num = parseFloat((Number(n1) + Number(this.step)) + (n2? '.'+n2: '')).toFixed(fixedLen);
+                this.num = parseFloat(n1 + (n2? '.'+n2: '')).toFixed(fixedLen);
                 if(this.transition) {
                     this.showNum = false;
                     this.showAddAnim = true;
@@ -192,9 +203,9 @@ export default {
         },
         reduce(){
             if(this.num - this.step >= this.minNum) {
-                let [n1, n2] = this.num.toString().split('.');
+                let [n1, n2] = this.fixedDecimalPlaces(this.num - Number(this.step)).split('.');
                 let fixedLen = n2? n2.length: 0;
-                this.num = parseFloat((Number(n1) - this.step) + (n2? '.'+n2: '')).toFixed(fixedLen);
+                this.num = parseFloat(n1 + (n2? '.'+n2: '')).toFixed(fixedLen);
                 if(this.transition) {
                     this.showNum = false;
                     this.showAddAnim = false;