mirror of
https://github.com/langgenius/dify.git
synced 2026-03-26 22:52:38 +08:00
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Poojan <poojan@infocusp.com> Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com> Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com> Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com> Co-authored-by: User <user@example.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com> Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: tda <95275462+tda1017@users.noreply.github.com> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com> Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com> Co-authored-by: 99 <wh2099@pm.me> Co-authored-by: Br1an <932039080@qq.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: lif <1835304752@qq.com> Co-authored-by: weiguang li <codingpunk@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: Stable Genius <stablegenius043@gmail.com> Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com> Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
130 lines
3.4 KiB
TypeScript
130 lines
3.4 KiB
TypeScript
import type { FC } from 'react'
|
|
import type { InputProps } from '../input'
|
|
import { useCallback } from 'react'
|
|
import { cn } from '@/utils/classnames'
|
|
import Input from '../input'
|
|
|
|
export type InputNumberProps = {
|
|
unit?: string
|
|
value?: number
|
|
onChange: (value: number) => void
|
|
amount?: number
|
|
size?: 'regular' | 'large'
|
|
max?: number
|
|
min?: number
|
|
defaultValue?: number
|
|
disabled?: boolean
|
|
wrapClassName?: string
|
|
controlWrapClassName?: string
|
|
controlClassName?: string
|
|
} & Omit<InputProps, 'value' | 'onChange' | 'size' | 'min' | 'max' | 'defaultValue'>
|
|
|
|
export const InputNumber: FC<InputNumberProps> = (props) => {
|
|
const {
|
|
unit,
|
|
className,
|
|
onChange,
|
|
amount = 1,
|
|
value,
|
|
size = 'regular',
|
|
max,
|
|
min,
|
|
defaultValue,
|
|
wrapClassName,
|
|
controlWrapClassName,
|
|
controlClassName,
|
|
disabled,
|
|
...rest
|
|
} = props
|
|
|
|
const isValidValue = useCallback((v: number) => {
|
|
if (typeof max === 'number' && v > max)
|
|
return false
|
|
return !(typeof min === 'number' && v < min)
|
|
}, [max, min])
|
|
|
|
const inc = () => {
|
|
/* v8 ignore next 2 - @preserve */
|
|
if (disabled)
|
|
return
|
|
|
|
if (value === undefined) {
|
|
onChange(defaultValue ?? 0)
|
|
return
|
|
}
|
|
const newValue = value + amount
|
|
if (!isValidValue(newValue))
|
|
return
|
|
onChange(newValue)
|
|
}
|
|
const dec = () => {
|
|
/* v8 ignore next 2 - @preserve */
|
|
if (disabled)
|
|
return
|
|
|
|
if (value === undefined) {
|
|
onChange(defaultValue ?? 0)
|
|
return
|
|
}
|
|
const newValue = value - amount
|
|
if (!isValidValue(newValue))
|
|
return
|
|
onChange(newValue)
|
|
}
|
|
|
|
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
|
if (e.target.value === '') {
|
|
onChange(0)
|
|
return
|
|
}
|
|
const parsed = Number(e.target.value)
|
|
if (Number.isNaN(parsed))
|
|
return
|
|
|
|
if (!isValidValue(parsed))
|
|
return
|
|
onChange(parsed)
|
|
}, [isValidValue, onChange])
|
|
|
|
return (
|
|
<div data-testid="input-number-wrapper" className={cn('flex', wrapClassName)}>
|
|
<Input
|
|
{...rest}
|
|
// disable default controller
|
|
type="number"
|
|
className={cn('rounded-r-none no-spinner', className)}
|
|
value={value ?? 0}
|
|
max={max}
|
|
min={min}
|
|
disabled={disabled}
|
|
onChange={handleInputChange}
|
|
unit={unit}
|
|
size={size}
|
|
/>
|
|
<div
|
|
data-testid="input-number-controls"
|
|
className={cn('flex flex-col rounded-r-md border-l border-divider-subtle bg-components-input-bg-normal text-text-tertiary focus:shadow-xs', disabled && 'cursor-not-allowed opacity-50', controlWrapClassName)}
|
|
>
|
|
<button
|
|
type="button"
|
|
onClick={inc}
|
|
disabled={disabled}
|
|
aria-label="increment"
|
|
className={cn(size === 'regular' ? 'pt-1' : 'pt-1.5', 'px-1.5 hover:bg-components-input-bg-hover', disabled && 'cursor-not-allowed hover:bg-transparent', controlClassName)}
|
|
>
|
|
<span className="i-ri-arrow-up-s-line size-3" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={dec}
|
|
disabled={disabled}
|
|
aria-label="decrement"
|
|
className={cn(size === 'regular' ? 'pb-1' : 'pb-1.5', 'px-1.5 hover:bg-components-input-bg-hover', disabled && 'cursor-not-allowed hover:bg-transparent', controlClassName)}
|
|
>
|
|
<span className="i-ri-arrow-down-s-line size-3" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|