Skip to content

Commit

Permalink
fix(number-input): unexpected behavior in sync mode
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Apr 5, 2023
1 parent 0a39d69 commit 9c77dc3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
47 changes: 18 additions & 29 deletions components/number-input/number-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import { numberInputProps } from './props'
type InputEventType = 'input' | 'change'
const numberRE = /^-?[0-9]*\.?[0-9]*$/
const isNullOrNaN = (value: unknown) => isNull(value) || Number.isNaN(value)
const isEmpty = (value: unknown) => !value && value !== 0
export default defineComponent({
Expand Down Expand Up @@ -251,12 +250,6 @@ export default defineComponent({
? props.formatter(preciseNumber.value as number)
: preciseNumber.value.toString()
})
const plusDisabled = computed(() => {
return !isNullOrNaN(currentValue.value) && toNumber(currentValue.value) >= props.max
})
const minusDisabled = computed(() => {
return !isNullOrNaN(currentValue.value) && toNumber(currentValue.value) <= props.min
})
const hasValue = computed(() => !!(currentValue.value || currentValue.value === 0))
const showClear = computed(() => {
return !props.disabled && props.clearable && isHover.value && hasValue.value
Expand All @@ -274,7 +267,9 @@ export default defineComponent({
watch(
() => props.value,
value => {
currentValue.value = isNull(value) ? NaN : value
if (!focused.value || !numberRE.test(String(currentValue.value))) {
currentValue.value = isNull(value) ? NaN : value
}
}
)
Expand All @@ -301,10 +296,6 @@ export default defineComponent({
}
function plusNumber(event: MouseEvent) {
if (plusDisabled.value) {
return
}
!focused.value && focus()
changeStep(
'plus',
Expand All @@ -313,10 +304,6 @@ export default defineComponent({
}
function minusNumber(event: MouseEvent) {
if (minusDisabled.value) {
return
}
!focused.value && focus()
changeStep(
'minus',
Expand Down Expand Up @@ -367,7 +354,6 @@ export default defineComponent({
let value = stringValue.trim()
debugger
if (type === 'change' && stringValue && !numberRE.test(stringValue)) {
const floatValue = parseFloat(stringValue)
Expand All @@ -387,9 +373,7 @@ export default defineComponent({
function setValue(value: string | number, type: InputEventType) {
if (type !== 'input') {
currentValue.value = isEmpty(value)
? NaN
: boundRange(toNumber(value), props.min, props.max)
currentValue.value = isEmpty(value) ? NaN : toNumber(value)
} else {
currentValue.value = value
}
Expand All @@ -409,20 +393,27 @@ export default defineComponent({
}
function emitChangeEvent(type: InputEventType) {
const value = isEmpty(currentValue.value) ? getEmptyValue() : toNumber(currentValue.value)
const empty = isEmpty(currentValue.value)
const value = empty ? getEmptyValue() : toNumber(currentValue.value)
type = type === 'input' ? 'input' : 'change'
if (type === 'change') {
if (lastValue === value) return
const boundValue = empty ? value : boundRange(toNumber(value), props.min, props.max)
const boundChange = boundValue !== value
lastValue = value
if (!empty) {
currentValue.value = boundValue
}
!props.sync && setFieldValue(value)
emitEvent(props.onChange, value)
if (!props.sync && lastValue === boundValue) return
if (!props.sync) {
emit('update:value', value)
lastValue = boundValue
;(!props.sync || boundChange) && setFieldValue(boundValue)
emitEvent(props.onChange, boundValue)
if (!props.sync || boundChange) {
emit('update:value', boundValue)
validateField()
}
} else {
Expand Down Expand Up @@ -481,8 +472,6 @@ export default defineComponent({
inputStyle,
preciseNumber,
formattedValue,
plusDisabled,
minusDisabled,
hasValue,
showClear,
inputValue,
Expand Down
3 changes: 2 additions & 1 deletion docs/demos/number-input/range/demo.en-US.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
v-model:value="value"
:min="0"
:max="10"
style="max-width: 300px;"
style="max-width: 300px"
></NumberInput>
<p>Number Input Value: {{ value }}</p>
</template>

<script setup lang="ts">
Expand Down
3 changes: 2 additions & 1 deletion docs/demos/number-input/range/demo.zh-CN.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
v-model:value="value"
:min="0"
:max="10"
style="max-width: 300px;"
style="max-width: 300px"
></NumberInput>
<p>Number Input Value: {{ value }}</p>
</template>

<script setup lang="ts">
Expand Down
6 changes: 4 additions & 2 deletions docs/demos/number-input/sync/demo.en-US.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<NumberInput
v-model:value="value"
sync
clearable=""
style="max-width: 300px;"
clearable
:min="-10"
:max="10"
style="max-width: 300px"
></NumberInput>
<p>Number Input Value: {{ value }}</p>
</template>
Expand Down
6 changes: 4 additions & 2 deletions docs/demos/number-input/sync/demo.zh-CN.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<NumberInput
v-model:value="value"
sync
clearable=""
style="max-width: 300px;"
clearable
:min="-10"
:max="10"
style="max-width: 300px"
></NumberInput>
<p>Number Input Value: {{ value }}</p>
</template>
Expand Down

0 comments on commit 9c77dc3

Please sign in to comment.