Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Input): 增加数字输入框增量设置和超出警告功能 (#748)
- Loading branch information
Showing
3 changed files
with
106 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,61 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import React, { useState, useMemo } from 'react'; | ||
import Input, { InputProps } from './'; | ||
|
||
interface InputNumberProps extends InputProps { | ||
min?: number; | ||
max?: number; | ||
step?: number; | ||
overLimitColor?: string; | ||
formatter?: (value: number) => string; | ||
parser?: (value: number) => number; | ||
keyboard?: boolean; | ||
} | ||
|
||
export default React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => { | ||
const { min, max, ...inputProps } = props; | ||
const { | ||
className, | ||
min, | ||
max, | ||
step, | ||
overLimitColor, | ||
keyboard = false, | ||
formatter, | ||
prefixCls = 'w-input-number', | ||
...otherProps | ||
} = props; | ||
|
||
const [value, valueSet] = useState(props.value || 0); | ||
const value = useMemo(() => Number.parseFloat((props.value || 0)?.toString()), [props.value]); | ||
const [isOverLimit, isOverLimitSet] = useState(overLimitComp(value)); | ||
|
||
function overLimitComp(value: number) { | ||
if (typeof min === 'number' && value < min) return true; | ||
if (typeof max === 'number' && value > max) return true; | ||
return false; | ||
} | ||
|
||
const onChange = (v: React.ChangeEvent<HTMLInputElement>) => { | ||
const parseValue = Number.parseInt(v.target.value); | ||
if (typeof min === 'number' && parseValue < min) return; | ||
if (typeof max === 'number' && parseValue > max) return; | ||
const isOverLimit = overLimitComp(Number.parseFloat(v.target.value)); | ||
isOverLimitSet(isOverLimit); | ||
|
||
valueSet(v.target.value); | ||
props.onChange?.(v); | ||
}; | ||
|
||
return <Input {...inputProps} value={value} onChange={onChange} type="number" />; | ||
const overLimitProps = useMemo(() => { | ||
if (!overLimitColor) return { min, max }; | ||
}, []); | ||
|
||
const cls = [prefixCls, className].filter(Boolean).join(' ').trim(); | ||
const inputStyle = useMemo(() => (isOverLimit ? { color: overLimitColor?.toString() } : undefined), [isOverLimit]); | ||
|
||
return ( | ||
<Input | ||
{...otherProps} | ||
className={cls} | ||
type="number" | ||
inputStyle={inputStyle} | ||
onChange={onChange} | ||
step={step} | ||
{...overLimitProps} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters