Skip to content

NumberBox

This component is generally used in the scene where the number of items is selected for shopping in the mall

Note: This input box can only enter integers greater than or equal to 0

平台兼容性

App(vue)App(nvue)H5小程序VUE2VUE3

Basic usage

Bind the initial value of value through v-model, this value is two-way bound, no need to reassign the returned value to value in the callback.

vue
<template>
    <wu-number-box v-model="value" @change="valChange"></wu-number-box>
</template>
<script>
export default {
    data() {
        return {
            value: 0
        }
    },
    methods: {
        valChange(e) {
            console.log('The current value is: ' + e.value)
        }
    }
}
</script>
<template>
    <wu-number-box v-model="value" @change="valChange"></wu-number-box>
</template>
<script>
export default {
    data() {
        return {
            value: 0
        }
    },
    methods: {
        valChange(e) {
            console.log('The current value is: ' + e.value)
        }
    }
}
</script>

Step size setting

  • Use the step attribute to set the value that changes each time the increase or decrease button is clicked. The default is 1. The following example will add 2 or subtract 2 each time
vue
<wu-number-box :step="2"></wu-number-box>
<wu-number-box :step="2"></wu-number-box>

Limit input range

Limit the input value minimum and maximum value by min and max parameters

vue
<wu-number-box :min="1" :max="100"></wu-number-box>
<wu-number-box :min="1" :max="100"></wu-number-box>

Limit input to only integers

Restrict input type by integer

vue
<wu-number-box integer></wu-number-box>
<wu-number-box integer></wu-number-box>

disable

vue
<!-- Disable the input box by setting the `disabled` parameter. In the disabled state, you cannot click the plus and minus buttons or modify the value of the input box -->
<wu-number-box :disabled="true"></wu-number-box>

<!-- disable input box -->
<wu-number-box :disabledInput="true"></wu-number-box>

<!-- Disable add button -->
<wu-number-box :disablePlus="true"></wu-number-box>

<!-- Disable the decrease button -->
<wu-number-box :disableMinus="true"></wu-number-box>

<!-- Disable long press -->
<wu-number-box :longPress="false"></wu-number-box>
<!-- Disable the input box by setting the `disabled` parameter. In the disabled state, you cannot click the plus and minus buttons or modify the value of the input box -->
<wu-number-box :disabled="true"></wu-number-box>

<!-- disable input box -->
<wu-number-box :disabledInput="true"></wu-number-box>

<!-- Disable add button -->
<wu-number-box :disablePlus="true"></wu-number-box>

<!-- Disable the decrease button -->
<wu-number-box :disableMinus="true"></wu-number-box>

<!-- Disable long press -->
<wu-number-box :longPress="false"></wu-number-box>

fixed number of decimal places

Use step to set the step length, and decimal-length to set the number of decimal places

vue
<wu-number-box step="0.25" decimal-length="1"></wu-number-box>
<wu-number-box step="0.25" decimal-length="1"></wu-number-box>

Asynchronous changes

Set the asynchronous change through asyncChange, you need to manually control the input value after it is turned on (default false)

vue
<template>
	<wu-number-box v-model="value" :asyncChange="true" @change="onChange"></wu-number-box>
</template>
<script>
	export default {
		data() {
			return {
				value: 1
			}
		},
		methods: {
			onChange(e) {
				uni.showLoading({
					title: 'Loading',
					mask: true
				})
				setTimeout(() => {
					this.value = this.value + 1;
					uni.hideLoading();
				}, 1000)
			}
		}
	}
</script>
<template>
	<wu-number-box v-model="value" :asyncChange="true" @change="onChange"></wu-number-box>
</template>
<script>
	export default {
		data() {
			return {
				value: 1
			}
		},
		methods: {
			onChange(e) {
				uni.showLoading({
					title: 'Loading',
					mask: true
				})
				setTimeout(() => {
					this.value = this.value + 1;
					uni.hideLoading();
				}, 1000)
			}
		}
	}
</script>

Custom color and size

  • Set button size via button-size parameter
  • Set the icon style of plus and minus button via icon-style parameter
