-
Notifications
You must be signed in to change notification settings - Fork 19
Number input #1582
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
Number input #1582
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
749ba48
Number input first pass
benjaminleonard 638baa6
Merge branch 'main' into number-input
benjaminleonard 28777a9
Add number field
benjaminleonard ef6722b
Update libs/ui/lib/number-input/NumberInput.stories.tsx
benjaminleonard 8db39a6
Update libs/ui/lib/number-input/NumberInput.tsx
benjaminleonard 9faad6b
Bot commit: format with prettier
github-actions[bot] 135783b
fix defaultValue and onChange (mostly), only use number input on numb…
david-crespo a185a0b
actually fix the type error by getting rid of the spread
david-crespo db932fc
props cleanup
david-crespo 8e05496
update e2es
david-crespo 831616a
fix date picker state type error, use isInvalid (validationState depr…
david-crespo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import cn from 'classnames' | ||
| import { useId } from 'react' | ||
| import type { FieldPathByValue, FieldValues } from 'react-hook-form' | ||
| import { Controller } from 'react-hook-form' | ||
|
|
||
| import { FieldLabel, TextInputHint, NumberInput as UINumberField } from '@oxide/ui' | ||
| import { capitalize } from '@oxide/util' | ||
|
|
||
| import { ErrorMessage } from './ErrorMessage' | ||
| import type { TextFieldProps } from './TextField' | ||
|
|
||
| export function NumberField< | ||
| TFieldValues extends FieldValues, | ||
| // can only be used on fields with number values | ||
| TName extends FieldPathByValue<TFieldValues, number> | ||
| >({ | ||
| name, | ||
| label = capitalize(name), | ||
| units, | ||
| description, | ||
| helpText, | ||
| required, | ||
| ...props | ||
| }: Omit<TextFieldProps<TFieldValues, TName>, 'id'>) { | ||
| // id is omitted from props because we generate it here | ||
| const id = useId() | ||
| return ( | ||
| <div className="max-w-lg"> | ||
| <div className="mb-2"> | ||
| <FieldLabel id={`${id}-label`} tip={description} optional={!required}> | ||
| {label} {units && <span className="ml-1 text-secondary">({units})</span>} | ||
| </FieldLabel> | ||
| {helpText && ( | ||
| <TextInputHint id={`${id}-help-text`} className="mb-2"> | ||
| {helpText} | ||
| </TextInputHint> | ||
| )} | ||
| </div> | ||
| {/* passing the generated id is very important for a11y */} | ||
| <NumberFieldInner name={name} {...props} id={id} /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Primarily exists for `NumberField`, but we occasionally also need a plain field | ||
| * 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 | ||
| * from there to here. For the case where `TextFieldInner` is used | ||
| * independently, we also generate an ID for use only if none is passed in. | ||
| */ | ||
| export const NumberFieldInner = < | ||
| TFieldValues extends FieldValues, | ||
| TName extends FieldPathByValue<TFieldValues, number> | ||
| >({ | ||
| name, | ||
| label = capitalize(name), | ||
| validate, | ||
| control, | ||
| description, | ||
| required, | ||
| id: idProp, | ||
| }: TextFieldProps<TFieldValues, TName>) => { | ||
| const generatedId = useId() | ||
| const id = idProp || generatedId | ||
|
|
||
| return ( | ||
| <Controller | ||
| name={name} | ||
| control={control} | ||
| rules={{ required, validate }} | ||
| render={({ field: { value, ...fieldRest }, fieldState: { error } }) => { | ||
| return ( | ||
| <> | ||
| <UINumberField | ||
| id={id} | ||
| error={!!error} | ||
| aria-labelledby={cn(`${id}-label`, { | ||
| [`${id}-help-text`]: !!description, | ||
| })} | ||
| aria-describedby={description ? `${id}-label-tip` : undefined} | ||
| defaultValue={value} | ||
| {...fieldRest} | ||
| /> | ||
| <ErrorMessage error={error} label={label} /> | ||
| </> | ||
| ) | ||
| }} | ||
| /> | ||
| ) | ||
| } |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { NumberInput } from './NumberInput' | ||
|
|
||
| export const Default = () => ( | ||
| <div className="max-w-lg"> | ||
| <NumberInput defaultValue={6} /> | ||
| </div> | ||
| ) | ||
|
|
||
| export const WithUnit = () => ( | ||
| <div className="max-w-lg"> | ||
| <NumberInput | ||
| defaultValue={6} | ||
| formatOptions={{ | ||
| style: 'unit', | ||
| unit: 'inch', | ||
| unitDisplay: 'long', | ||
| }} | ||
| /> | ||
| </div> | ||
| ) | ||
|
|
||
| export const StepValues = () => ( | ||
| <div className="max-w-lg space-y-4 text-sans-md children:space-y-2"> | ||
| <div> | ||
| <div>Step</div> | ||
| <NumberInput step={10} /> | ||
| </div> | ||
| <div> | ||
| <div>Step + minValue</div> | ||
| <NumberInput minValue={2} step={2} /> | ||
| </div> | ||
| <div> | ||
| <div>Step + minValue + maxValue</div> | ||
| <NumberInput minValue={2} maxValue={20} step={2} /> | ||
| </div> | ||
| </div> | ||
| ) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.