Skip to content

Commit

Permalink
fix defaultValue and onChange (mostly), only use number input on numb…
Browse files Browse the repository at this point in the history
…er fields
  • Loading branch information
david-crespo committed Oct 5, 2023
1 parent 9faad6b commit aac3c51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/components/form/fields/DiskSizeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Copyright Oxide Computer Company
*/
import type { FieldPath, FieldValues } from 'react-hook-form'
import type { FieldPath, FieldPathByValue, FieldValues } from 'react-hook-form'

import { MAX_DISK_SIZE_GiB } from '@oxide/api'

Expand All @@ -21,7 +21,7 @@ interface DiskSizeProps<

export function DiskSizeField<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>
TName extends FieldPathByValue<TFieldValues, number>
>({ required = true, name, minSize = 1, ...props }: DiskSizeProps<TFieldValues, TName>) {
return (
<NumberField
Expand Down
10 changes: 7 additions & 3 deletions app/components/form/fields/NumberField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import cn from 'classnames'
import { useId } from 'react'
import type { FieldPath, FieldValues } from 'react-hook-form'
import type { FieldPathByValue, FieldValues } from 'react-hook-form'
import { Controller } from 'react-hook-form'

import type { NumberInputProps as UINumberFieldProps } from '@oxide/ui'
Expand All @@ -19,7 +19,8 @@ import type { TextFieldProps } from './TextField'

export function NumberField<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>
// can only be used on fields with number values
TName extends FieldPathByValue<TFieldValues, number>
>({
name,
label = capitalize(name),
Expand Down Expand Up @@ -60,7 +61,7 @@ export function NumberField<
*/
export const NumberFieldInner = <
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>
TName extends FieldPathByValue<TFieldValues, number>
>({
name,
label = capitalize(name),
Expand All @@ -69,6 +70,9 @@ export const NumberFieldInner = <
description,
required,
id: idProp,
// only taking these out of props because spreading them in messes up the types
defaultValue: _defaultValue,
onChange: _onChange,
...props
}: TextFieldProps<TFieldValues, TName> & UINumberFieldProps) => {
const generatedId = useId()
Expand Down

0 comments on commit aac3c51

Please sign in to comment.