123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <template>
- <view class="fs-number-box" :class="{ 'fs-number-box-round': round }">
- <view class="fs-number-box-item fs-number-box-left" @click="minus">
- <fs-icon type="icon-minus" size="32rpx" :color="minusDisabled ? '#c8c9cc' : ''"></fs-icon>
- </view>
- <input
- class="fs-number-box-item fs-number-box-middle"
- type="number"
- :value="modelValue"
- @blur="handleChange"
- :disabled="disableInput"
- />
- <view class="fs-number-box-item fs-number-box-right" @click="add">
- <fs-icon type="icon-plus" size="32rpx" :color="addDisabled ? '#c8c9cc' : ''"></fs-icon>
- </view>
- </view>
- </template>
- <script>
- /**
- * 步进器组件
- * @description 步进器组件
- * @property {Number} min 最小值
- * @property {Number} max 最大值
- * @property {Number} step 步幅
- * @property {Boolean} round 是否圆角
- * @property {Boolean} disableInput 禁止输入框输入
- */
- export default {
- name: 'fs-number-box'
- }
- </script>
- <script setup>
- import { ref, watch, computed } from 'vue'
- const props = defineProps({
- modelValue: {
- type: Number,
- default: 1
- },
- min: {
- type: Number,
- default: 1
- },
- max: {
- type: Number,
- default: Number.MAX_SAFE_INTEGER
- },
- step: {
- type: Number,
- default: 1
- },
- round: Boolean,
- disableInput: Boolean
- })
- const emits = defineEmits(['update:modelValue', 'change'])
- let initValue = ref(props.modelValue)
- watch(
- () => props.modelValue,
- val => {
- initValue.value = val
- }
- )
- watch(initValue, val => {
- emits('update:modelValue', val)
- emits('change', val)
- })
- const add = () => {
- initValue.value += Number(props.step)
- if (initValue.value > props.max) {
- initValue.value = props.max
- }
- }
- const minus = () => {
- initValue.value -= Number(props.step)
- if (initValue.value < props.min) {
- initValue.value = props.min
- }
- }
- const handleChange = e => {
- initValue.value = Number(e.detail.value) || props.min
- if (initValue.value < props.min) {
- initValue.value = props.min
- } else if (initValue.value > props.max) {
- initValue.value = props.max
- }
- }
- const minusDisabled = computed(() => initValue.value === props.min)
- const addDisabled = computed(() => initValue.value === props.max)
- </script>
- <style lang="scss" scoped>
- .fs-number-box {
- display: inline-flex;
- border: 2rpx solid var(--border-color);
- height: 60rpx;
- background-color: #fff;
- &-round {
- border-radius: 30rpx;
- }
- &-item {
- display: flex;
- height: 100%;
- justify-content: center;
- align-items: center;
- }
- &-left,
- &-right {
- width: 60rpx;
- }
- &-middle {
- box-sizing: border-box;
- width: 80rpx;
- border-left: 2rpx solid var(--border-color);
- border-right: 2rpx solid var(--border-color);
- padding: 10rpx;
- text-align: center;
- }
- }
- </style>
|