输入框组件文档如下:
源代码如下:
import React, { FC, useState, useMemo, memo } from 'react';
import { CloseOutlined, EyeOutlined, UpOutlined, DownOutlined } from '@ant-design/icons';
import style from './index.module.less';
interface InputProps {
width?: string;
moreStyle?: object;
type?: string;
placeholder?: string;
showClear?: boolean;
showTogglePwd?: boolean;
min?: number;
max?: number;
step?: number;
handleIptChange?: Function;
handleNumChange?: Function;
defaultValue?: string;
}
const Input: FC<InputProps> = (props) => {
const {
width,
moreStyle,
type,
placeholder,
showClear,
showTogglePwd,
min,
max,
step,
handleIptChange,
handleNumChange,
defaultValue,
} = props;
const [iptValue, setIptValue] = useState<string | number>(defaultValue || '');
const [pwdIptState, setPwdIptState] = useState(true);
const changeIpt = (e: any) => {
setIptValue(e.target.value);
if (handleIptChange) {
handleIptChange(e.target.value);
}
};
const blurIpt = (e: any) => {
if (type === 'num' && Number(iptValue) == NaN) {
setIptValue('');
}
};
const addNum = () => {
if (type === 'num' && Number(iptValue) == NaN) {
return setIptValue('');
}
const stepNum = step || 1;
if (step && max && Number(iptValue) + stepNum > max) {
handleNumChange && handleNumChange(max);
return setIptValue(max);
}
if (step && min && Number(iptValue) + stepNum < min) {
handleNumChange && handleNumChange(min);
return setIptValue(min);
}
handleNumChange && handleNumChange(Number(iptValue) + stepNum);
setIptValue(Number(iptValue) + stepNum);
};
const lowNum = () => {
if (type === 'num' && Number(iptValue) == NaN) {
return setIptValue('');
}
const stepNum = step || 1;
if (step && min && Number(iptValue) - stepNum < min) {
handleNumChange && handleNumChange(min);
return setIptValue(min);
}
handleNumChange && handleNumChange(Number(iptValue) - stepNum);
setIptValue(Number(iptValue) - stepNum);
};
const iptType = useMemo(() => {
if (showTogglePwd && type === 'password') {
return pwdIptState ? 'password' : 'text';
}
return type || 'text';
}, [type, showTogglePwd, pwdIptState]);
const exticStyle = useMemo(() => {
let style = { width: '170px' };
if (width) {
style.width = width + 'px';
}
console.log({ ...style, ...moreStyle });
return { ...style, ...moreStyle };
}, [width, moreStyle]);
return (
<div className={style.box} style={{ width: width ? width + 'px' : '170px' }}>
<input
className={style.input}
style={exticStyle}
type={iptType}
placeholder={placeholder}
value={iptValue}
onChange={changeIpt}
onBlur={blurIpt}
/>
{
(showClear && (
<CloseOutlined
style={{ position: 'absolute', right: '5px', fontSize: '12px', cursor: 'pointer' }}
onClick={() => setIptValue('')}
/>
)) ||
(type === 'password' && showTogglePwd && (
<EyeOutlined
style={{ position: 'absolute', right: '5px', fontSize: '12px', cursor: 'pointer' }}
onClick={() => setPwdIptState(!pwdIptState)}
/>
)) ||
(type === 'num' && (
<div className={style.numTags}>
<UpOutlined style={{ cursor: 'pointer', fontSize: '10px' }} onClick={addNum} />
<DownOutlined style={{ cursor: 'pointer', fontSize: '10px' }} onClick={lowNum} />
</div>
))
}
</div>
);
};
export default memo(Input);
组件库线上链接变了哈~ http://124.222.161.174:8080/#/common/input 多多关注,看到记得点赞哦
|