uniapp官方数字输入框组件修改版

本文介绍了作者对uniapp官方数字输入框组件的不满意之处,并分享了手动修改后的版本,强调了解决问题要从事件源入手。文章还提到了11.21的更新内容,关于value属性在父子组件通信中的作用,以及如何通过改变value来设置数字输入框的默认值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uniapp官方给的数字输入框组件就是个shit,下面上手动修改过的新版本
解决问题的核心就是——遇到问题寻找事件源,这样才能解决问题

11.21更新—— 这个value的值也是有用的,之前觉得没用,因为这是父子传参,单向数据流动,value是props中接收父组件的一个参数,改变它才能改变数字输入框上的默认值,不然如果你的子组件props-value中default的值就是数字输入框默认显示的值

<template>
	<view class="uni-numbox">
		<view @click="_calcValue('minus')" class="uni-numbox__minus">
			<text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</text>
		</view>
		<input :disabled="disabled" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
		<view @click="_calcValue('plus')" class="uni-numbox__plus">
			<text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">+</text>
		</view>
	</view>
</template>
<script>
	export default {
		name: "UniNumberBox",
		props: {
			msg:Object,//父传子,把类型传过去,因为有多个输入框
			value: {
				type: [Number, String],
				default: 0.20
			},
			min: {
				type: Number,
				default: 0.2
			},
			max: {
				type: Number,
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				inputValue: 0
			};
		},
		watch: {
			value(val) {
				this.inputValue = +val;
			},
			inputValue(newVal, oldVal) {
				if (+newVal !== +oldVal) {
					console.log(this.msg)
					let obj = {
						num:newVal,
						type:this.msg.type//把从父组件收到的类型再回传给父组件,父组件的change事件就接收类型,通过这个类型做判断
					}
					this.$emit("change", obj);//在这里调用父组件上的change事件,实现子向父传参
				}
			}
		},
		created() {
			this.inputValue = +this.value;
		},
		methods: {
			_calcValue(type) {
				if (this.disabled) {
					return;
				}
				const scale = this._getDecimalScale();
				let value = this.inputValue * scale;				
				let step = this.step * scale;
				if (type === "minus") {
					value -= step;
					if (value < this.min) {
						return;
					}
					if(value > this.max){
						value = this.max
					}
				} else if (type === "plus") {
					value += step;
					if (value > this.max) {
						return;
					}
					if(value < this.min){
						value = this.min
					}
				}
				value <=20?value=20:value;//限制最小值
				value >=60?value=60:value;//限制最大值 
				this.inputValue = String(Number(value / scale).toFixed(2));//inputvalue才是显示的数字,只保留他小数点后两位
			},
			_getDecimalScale() {
				let scale = 1;
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + "").split(".")[1].length);
				}
				return scale;
			},
			_onBlur(event) {
				let value = event.detail.value;
				if (!value) {
					// this.inputValue = 0;
					return;
				}
				value = +value;
				if (value > this.max) {
					value = this.max;
				} else if (value < this.min) {
					value = this.min;
				}
				this.inputValue = value;
			}
		}
	};
</script>
<style lang="scss" scoped>
	$box-height: 35px;
	/* #ifdef APP-NVUE */
	$box-line-height: 35px;
	/* #endif */
	$box-line-height: 26px;
	$box-width: 35px;

	.uni-numbox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		height: $box-height;
		line-height: $box-height;
		width: 120px;
	}

	.uni-numbox__value {
		background-color: $uni-bg-color;
		width: 40px;
		height: $box-height;
		text-align: center;
		font-size: $uni-font-size-lg;
		border-width: 1rpx;
		border-style: solid;
		border-color: $uni-border-color;
		border-left-width: 0;
		border-right-width: 0;
	}

	.uni-numbox__minus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		// line-height: $box-line-height;
		// text-align: center;
		font-size: 20px;
		color: $uni-text-color;
		background-color: $uni-bg-color-grey;
		border-width: 1rpx;
		border-style: solid;
		border-color: $uni-border-color;
		border-top-left-radius: $uni-border-radius-base;
		border-bottom-left-radius: $uni-border-radius-base;
		border-right-width: 0;
	}

	.uni-numbox__plus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		border-width: 1rpx;
		border-style: solid;
		border-color: $uni-border-color;
		border-top-right-radius: $uni-border-radius-base;
		border-bottom-right-radius: $uni-border-radius-base;
		background-color: $uni-bg-color-grey;
		border-left-width: 0;
	}

	.uni-numbox--text {
		font-size: 40rpx;
		color: $uni-text-color;
	}

	.uni-numbox--disabled {
		color: $uni-text-color-disable;
	}
</style>

import uniNumberBox from "@/components/uni-number-box/uni-number-box.vue"


<!-- 数字输入框 -->
			<view class="create-item">
				<label><text class="my-icon">&#xe7b0;</text>微信利率</label>
				<uni-number-box :step="0.01" @change="change" :msg="wxPayProrata"/>
			</view>
			<view class="create-item">
				<label><text class="my-icon">&#xe7b0;</text>支付宝利率</label>
				<uni-number-box :step="0.01"  @change="change" :msg="zfbPayProrata"/>
			</view>
			<view class="create-item">
				<label><text class="my-icon">&#xe7b0;</text>乐刷利率</label>
				<uni-number-box :step="0.01"  @change="change" :msg="lsPayProrata" />
			</view>
			<view class="create-item">
				<label><text class="my-icon">&#xe7b0;</text>客如云利率</label>
				<uni-number-box :step="0.01"  @change="change" :msg="kryPayProrata" />
			</view>
			<view class="create-item">
				<label><text class="my-icon">&#xe7b0;</text>天阙随行付利率</label>
				<uni-number-box :step="0.01"  @change="change" :msg="tqSxfPayProrata" />
			</view>


methods:{
	//获取利率
			change(value) {
				switch (value.type) {
					case 0:
						this.wxPayProrata.num = value.num
						break;
					case 1:
						this.zfbPayProrata.num = value.num
						break;
					case 2:
						this.lsPayProrata.num = value.num
						break;
					case 3:
						this.kryPayProrata.num = value.num
						break;
					case 4:
						this.tqSxfPayProrata.num = value.num
						break;
				}
				// console.log(typeof Number((Number(this.wxPayProrata.num)/100).toFixed(4)))
				
			},
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值