refactor: input number component

feat/parent-child-retrieval-api
AkaraChen 1 year ago
parent b7d9987953
commit 1843f6ccb6

@ -1,4 +1,3 @@
import { useState } from 'react'
import type { FC, SetStateAction } from 'react' import type { FC, SetStateAction } from 'react'
import { RiArrowDownSLine, RiArrowUpSLine } from '@remixicon/react' import { RiArrowDownSLine, RiArrowUpSLine } from '@remixicon/react'
import Input, { type InputProps } from '../input' import Input, { type InputProps } from '../input'
@ -6,35 +5,34 @@ import classNames from '@/utils/classnames'
export type InputNumberProps = { export type InputNumberProps = {
unit?: string unit?: string
value: number
onChange: (value: number) => void onChange: (value: number) => void
amount?: number amount?: number
size?: 'sm' | 'md' size?: 'sm' | 'md'
} & Omit<InputProps, 'value' | 'onChange' | 'size'> } & Omit<InputProps, 'value' | 'onChange' | 'size'>
export const InputNumber: FC<InputNumberProps> = (props) => { export const InputNumber: FC<InputNumberProps> = (props) => {
const { unit, className, onChange, defaultValue = 0, amount = 1, size = 'sm', max, min, ...rest } = props const { unit, className, onChange, amount = 1, value, size = 'sm', max, min, ...rest } = props
const [val, setVal] = useState<number>(defaultValue as number) const update = (input: SetStateAction<number>) => {
const update = (value: SetStateAction<number>) => { const current = typeof input === 'function' ? input(value) : input as number
const current = typeof value === 'function' ? value(val) : value as number
if (max && current >= (max as number)) if (max && current >= (max as number))
return return
if (min && current <= (min as number)) if (min && current <= (min as number))
return return
setVal(value) onChange(current)
} }
const inc = () => update(val => val + amount) const inc = () => update(val => val + amount)
const dec = () => update(val => val - amount) const dec = () => update(val => val - amount)
return <div className='flex'> return <div className='flex'>
<Input {...rest} <Input {...rest}
className={classNames('rounded-r-none', className)} className={classNames('rounded-r-none', className)}
value={val} value={value}
max={max} max={max}
min={min} min={min}
onChange={(e) => { onChange={(e) => {
const parsed = Number(e.target.value) const parsed = Number(e.target.value)
if (Number.isNaN(parsed)) if (Number.isNaN(parsed))
return return
setVal(parsed)
onChange(parsed) onChange(parsed)
}} }}
/> />

@ -55,10 +55,8 @@ const ParamItem: FC<Props> = ({ className, id, name, noTooltip, tip, step = 0.1,
max={max} max={max}
step={step} step={step}
size='sm' size='sm'
value={value}
onChange={(value) => { onChange={(value) => {
if (value < min || value > max)
return
onChange(id, value) onChange(id, value)
}} }}
/> />

@ -672,11 +672,11 @@ const StepTwo = ({
onChange={e => setSegmentIdentifier(e.target.value)} onChange={e => setSegmentIdentifier(e.target.value)}
/> />
<MaxLengthInput <MaxLengthInput
defaultValue={max} value={max}
onChange={setMax} onChange={setMax}
/> />
<OverlapInput <OverlapInput
defaultValue={overlap} value={overlap}
min={1} min={1}
onChange={setOverlap} onChange={setOverlap}
/> />
@ -749,7 +749,7 @@ const StepTwo = ({
})} })}
/> />
<MaxLengthInput <MaxLengthInput
defaultValue={parentChildConfig.parent.maxLength} value={parentChildConfig.parent.maxLength}
onChange={value => setParentChildConfig({ onChange={value => setParentChildConfig({
...parentChildConfig, ...parentChildConfig,
parent: { parent: {
@ -791,8 +791,7 @@ const StepTwo = ({
})} })}
/> />
<MaxLengthInput <MaxLengthInput
defaultValue={parentChildConfig.child.maxLength} value={parentChildConfig.child.maxLength}
onChange={value => setParentChildConfig({ onChange={value => setParentChildConfig({
...parentChildConfig, ...parentChildConfig,
child: { child: {

Loading…
Cancel
Save