diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index d6d5560b4..791b6e957 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -7,7 +7,7 @@ ### Fixes - Fix `n-time-picker`'s `use-12-hours` type error warning, closes [#4308](https://github.com/tusen-ai/naive-ui/issues/4308) -- Fix `input-number` the problem that the negative sign is replaced when the negative sign is entered +- Fix `input-number` the problem that the negative sign is replaced when the negative sign is entered. - Fix `n-data-table`'s header will show scrollbar in some old browsers, closes [#6557](https://github.com/tusen-ai/naive-ui/issues/6557). ### Features diff --git a/src/input-number/src/utils.ts b/src/input-number/src/utils.ts index 6ee4bf87a..91b825458 100644 --- a/src/input-number/src/utils.ts +++ b/src/input-number/src/utils.ts @@ -11,13 +11,17 @@ export function parse(value: string): number | null { return Number(value) } -// can be parsed to number but shouldn't be applied when inputing -// when value includes `.`, ending with 0 and`.`, doesn't update, if 0 parse func will remove 0 -// allow negative sign +// This function is created for `update-value-on-input` prop. When the prop is +// true, the input value will update the value and 's value at the same +// time. So we need to make user's content won't be replaced by its parsed value +// in some certain cases. For example '0.' should be parsed and replaced by '0', +// '-0' should be parsed and replaced by '0', since user may input '-0.1' after. export function isWipValue(value: string): boolean { return ( - (value.includes('.') && /^-?\d*\.?\d*$/.test(value)) - || /^-?\d*$/.test(value) + (value.includes('.') + && (/^(-)?\d+.*(\.|0)$/.test(value) || /^(-)?\.\d+$/.test(value))) + || value === '-' + || value === '-0' ) }