-
Notifications
You must be signed in to change notification settings - Fork 19
Use NumberFields in more places; enhance NumberField component #1926
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7376f7
Use NumberFields in more places; enhance NumberField component
charliepark e3dba4e
Update tests
charliepark cba5e66
Remove 'type' option from TextField; refactor onChange logic
charliepark ecbee11
Apply default minimum of 0 on NumberFields
charliepark f522cf7
Merge branch 'main' into number-fields-with-boundaries
charliepark f624c15
Allow for the possibility of password types in TextFields
charliepark 5c2fdfd
More explicit test querying
charliepark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ export interface TextFieldProps< | |
| > extends UITextFieldProps { | ||
| name: TName | ||
| /** HTML type attribute, defaults to text */ | ||
| type?: string | ||
charliepark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type?: 'text' | 'password' | ||
| /** Will default to name if not provided */ | ||
| label?: string | ||
| /** | ||
|
|
@@ -92,17 +92,9 @@ export function TextField< | |
| ) | ||
| } | ||
|
|
||
| function numberToInputValue(value: number) { | ||
| // could add `|| value === 0`, but that means when the value is 0, we always | ||
| // show an empty string, which is weird, and doubly bad because then the | ||
| // browser apparently fails to validate it against minimum (if one is | ||
| // provided). I found it let me submit instance create with 0 CPUs. | ||
| return isNaN(value) ? '' : value.toString() | ||
| } | ||
|
|
||
| /** | ||
| * Primarily exists for `TextField`, but we occasionally also need a plain field | ||
| * without a label on it. Note special handling of `type="number"`. | ||
| * without a label on it. | ||
| * | ||
| * Note that `id` is an allowed prop, unlike in `TextField`, where it is always | ||
| * generated from `name`. This is because we need to pass the generated ID in | ||
|
|
@@ -131,7 +123,7 @@ export const TextFieldInner = < | |
| name={name} | ||
| control={control} | ||
| rules={{ required, validate }} | ||
| render={({ field: { onChange, value, ...fieldRest }, fieldState: { error } }) => { | ||
| render={({ field: { onChange, ...fieldRest }, fieldState: { error } }) => { | ||
| return ( | ||
| <> | ||
| <UITextField | ||
|
|
@@ -143,32 +135,9 @@ export const TextFieldInner = < | |
| [`${id}-help-text`]: !!tooltipText, | ||
| })} | ||
| aria-describedby={tooltipText ? `${id}-label-tip` : undefined} | ||
| // note special handling for number fields, which produce a number | ||
| // for the calling code despite the actual input value necessarily | ||
| // being a string. | ||
| onChange={(e) => { | ||
| if (transform) { | ||
| onChange(transform(e.target.value)) | ||
| return | ||
| } | ||
| if (type === 'number') { | ||
| if (e.target.value.trim() === '') { | ||
| onChange(0) | ||
| } else if (!isNaN(e.target.valueAsNumber)) { | ||
| onChange(e.target.valueAsNumber) | ||
| } | ||
| // otherwise ignore the input. this means, for example, typing | ||
| // letters does nothing. If we instead said take anything | ||
| // that's NaN and fall back to 0, typing a letter would reset | ||
| // the field to 0, which is silly. Browsers are supposed to | ||
| // ignore non-numeric input for you anyway, but Firefox does | ||
| // not. | ||
| return | ||
| } | ||
|
|
||
| onChange(e.target.value) | ||
| onChange(transform ? transform(e.target.value) : e.target.value) | ||
| }} | ||
| value={type === 'number' ? numberToInputValue(value) : value} | ||
|
Comment on lines
137
to
-171
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, I love to see it. Thank you @charliepark, this soothes my weary soul. |
||
| {...fieldRest} | ||
| {...props} | ||
| /> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the
min !== undefinedconstruct is necessary because ifminis0,min ? __ : __evaluates to false andminValueis set asundefined