Skip to content

Commit

Permalink
fix: support placeholder in numberinput
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagebakers committed Oct 18, 2023
1 parent 654ad18 commit be52e78
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/tiny-seals-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@saas-ui/forms': patch
'@saas-ui/react': patch
---

Fixed issue where placeholder would not be passed to the NumberInputField
12 changes: 4 additions & 8 deletions packages/saas-ui-forms/src/default-fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import {
} from './select'

import { createField } from './create-field'
import { ObjectField } from './object-field'
import { ArrayField } from './array-field'

export interface InputFieldProps extends InputProps {
type?: string
Expand All @@ -59,12 +57,10 @@ export const InputField = createField<InputFieldProps>(
})
)

export interface NumberInputFieldProps extends NumberInputProps {
type: 'number'
}

export const NumberInputField = createField<NumberInputFieldProps>(
NumberInput,
export const NumberInputField = createField<NumberInputProps>(
forwardRef((props, ref) => {
return <NumberInput ref={ref} {...props} />
}),
{
isControlled: true,
}
Expand Down
15 changes: 14 additions & 1 deletion packages/saas-ui-forms/src/number-input/number-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NumberIncrementStepper,
NumberDecrementStepper,
NumberInputProps as ChakraNumberInputProps,
NumberInputFieldProps,
} from '@chakra-ui/react'

import { ChevronDownIcon, ChevronUpIcon } from '@saas-ui/core'
Expand All @@ -25,6 +26,14 @@ interface NumberInputOptions {
* Render a custom decrement icon.
*/
decrementIcon?: React.ReactNode
/**
* The placeholder text when no value is selected.
*/
placeholder?: string
/**
* Props to pass to the NumberInputField component.
*/
fieldProps?: NumberInputFieldProps
}

export interface NumberInputProps
Expand All @@ -36,12 +45,16 @@ export const NumberInput = forwardRef<NumberInputProps, 'div'>((props, ref) => {
hideStepper,
incrementIcon = <ChevronUpIcon />,
decrementIcon = <ChevronDownIcon />,
placeholder,
fieldProps: _fieldProps,
...rest
} = props

const fieldProps = { placeholder, ..._fieldProps }

return (
<ChakraNumberInput {...rest} ref={ref}>
<NumberInputField />
<NumberInputField {...fieldProps} />

{!hideStepper && (
<NumberInputStepper>
Expand Down
9 changes: 8 additions & 1 deletion packages/saas-ui-forms/stories/field.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ export const Basic = () => (
{({ Field }) => (
<FormLayout>
<Field name="text" label="Text" type="text" />
<Field name="number" label="Number" type="text" min={1} max={10} />
<Field
name="number"
label="Number"
type="number"
min={1}
max={10}
placeholder="Number"
/>
<Field name="textarea" label="Textarea" type="textarea" />
<Field name="switch" label="Switch" type="switch" />
<Field
Expand Down
9 changes: 9 additions & 0 deletions packages/saas-ui-forms/tests/create-field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const inputProps: FieldProps = {
type: 'text',
}

const numberProps: FieldProps = {
name: 'test',
type: 'number',
}

test('should have correct event handler type on Field', async () => {
expectTypeOf<(typeof selectProps)['onChange']>().toEqualTypeOf<
((value: string | string[]) => void) | undefined
Expand All @@ -53,4 +58,8 @@ test('should have correct event handler type on Field', async () => {
expectTypeOf<(typeof inputProps)['onChange']>().toEqualTypeOf<
React.ChangeEventHandler<HTMLInputElement> | undefined
>()

expectTypeOf<(typeof numberProps)['onChange']>().toEqualTypeOf<
((valueAsString: string, valueAsNumber: number) => void) | undefined
>()
})

0 comments on commit be52e78

Please sign in to comment.