diff --git a/components/input/input.tsx b/components/input/input.tsx index a37da1d..d907514 100644 --- a/components/input/input.tsx +++ b/components/input/input.tsx @@ -67,7 +67,7 @@ const Input = React.forwardRef(initialValue) const [hover, setHover] = useState(false) const { heightRatio, fontSize } = useMemo(() => getSizes(size), [size]) - const showClearIcon = useMemo(() => clearable && selfValue !== '', [selfValue, clearable]) + const isControlledComponent = useMemo(() => value !== undefined, [value]) const labelClasses = useMemo(() => (labelRight ? 'right-label' : label ? 'left-label' : ''), [ label, labelRight, @@ -80,12 +80,12 @@ const Input = React.forwardRef) => { if (disabled || readOnly) return setSelfValue(event.target.value) onChange && onChange(event) } - const clearHandler = (event: React.MouseEvent) => { setSelfValue('') onClearClick && onClearClick(event) @@ -121,9 +121,18 @@ const Input = React.forwardRef { - if (value === undefined) return - setSelfValue(value) - }, [value]) + if (isControlledComponent) { + setSelfValue(value as string) + } + }) + + const controlledValue = isControlledComponent + ? { value: selfValue } + : { defaultValue: initialValue } + const inputProps = { + ...props, + ...controlledValue, + } return (
@@ -139,7 +148,6 @@ const Input = React.forwardRef {clearable && ( + { + const ref = React.useRef(null) + const setChange = () => { + ref && (ref.current.value = Math.random().toString(32)) + } + return ( + <> + console.log(e.target.value)} ref={ref} /> + + + + ) +} +`} +/> + Input.Props diff --git a/pages/zh-cn/components/input.mdx b/pages/zh-cn/components/input.mdx index 07d6269..ae7c284 100644 --- a/pages/zh-cn/components/input.mdx +++ b/pages/zh-cn/components/input.mdx @@ -186,6 +186,28 @@ export const meta = { `} /> + { + const ref = React.useRef(null) + const setChange = () => { + ref && (ref.current.value = Math.random().toString(32)) + } + return ( + <> + console.log(e.target.value)} ref={ref} /> + + + + ) +} +`} +/> + Input.Props