From be52e785636ff7933bc4236abef317a77170c586 Mon Sep 17 00:00:00 2001 From: Pagebakers Date: Wed, 18 Oct 2023 17:26:47 +0200 Subject: [PATCH] fix: support placeholder in numberinput --- .changeset/tiny-seals-beam.md | 6 ++++++ packages/saas-ui-forms/src/default-fields.tsx | 12 ++++-------- .../src/number-input/number-input.tsx | 15 ++++++++++++++- packages/saas-ui-forms/stories/field.stories.tsx | 9 ++++++++- .../saas-ui-forms/tests/create-field.test.tsx | 9 +++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 .changeset/tiny-seals-beam.md diff --git a/.changeset/tiny-seals-beam.md b/.changeset/tiny-seals-beam.md new file mode 100644 index 000000000..036cc31c1 --- /dev/null +++ b/.changeset/tiny-seals-beam.md @@ -0,0 +1,6 @@ +--- +'@saas-ui/forms': patch +'@saas-ui/react': patch +--- + +Fixed issue where placeholder would not be passed to the NumberInputField diff --git a/packages/saas-ui-forms/src/default-fields.tsx b/packages/saas-ui-forms/src/default-fields.tsx index ac2d739cd..d297c0523 100644 --- a/packages/saas-ui-forms/src/default-fields.tsx +++ b/packages/saas-ui-forms/src/default-fields.tsx @@ -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 @@ -59,12 +57,10 @@ export const InputField = createField( }) ) -export interface NumberInputFieldProps extends NumberInputProps { - type: 'number' -} - -export const NumberInputField = createField( - NumberInput, +export const NumberInputField = createField( + forwardRef((props, ref) => { + return + }), { isControlled: true, } diff --git a/packages/saas-ui-forms/src/number-input/number-input.tsx b/packages/saas-ui-forms/src/number-input/number-input.tsx index 45355289d..e900351ed 100644 --- a/packages/saas-ui-forms/src/number-input/number-input.tsx +++ b/packages/saas-ui-forms/src/number-input/number-input.tsx @@ -8,6 +8,7 @@ import { NumberIncrementStepper, NumberDecrementStepper, NumberInputProps as ChakraNumberInputProps, + NumberInputFieldProps, } from '@chakra-ui/react' import { ChevronDownIcon, ChevronUpIcon } from '@saas-ui/core' @@ -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 @@ -36,12 +45,16 @@ export const NumberInput = forwardRef((props, ref) => { hideStepper, incrementIcon = , decrementIcon = , + placeholder, + fieldProps: _fieldProps, ...rest } = props + const fieldProps = { placeholder, ..._fieldProps } + return ( - + {!hideStepper && ( diff --git a/packages/saas-ui-forms/stories/field.stories.tsx b/packages/saas-ui-forms/stories/field.stories.tsx index c3aa72a00..04c2a02cb 100644 --- a/packages/saas-ui-forms/stories/field.stories.tsx +++ b/packages/saas-ui-forms/stories/field.stories.tsx @@ -59,7 +59,14 @@ export const Basic = () => ( {({ Field }) => ( - + { expectTypeOf<(typeof selectProps)['onChange']>().toEqualTypeOf< ((value: string | string[]) => void) | undefined @@ -53,4 +58,8 @@ test('should have correct event handler type on Field', async () => { expectTypeOf<(typeof inputProps)['onChange']>().toEqualTypeOf< React.ChangeEventHandler | undefined >() + + expectTypeOf<(typeof numberProps)['onChange']>().toEqualTypeOf< + ((valueAsString: string, valueAsNumber: number) => void) | undefined + >() })