import React, { FunctionComponent, useEffect, useRef, useState } from 'react' import IconButton from '@mui/material/IconButton' import Magnify from 'mdi-material-ui/Magnify' import LmIcon from '../icon/LmIcon' import { ListSearchAutocompleteStoryblok } from '../../typings/generated/components-schema' import useMediaQuery from '@mui/material/useMediaQuery' import Box from '@mui/material/Box' import { useTheme } from '@mui/material/styles' export const ListSearchAutocompleteContainer: FunctionComponent< React.PropsWithChildren<{ content: ListSearchAutocompleteStoryblok popperActive?: boolean }> > = ({ content, children, popperActive }) => { const [visible, setVisible] = useState(false) const theme = useTheme() const [isMobileAction, setMobileAction] = useState(true) const matches = useMediaQuery( theme.breakpoints.down(content.mobile_breakpoint || 'xs') ) const matchMobile = !!(content.mobile_breakpoint && matches) const ref = useRef() useEffect(() => { setMobileAction(matchMobile) }, [matchMobile, setMobileAction]) useEffect(() => { if (!isMobileAction) { return } if (visible) { const input = ref.current?.querySelector('input') as HTMLInputElement input?.focus() } }, [visible, isMobileAction]) useEffect(() => { if (!isMobileAction) { return } if (!popperActive) { setVisible(false) } }, [popperActive, isMobileAction]) const onOpen = () => { setVisible(true) } if (isMobileAction) { return ( <> {content.icon?.name ? ( ) : ( )} {children} ) } return <>{children} } ListSearchAutocompleteContainer.displayName = 'ListSearchAutocompleteContainer'