mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 01:18:05 +08:00
fix: ensure default values are handled correctly in InputNumber and related components
This commit is contained in:
@ -18,7 +18,7 @@ const NumberInputField = ({
|
|||||||
className,
|
className,
|
||||||
...inputProps
|
...inputProps
|
||||||
}: TextFieldProps) => {
|
}: TextFieldProps) => {
|
||||||
const field = useFieldContext<number | undefined>()
|
const field = useFieldContext<number>()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('flex flex-col gap-y-0.5', className)}>
|
<div className={cn('flex flex-col gap-y-0.5', className)}>
|
||||||
|
|||||||
@ -47,7 +47,7 @@ export const generateZodSchema = (fields: BaseConfiguration[]) => {
|
|||||||
zodType = (zodType as ZodString).nonempty(`${field.label} is required`)
|
zodType = (zodType as ZodString).nonempty(`${field.label} is required`)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
zodType = zodType.optional()
|
zodType = zodType.optional().nullable()
|
||||||
}
|
}
|
||||||
|
|
||||||
shape[field.variable] = zodType
|
shape[field.variable] = zodType
|
||||||
|
|||||||
@ -63,11 +63,11 @@ describe('InputNumber Component', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('handles empty input', () => {
|
it('handles empty input', () => {
|
||||||
render(<InputNumber {...defaultProps} value={0} />)
|
render(<InputNumber {...defaultProps} value={1} />)
|
||||||
const input = screen.getByRole('spinbutton')
|
const input = screen.getByRole('spinbutton')
|
||||||
|
|
||||||
fireEvent.change(input, { target: { value: '' } })
|
fireEvent.change(input, { target: { value: '' } })
|
||||||
expect(defaultProps.onChange).toHaveBeenCalledWith(undefined)
|
expect(defaultProps.onChange).toHaveBeenCalledWith(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('handles invalid input', () => {
|
it('handles invalid input', () => {
|
||||||
@ -75,7 +75,7 @@ describe('InputNumber Component', () => {
|
|||||||
const input = screen.getByRole('spinbutton')
|
const input = screen.getByRole('spinbutton')
|
||||||
|
|
||||||
fireEvent.change(input, { target: { value: 'abc' } })
|
fireEvent.change(input, { target: { value: 'abc' } })
|
||||||
expect(defaultProps.onChange).not.toHaveBeenCalled()
|
expect(defaultProps.onChange).toHaveBeenCalledWith(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('displays unit when provided', () => {
|
it('displays unit when provided', () => {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import classNames from '@/utils/classnames'
|
|||||||
export type InputNumberProps = {
|
export type InputNumberProps = {
|
||||||
unit?: string
|
unit?: string
|
||||||
value?: number
|
value?: number
|
||||||
onChange: (value?: number) => void
|
onChange: (value: number) => void
|
||||||
amount?: number
|
amount?: number
|
||||||
size?: 'regular' | 'large'
|
size?: 'regular' | 'large'
|
||||||
max?: number
|
max?: number
|
||||||
@ -46,7 +46,7 @@ export const InputNumber: FC<InputNumberProps> = (props) => {
|
|||||||
if (disabled) return
|
if (disabled) return
|
||||||
|
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
onChange(defaultValue)
|
onChange(defaultValue ?? 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const newValue = value + amount
|
const newValue = value + amount
|
||||||
@ -58,7 +58,7 @@ export const InputNumber: FC<InputNumberProps> = (props) => {
|
|||||||
if (disabled) return
|
if (disabled) return
|
||||||
|
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
onChange(defaultValue)
|
onChange(defaultValue ?? 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const newValue = value - amount
|
const newValue = value - amount
|
||||||
@ -69,7 +69,7 @@ export const InputNumber: FC<InputNumberProps> = (props) => {
|
|||||||
|
|
||||||
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
if (e.target.value === '') {
|
if (e.target.value === '') {
|
||||||
onChange(undefined)
|
onChange(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const parsed = Number(e.target.value)
|
const parsed = Number(e.target.value)
|
||||||
@ -85,8 +85,8 @@ export const InputNumber: FC<InputNumberProps> = (props) => {
|
|||||||
<Input {...rest}
|
<Input {...rest}
|
||||||
// disable default controller
|
// disable default controller
|
||||||
type='number'
|
type='number'
|
||||||
className={classNames('no-spinner rounded-r-none', className)}
|
className={classNames('rounded-r-none', className)}
|
||||||
value={value}
|
value={value ?? 0}
|
||||||
max={max}
|
max={max}
|
||||||
min={min}
|
min={min}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ export const useInitialData = (variables: RAGPipelineVariables, lastRunInputData
|
|||||||
if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
|
if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
|
||||||
acc[variableName] = defaultValue ?? ''
|
acc[variableName] = defaultValue ?? ''
|
||||||
if (type === BaseFieldType.numberInput)
|
if (type === BaseFieldType.numberInput)
|
||||||
acc[variableName] = defaultValue
|
acc[variableName] = defaultValue ?? 0
|
||||||
if (type === BaseFieldType.checkbox)
|
if (type === BaseFieldType.checkbox)
|
||||||
acc[variableName] = defaultValue ?? false
|
acc[variableName] = defaultValue ?? false
|
||||||
if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
|
if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
|
||||||
|
|||||||
Reference in New Issue
Block a user