-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(VNumberInput): clearing v-number-input with backspace resets to 0 #20729
fix(VNumberInput): clearing v-number-input with backspace resets to 0 #20729
Conversation
…kspace-resets-to-0
@@ -75,13 +75,14 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({ | |||
const model = computed({ | |||
get: () => _model.value, | |||
set (val) { | |||
if (val === null) { | |||
_model.value = null | |||
if (val === null || (typeof val === 'string' && !val)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing number via backspace currently results in warning:
[Vue warn]: Invalid prop: type check failed for prop "modelValue". Expected Number with value 0, got String with value "".
at <VNumberInput ref="input" model-value="" rules= (3) [ƒ, ƒ, ƒ] ... >
at <VContainer>
at <VApp>
at <Playground>
at <App>
modelValue
here should be strictly enforced with type number | null
, probably try to set _model.value
to null
when backspace to empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @yuwu9145, thanks for the review!
As I described in the PR description - the useValidation composable doesn't perform validation when the input value changes to null. When resetting the input, it sets the value to an empty string. Instead of assigning 0, the model should be reset to match this behavior. Using null for resetting is not viable because validation won't be triggered. Therefore, the model value should remain as the empty string in such cases to ensure proper validation behavior.
I think it might be worth of refactoring to extend the model type with string. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still believe model value should be set to null
in number input.
As for the issue of null
doesn't trigger validation, it's a separate issue that should be resolved in validation composable demo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should aim to make number input work consistently with text field in terms of validation, but model value type is kept with number | null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens because of this condition in useValidation
composable, but it was added over two years ago and I assume it can affect many places if we change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's not a concern in VNumberInput and validation issue should be carefully resolved/tested in #20765
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we can close this PR or just solve the first part of the issue regarding resetting the value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need to resolve the issue regarding resetting the value to null, and ignore validation.
I have pushed a commit, see if you had any comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuwu9145 it's fine for me, thanks for your participation and prompt reply! :)
@@ -294,9 +297,9 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({ | |||
|
|||
{ incrementControlNode() } | |||
</div> | |||
) : (!props.reverse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change just a code format? Or was there any logically concrete reason behind it? @EvgenyWas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only code format to remove the exclamation mark. I believe it's easier to read especially when there is the same condition with another case.
Description
The
useValidation
composable doesn't perform validation when the input value changes tonull
. When resetting the input, it sets the value to an empty string. Instead of assigning 0, the model should be reset to match this behavior. Usingnull
for resetting is not viable because validation won't be triggered. Therefore, the model value should remain as the empty string in such cases to ensure proper validation behavior.fixes #20607
Markup: