/* * @Author: your name * @Date: 2021-12-16 14:37:12 * @Description: * @FilePath: \zl-business\src\components\Unit\textInput\index.tsx */ import React, { useEffect, useMemo, useState, useRef } from 'react'; import Icon from 'src/components/Icon/index'; import Wrap from '../wrap/Wrap'; import { Input } from 'antd'; import { returnClass } from '../unit'; import '../g.scss'; function LabelTextInput(props: ILabelTextInput) { const { title, value = '', change, size = 'normal', disabled = false, suffixicon, prefixIcon, paddingSize, padding, wrapStyle = { padding: '6px 20px 6px 20px' }, placeholder, maxLength, empty = false, maxWidth, nonNull = true // 非空 } = props; const [inputValue, setInputValue] = useState(value); // 值 const inputRef = useRef(null); const inputRef2 = useRef(null); // 监听值变化 useEffect(() => { setInputValue(value); }, [value, placeholder]); //如果接受数据是对象,有disabled的状态 const Redio = useMemo(() => { return ( {title ? ( <>
{title}
{empty ? ( {value === '' ? '空' : {value}} ) : ( ) : ( '' ) } onPressEnter={() => { inputRef?.current?.blur(); }} onChange={(e) => { change?.(e.target.value, 'change'); setInputValue(e.target.value); }} onBlur={(e) => { if (nonNull && !e.target.value) { setInputValue(value); change?.(value, 'blur'); } else { change?.(e.target.value, 'blur'); } }} /> )}
) : ( <> {empty ? ( {value === '' ? '空' : {value}} ) : ( ) : ( '' ) } placeholder={placeholder} onChange={(e) => { change?.(e.target.value, 'change'); setInputValue(e.target.value); }} onPressEnter={(e) => { inputRef2?.current?.blur(); }} onBlur={(e) => change?.(e.target.value, 'blur')} /> )} )}
); }, [ title, value, disabled, suffixicon, prefixIcon, inputValue, placeholder, empty ]); return Redio; } export default LabelTextInput; export interface ILabelTextInput { title?: any; value?: any; change?: Function; size?: string; disabled?: boolean; suffixicon?: string; prefixIcon?: string; paddingSize?: string; wrapStyle?: React.CSSProperties; padding?: string; placeholder?: string; maxLength?: number; empty?: boolean; nonNull?: boolean; maxWidth?: number; }