/**
|
* CNumberStep
|
* @author Tevin
|
*/
|
|
<template>
|
<view class="c-number-step">
|
<AtInput
|
ref="input"
|
:name="itemRes.name"
|
:title="itemRes.label"
|
:required="itemRes.required"
|
:disabled="itemRes.disabled"
|
:error="itemRes.error"
|
/>
|
<view
|
class="c-number-step-step"
|
:class="[unit?'on-unit':'', isEmpty?'on-empty':'']"
|
ref="number"
|
>
|
<AtInputNumber
|
:min="range[0]"
|
:max="range[1]"
|
:step="step"
|
:width="120"
|
:value="itemRes.formData[itemRes.name]"
|
:onChange="evt=>handleChange(evt)"
|
/>
|
<view class="c-number-step-unit">{{unit}}</view>
|
</view>
|
</view>
|
</template>
|
|
<script>
|
import Taro from '@tarojs/taro';
|
import { $ } from '@tarojs/extend';
|
import { AtInput, AtInputNumber } from 'taro-ui-vue';
|
import './cNumberStep.scss';
|
|
export default {
|
name: 'CNumberStep',
|
components: {
|
AtInput,
|
AtInputNumber,
|
},
|
props: {
|
// 表单数据资源(表单组件内部机制专用)
|
itemRes: Object,
|
// 占位提示
|
placeholder: String,
|
// 取值范围
|
range: {
|
type: Array,
|
default: () => [0, 100],
|
},
|
// 步长
|
step: {
|
type: Number,
|
default: 1,
|
},
|
// 奇偶修正 odd 奇数 / even 偶数
|
correct: {
|
type: String,
|
default: '',
|
},
|
// 数值单位
|
unit: {
|
type: String,
|
default: '',
|
},
|
},
|
data() {
|
return {};
|
},
|
computed: {
|
// 空值判断,弥补空值时显示最小值的问题
|
isEmpty() {
|
const curVal = this.itemRes.formData[this.itemRes.name];
|
if (typeof curVal === 'undefined') {
|
return true;
|
} else if (!curVal && curVal !== 0) {
|
return true;
|
} else {
|
return false;
|
}
|
},
|
},
|
methods: {
|
handleChange(val) {
|
// 奇偶修正模式
|
if (this.correct) {
|
const lastValue = this.isEmpty
|
? this.range[0]
|
: 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)
|
.find('.at-input__container')
|
.prepend(this.$refs.number.$el);
|
} else if (process.env.TARO_ENV === 'weapp') {
|
$(this.$refs.input.$el)
|
.find('.at-input__container')
|
.append(this.$refs.number);
|
}
|
},
|
};
|
</script>
|