vue
<wu-number-box button-size="36" color="#ffffff" bgColor="#2979ff" iconStyle="color: #fff"></wu-number-box>
<wu-number-box button-size="36" color="#ffffff" bgColor="#2979ff" iconStyle="color: #fff"></wu-number-box>

Custom slots

vue
<template>
	<view class="" style="padding: 50rpx">
		<wu-number-box v-model="value">
			<template #minus>
				<view slot="minus" class="minus">
					<wu-icon name="minus" size="12"></wu-icon>
				</view>
			</template>
			<template #input>
				<text style="width: 50px;text-align: center;" class="input">{{value}}</text>
			</template>
			<template #plus>
				<view class="plus">
					<wu-icon name="plus" color="#FFFFFF" size="12"></wu-icon>
				</view>
			</template>
		</wu-number-box>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: 1
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/uni_modules/wu-ui-tools/libs/css/components.scss';

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
		font-size: 20px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
</style>
<template>
	<view class="" style="padding: 50rpx">
		<wu-number-box v-model="value">
			<template #minus>
				<view slot="minus" class="minus">
					<wu-icon name="minus" size="12"></wu-icon>
				</view>
			</template>
			<template #input>
				<text style="width: 50px;text-align: center;" class="input">{{value}}</text>
			</template>
			<template #plus>
				<view class="plus">
					<wu-icon name="plus" color="#FFFFFF" size="12"></wu-icon>
				</view>
			</template>
		</wu-number-box>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: 1
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/uni_modules/wu-ui-tools/libs/css/components.scss';

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
		font-size: 20px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
</style>

API

NumberBox Props

property namedescriptiontypedefault valueoptional values ​​
value / modelValue / v-modelThe value used for two-way binding, set to the default min value (minimum value) during initializationString / Number1-
nameThe stepper identifier, returned in the change callbackString / Number--
minThe minimum value that the user can enterString / Number1-
maxThe maximum value that can be entered by the userString / NumberNumber.MAX_SAFE_INTEGER-
stepStep size, the value added or subtracted each time, supports decimal values, if decimals are requiredString / Number1-
integerWhether only positive integers can be enteredBooleanfalsetrue / false
disabledWhether to disable operations, including input boxes, plus and minus buttonsBooleanfalsetrue / false
disabledInputWhether to prohibit the input boxBooleanfalsetrue / false
asyncChangeWhether to enable asynchronous change, after enabling it, you need to manually control the input valueBooleanfalsetrue / false
inputWidthInput box width, unit pxString / Number35-
showMinusWhether to show the decrease buttonBooleantruetrue / false
showPlusWhether to display the add buttonBooleantruetrue / false
decimalLengthnumber of decimal places to displayString / Number--
longPressWhether to allow long press to add and subtractBooleantruetrue / false
colorThe color of the input box text and the plus and minus button iconsString#323233-
buttonSizeButton size, width and height are equal to this value, unit px, input box height is consistent with this valueString / Number30-
bgColorThe background color of input boxes and buttonsString#EBECEE-
cursorSpacingSpecify the distance between the cursor and the keyboard to prevent the keyboard from covering the input box, the unit is pxString / Number100-
disableMinusWhether to disable the decrease buttonBooleantruetrue / false
disablePlusWhether to disable the add buttonBooleantruetrue / false
iconStyleThe style of the plus and minus button iconsString--

NumberBox Events

Event NameDescriptionCallback Parameters
focusThe input box is triggered by focus (when the button is clickable), in the form of an objectvalue: the current value of the input box, name: the stepper identifier
blurTriggered when the input box loses focus, in the form of an objectvalue: the current value of the input box, name: the stepper identifier
changeTriggered when the content of the input box changes, in the form of an objectvalue: the current value of the input box, name: the stepper identifier
overlimitTriggered when the range threshold is exceededtype: (minus has reached the minimum value, plus has reached the maximum value)

NumberBox Slots

slot nameillustrate
minusreduce button
inputinput
plusadd button