import React, { HTMLAttributes, useRef } from 'react'; import './index.scss'; export type Tip = { id: number; message: React.ReactNode; }; export interface AutoCompleteProps extends HTMLAttributes { children?: React.ReactNode; tips?: Tip[]; value: string | number; callback: (value: any) => void; } const AutoComplete: React.FC = (props) => { const { children, tips, value, callback, ...rest } = props; const tipWrapRef = useRef(null); const tipRef = useRef(null); const inputRef = useRef(null); const open = () => { tipWrapRef.current!.style.height = `${tipRef.current!.getBoundingClientRect().height}px`; }; const close = () => { tipWrapRef.current!.style.height = '0px'; }; const tipHandle = (e: React.MouseEvent) => { const el = e.target as HTMLParagraphElement; callback(el.innerText); }; return (
) => { callback(e.target.value); }} ref={inputRef} type="text" onFocus={open} onBlur={close} />
{children}
{tips?.map((item) => { return (
) => { tipHandle(e); }} > {item.message}
); })}
); }; AutoComplete.defaultProps = { children: '', tips: [] }; export default AutoComplete;