Skip to content

Commit ff99b22

Browse files
fix: 🐛 优化 InputNumbe 处理中间状态值的逻辑,支持配置不立即响应输入变化 (#1116)
1 parent cb408f5 commit ff99b22

6 files changed

Lines changed: 1623 additions & 304 deletions

File tree

docs/component/input-number.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ function handleChange({ value }) {
4949
<wd-input-number v-model="value" @change="handleChange" disable-input />
5050
```
5151

52+
## 禁用按钮
53+
54+
可以单独禁用增加或减少按钮。
55+
56+
```html
57+
<!-- 禁用减号按钮 -->
58+
<wd-input-number v-model="value" @change="handleChange" disable-minus />
59+
60+
<!-- 禁用加号按钮 -->
61+
<wd-input-number v-model="value" @change="handleChange" disable-plus />
62+
```
5263

5364
## 无输入框
5465

@@ -97,6 +108,49 @@ function handleChange({ value }) {
97108
}
98109
```
99110

111+
## 非立即更新模式
112+
113+
设置 `immediate-change``false`,输入框内容变化时不会立即触发 `change` 事件,仅在失焦或点击按钮时触发。
114+
115+
```html
116+
<!-- 立即更新模式(默认) -->
117+
<wd-input-number v-model="value1" @change="handleChange" :immediate-change="true" />
118+
119+
<!-- 非立即更新模式 -->
120+
<wd-input-number v-model="value2" @change="handleChange" :immediate-change="false" />
121+
```
122+
123+
```typescript
124+
const value1 = ref<number>(1)
125+
const value2 = ref<number>(1)
126+
function handleChange({ value }) {
127+
console.log(value)
128+
}
129+
```
130+
131+
## 初始化时自动更新
132+
133+
设置 `update-on-init` 属性控制是否在初始化时更新 `v-model` 为修正后的值。
134+
135+
-`update-on-init="true"`(默认)时,会将初始值修正到符合 `min``max``step``precision` 等规则的有效值,并同步更新 `v-model`
136+
-`update-on-init="false"` 时,保持初始值不修正(不改变 `v-model`),但仍会进行显示格式化(如精度处理)
137+
138+
```html
139+
<!-- 自动更新初始值(默认) -->
140+
<wd-input-number v-model="value1" @change="handleChange" :update-on-init="true" :min="3" :max="15" :step="2" step-strictly />
141+
142+
<!-- 不更新初始值,保持原始值 -->
143+
<wd-input-number v-model="value2" @change="handleChange" :update-on-init="false" :min="3" :max="15" :step="2" step-strictly />
144+
```
145+
146+
```typescript
147+
const value1 = ref<number>(1) // 会自动修正为4(≥3的最小2的倍数)
148+
const value2 = ref<number>(1) // 保持为1,不会修正但会格式化显示
149+
function handleChange({ value }) {
150+
console.log(value)
151+
}
152+
```
153+
100154
## 异步变更
101155

102156
通过 `before-change` 可以在输入值变化前进行校验和拦截。
@@ -153,6 +207,9 @@ const beforeChange: InputNumberBeforeChange = (value) => {
153207
| adjustPosition | 原生属性,键盘弹起时,是否自动上推页面 | boolean | - | true | 1.3.11 |
154208
| before-change | 输入框值改变前触发,返回 false 会阻止输入框值改变,支持返回 `Promise` | `(value: number \| string) => boolean \| Promise<boolean>` | - | - | 1.6.0 |
155209
| long-press | 是否允许长按进行加减 | boolean | - | false | 1.8.0 |
210+
| immediate-change | 是否立即响应输入变化,false 时仅在失焦和按钮点击时更新 | boolean | - | true | $LOWEST_VERSION$ |
211+
| update-on-init | 是否在初始化时更新 v-model 为修正后的值 | boolean | - | true | $LOWEST_VERSION$ |
212+
| input-type | 输入框类型 | string | number / digit | digit | $LOWEST_VERSION$ |
156213

157214

158215
## Events

docs/en-US/component/input-number.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ Set the `disable-input` property.
4949
<wd-input-number v-model="value" @change="handleChange" disable-input />
5050
```
5151

52+
## Disable Buttons
53+
54+
You can disable the increase or decrease buttons individually.
55+
56+
```html
57+
<!-- Disable minus button -->
58+
<wd-input-number v-model="value" @change="handleChange" disable-minus />
59+
60+
<!-- Disable plus button -->
61+
<wd-input-number v-model="value" @change="handleChange" disable-plus />
62+
```
63+
5264
## Without Input Box
5365

5466
Set `without-input` to hide the input box.
@@ -96,6 +108,49 @@ function handleChange({ value }) {
96108
}
97109
```
98110

111+
## Non-Immediate Update Mode
112+
113+
Set `immediate-change` to `false`, the `change` event will not be triggered immediately when the input box content changes, only when it loses focus or buttons are clicked.
114+
115+
```html
116+
<!-- Immediate update mode (default) -->
117+
<wd-input-number v-model="value1" @change="handleChange" :immediate-change="true" />
118+
119+
<!-- Non-immediate update mode -->
120+
<wd-input-number v-model="value2" @change="handleChange" :immediate-change="false" />
121+
```
122+
123+
```typescript
124+
const value1 = ref<number>(1)
125+
const value2 = ref<number>(1)
126+
function handleChange({ value }) {
127+
console.log(value)
128+
}
129+
```
130+
131+
## Auto-update on Initialization
132+
133+
Set the `update-on-init` property to control whether to update the `v-model` with the corrected value during initialization.
134+
135+
- When `update-on-init="true"` (default), the initial value will be corrected to comply with `min`, `max`, `step`, `precision` and other rules, and the `v-model` will be updated synchronously
136+
- When `update-on-init="false"`, the initial value will not be corrected (v-model unchanged), but display formatting (such as precision) will still be applied
137+
138+
```html
139+
<!-- Auto-update initial value (default) -->
140+
<wd-input-number v-model="value1" @change="handleChange" :update-on-init="true" :min="3" :max="15" :step="2" step-strictly />
141+
142+
<!-- Don't update initial value, keep original value -->
143+
<wd-input-number v-model="value2" @change="handleChange" :update-on-init="false" :min="3" :max="15" :step="2" step-strictly />
144+
```
145+
146+
```typescript
147+
const value1 = ref<number>(1) // Will be auto-corrected to 4 (minimum multiple of 2 that is ≥3)
148+
const value2 = ref<number>(1) // Remains 1, will not be corrected but will be formatted for display
149+
function handleChange({ value }) {
150+
console.log(value)
151+
}
152+
```
153+
99154
## Asynchronous Change
100155

101156
Through `before-change`, you can validate and intercept before the input value changes.
@@ -152,6 +207,9 @@ Set the `long-press` property to allow long press for increment/decrement.
152207
| adjustPosition | Native property, whether to automatically push up the page when keyboard pops up | boolean | - | true | 1.3.11 |
153208
| before-change | Triggered before input box value changes, returning false will prevent input box value from changing, supports returning `Promise` | `(value: number \| string) => boolean \| Promise<boolean>` | - | - | 1.6.0 |
154209
| long-press | Whether to allow long press for increment/decrement | boolean | - | false | 1.8.0 |
210+
| immediate-change | Whether to respond to input changes immediately, false will only update on blur and button clicks | boolean | - | true | $LOWEST_VERSION$ |
211+
| update-on-init | Whether to update v-model with corrected value during initialization | boolean | - | true | $LOWEST_VERSION$ |
212+
| input-type | Input field type | string | number / digit | digit | $LOWEST_VERSION$ |
155213

156214
## Events
157215

0 commit comments

Comments
 (0)