From 59f68cd63b01a8447bbea00d0ae8aec79eba4df8 Mon Sep 17 00:00:00 2001 From: twwu Date: Thu, 17 Jul 2025 18:15:52 +0800 Subject: [PATCH] fix: ensure default values are handled correctly in InputNumber and related components --- .../base/form/components/field/number-input.tsx | 2 +- .../base/form/form-scenarios/base/utils.ts | 2 +- web/app/components/base/input-number/index.spec.tsx | 6 +++--- web/app/components/base/input-number/index.tsx | 12 ++++++------ .../rag-pipeline/hooks/use-input-fields.ts | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/web/app/components/base/form/components/field/number-input.tsx b/web/app/components/base/form/components/field/number-input.tsx index 13f28e97db..46d2ced8b6 100644 --- a/web/app/components/base/form/components/field/number-input.tsx +++ b/web/app/components/base/form/components/field/number-input.tsx @@ -18,7 +18,7 @@ const NumberInputField = ({ className, ...inputProps }: TextFieldProps) => { - const field = useFieldContext() + const field = useFieldContext() return (
diff --git a/web/app/components/base/form/form-scenarios/base/utils.ts b/web/app/components/base/form/form-scenarios/base/utils.ts index 97a10c316b..39755bcf5e 100644 --- a/web/app/components/base/form/form-scenarios/base/utils.ts +++ b/web/app/components/base/form/form-scenarios/base/utils.ts @@ -47,7 +47,7 @@ export const generateZodSchema = (fields: BaseConfiguration[]) => { zodType = (zodType as ZodString).nonempty(`${field.label} is required`) } else { - zodType = zodType.optional() + zodType = zodType.optional().nullable() } shape[field.variable] = zodType diff --git a/web/app/components/base/input-number/index.spec.tsx b/web/app/components/base/input-number/index.spec.tsx index 891cbd21e3..75d19ecb6e 100644 --- a/web/app/components/base/input-number/index.spec.tsx +++ b/web/app/components/base/input-number/index.spec.tsx @@ -63,11 +63,11 @@ describe('InputNumber Component', () => { }) it('handles empty input', () => { - render() + render() const input = screen.getByRole('spinbutton') fireEvent.change(input, { target: { value: '' } }) - expect(defaultProps.onChange).toHaveBeenCalledWith(undefined) + expect(defaultProps.onChange).toHaveBeenCalledWith(0) }) it('handles invalid input', () => { @@ -75,7 +75,7 @@ describe('InputNumber Component', () => { const input = screen.getByRole('spinbutton') fireEvent.change(input, { target: { value: 'abc' } }) - expect(defaultProps.onChange).not.toHaveBeenCalled() + expect(defaultProps.onChange).toHaveBeenCalledWith(0) }) it('displays unit when provided', () => { diff --git a/web/app/components/base/input-number/index.tsx b/web/app/components/base/input-number/index.tsx index 4282f18800..286d664d55 100644 --- a/web/app/components/base/input-number/index.tsx +++ b/web/app/components/base/input-number/index.tsx @@ -6,7 +6,7 @@ import classNames from '@/utils/classnames' export type InputNumberProps = { unit?: string value?: number - onChange: (value?: number) => void + onChange: (value: number) => void amount?: number size?: 'regular' | 'large' max?: number @@ -46,7 +46,7 @@ export const InputNumber: FC = (props) => { if (disabled) return if (value === undefined) { - onChange(defaultValue) + onChange(defaultValue ?? 0) return } const newValue = value + amount @@ -58,7 +58,7 @@ export const InputNumber: FC = (props) => { if (disabled) return if (value === undefined) { - onChange(defaultValue) + onChange(defaultValue ?? 0) return } const newValue = value - amount @@ -69,7 +69,7 @@ export const InputNumber: FC = (props) => { const handleInputChange = useCallback((e: React.ChangeEvent) => { if (e.target.value === '') { - onChange(undefined) + onChange(0) return } const parsed = Number(e.target.value) @@ -85,8 +85,8 @@ export const InputNumber: FC = (props) => {