NumberInput component improvements #970
Labels
type: enhancement
New feature or request
work: frontend
Related to frontend code in the mathesar_ui directory
Milestone
Problem
The NumberInput component implemented as part of #915 is a low level component with few practical uses. The basic version was implemented inorder to support the form-builder component. Inorder to make it usable, it requires a number of improvements.
Proposed solution
The following contents are taken from @seancolsen's comments on the form-builder PR
Fix
NumberInput
settingvalue
to stringThe two-way binding for
value
should reliably produce a type ofnumber | undefined
.Case 1
NumberInput
component with a boundvalue
equal to7
.8
.value
to be78
(number).value
to be"78"
(string).Case 2
NumberInput
component with a boundvalue
equal to7
.value
to beundefined
.value
to be""
(empty string).Furthermore (and on a related note), I'm a bit confused about what's happening with
interface HTMLNumberInputElement
, and my hunch is that we should remove it.My understanding is that the
value
property of an<input>
will always be a string, even when settingtype='number'
. That means that writinge.target as HTMLNumberInputElement
(inNumberInput.svelte
) is incorrectly providing type assurance thatvalue
will be a number.I would expect
NumberInput
to assume that the underlying DOM element will have a string value, and to be converting that string to a number (i.e. withNumber(inputElement.value)
) to produce a binding to thevalue
prop that satisfiesnumber | undefined
.More gracefully handle some edge cases in
NumberInput
Independent sub-issues, with steps to reproduce.
Support pasting multi-character values
1
22
22
1
Adding a
+
after the group invalidKeyRegex
appear to fix this.Support entering
-0.1
-0
and observe-0
which is good.
-0.
0
Allow empty integer
1
andisInteger
Backspace
once0
Don't wipe contents when entering
7..
7.
and observe7.
which is good.
7.
Retain cursor position
78
7
and8
a
78
with cursor between7
and8
78
with cursor after8
Don't auto-delete decimal
1.2
2
1.
1
Don't auto-delete minus sign
-8
8
-
Don't truncate integer value when entering decimal midway through number
23
andisInteger
2
and3
.
23
2
Remove support for numbers like
8e9
, or allow users to enter them. I can enter89
, then place my cursor between the two digits and typee
. But I can't enter8e9
directly. I don't think we need support entering these kinds of numbers into the input though.Additional thoughts
beforeinput
event instead of theinput
event.Add tests for
NumberInput
This is the kind of component that really benefits from having some tests in my opinion.
We could pretty easily write tests for the scenarios I've laid out above, as well as other ones that already work correctly.
Remove
type='number'
onNumberInput
I hate the browser native number input, and I think most people do too.
I'd prefer that we remove
Considerations:
decimal
ornumeric
, potentially with some logic based on the props.onInput
function to account for the different behavior ofinputElement.value
when the user enters a non-numeric string likex
into the input.The text was updated successfully, but these errors were encountered: