import React, { forwardRef, useRef, useState, useEffect } from "react"; import { mergeRefs } from "../_util/merge-refs"; import { InputProps, Input } from "../input"; import { callBoth } from "../_util/call-both"; import { noop } from "../_util/noop"; interface TagSelectInputProps extends InputProps { maxWidth: number; reservedWidth?: number; } export const TagSelectInput = forwardRef(function TagSelectInput( { value, maxWidth, reservedWidth = 6, placeholder = "", style = {}, onInput = noop, ...props }: TagSelectInputProps, ref: React.Ref ) { const inputRef = useRef(null); const mirrorRef = useRef(null); const [width, setWidth] = useState(0); const [fullInputValue, setFullInputValue] = useState(value); useEffect(() => { setFullInputValue(value); }, [value]); useEffect(() => { mirrorRef.current.innerText = fullInputValue || placeholder || ""; const width = mirrorRef.current.clientWidth; setWidth(width > maxWidth ? maxWidth : width); }, [fullInputValue, maxWidth, placeholder]); const handleInput = e => setFullInputValue(e.currentTarget.value); return ( <> ); }); TagSelectInput.displayName = "TagSelectInput";