From 9842802a547a53ea8d82d65c2e7d0e1c3acbd888 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Wed, 21 Apr 2021 18:04:09 +0800
Subject: [PATCH] 数值增减组件增加奇偶修正功能

---
 forms/datePicker/CDateRangeAction.vue |    4 ++--
 forms/datePicker/CDateTimeAction.vue  |    4 ++--
 forms/numberStep/CNumberStep.vue      |   38 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/forms/datePicker/CDateRangeAction.vue b/forms/datePicker/CDateRangeAction.vue
index 65c19d6..3ad0181 100644
--- a/forms/datePicker/CDateRangeAction.vue
+++ b/forms/datePicker/CDateRangeAction.vue
@@ -28,7 +28,7 @@
                 </view>
                 <view class="date">
                     <picker
-                        mode='date'
+                        mode="date"
                         :value="startDate"
                         @change="evt => handleStartDateChange(evt.detail.value)"
                     >
@@ -41,7 +41,7 @@
                         </view>
                     </picker>
                     <picker
-                        mode='date'
+                        mode="date"
                         :value="endDate"
                         @change="evt => handleEndDateChange(evt.detail.value)"
                     >
diff --git a/forms/datePicker/CDateTimeAction.vue b/forms/datePicker/CDateTimeAction.vue
index 9ac2ad2..03919d0 100644
--- a/forms/datePicker/CDateTimeAction.vue
+++ b/forms/datePicker/CDateTimeAction.vue
@@ -33,7 +33,7 @@
                 </view>
                 <view class="date">
                     <picker
-                        mode='date'
+                        mode="date"
                         :value="date"
                         @change="evt => handleDateChange(evt.detail.value)"
                     >
@@ -46,7 +46,7 @@
                         </view>
                     </picker>
                     <picker
-                        mode='time'
+                        mode="time"
                         :value="time"
                         @change="evt => handleTimeChange(evt.detail.value)"
                     >
diff --git a/forms/numberStep/CNumberStep.vue b/forms/numberStep/CNumberStep.vue
index 324af11..add9fd5 100644
--- a/forms/numberStep/CNumberStep.vue
+++ b/forms/numberStep/CNumberStep.vue
@@ -24,7 +24,7 @@
                 :step="step"
                 :width="120"
                 :value="itemRes.formData[itemRes.name]"
-                :onChange="evt=>itemRes.onChange(evt)"
+                :onChange="evt=>handleChange(evt)"
             />
             <view class="c-number-step-unit">{{unit}}</view>
         </view>
@@ -58,6 +58,11 @@
             type: Number,
             default: 1,
         },
+        // 奇偶修正 odd 奇数 / even 偶数
+        correct: {
+            type: String,
+            default: '',
+        },
         // 数值单位
         unit: {
             type: String,
@@ -68,7 +73,36 @@
         return {};
     },
     computed: {},
-    methods: {},
+    methods: {
+        handleChange(val) {
+            // 奇偶修正模式
+            if (this.correct) {
+                const lastValue = this.itemRes.formData[this.itemRes.name];
+                let nextValue = val;
+                if (
+                    (this.correct === 'odd' && nextValue % 2 === 0) ||
+                    (this.correct === 'even' && nextValue % 2 === 1)
+                ) {
+                    // 增加
+                    if (lastValue < nextValue) {
+                        nextValue++;
+                    }
+                    // 减小
+                    else if (lastValue > nextValue) {
+                        nextValue--;
+                    }
+                }
+                // 范围
+                nextValue = Math.max(nextValue, this.range[0]);
+                nextValue = Math.min(nextValue, this.range[1]);
+                this.itemRes.onChange(nextValue);
+            }
+            // 正常模式
+            else {
+                this.itemRes.onChange(val);
+            }
+        },
+    },
     mounted() {
         if (process.env.TARO_ENV === 'h5') {
             $(this.$refs.input.$el)

--
Gitblit v1.9.1