import React from "react" import { DataOption, FiltersMessage } from "../types/filter.type" import { FilterAutocompleteField, FilterInputContainer, FilterTextField } from "./style" import { CircularProgress, IconButton, SvgIcon } from "@mui/material" export interface DataTableFilterAutoProps { style?: React.CSSProperties, messages: FiltersMessage, value: DataOption, options: (inputText: string) => Promise onChange: (value: DataOption | null) => void, } export const DataTableFilterAuto: React.FC = ({ style, value, options, onChange }) => { const [loading, setLoading] = React.useState(false); const [optionItems, setOptionItems] = React.useState(); const [timer, setTimer] = React.useState(); React.useEffect(() => { if (!!options && !optionItems) { setOptionItems([]); handleLoad(value && value.label ? value.label : ''); } }, [options, optionItems, setOptionItems]) const handleLoad = (inputText: string) => { setLoading(true); options(inputText) .then(result => { if (!!result) { setOptionItems(result) } }) .finally(() => { setLoading(false); }) } const getValue = () => { return value ? value : null; } const handleIsOptionEqualToValue = (option: any, value: any) => { return ( option.value !== undefined && option.value !== null && option.value !== '' && option.value === value.value ) || (option.label === value.label) } const handleChange = (event: any, value: any) => { onChange(value) } const handleInputChange = (event: any) => { if (timer) { clearTimeout(timer); } setTimer( setTimeout(() => { handleLoad(event.target.value) }, 500) ) } const handleClear = () => { onChange(null) } return ( option.label} isOptionEqualToValue={handleIsOptionEqualToValue} value={getValue()} onChange={handleChange} renderInput={(params: any) => { let endAdornment = params.InputProps.endAdornment as any; const adornmentChildren = endAdornment && endAdornment.props && endAdornment.props.children ? endAdornment.props.children : null; if (adornmentChildren && Array.isArray(adornmentChildren) && adornmentChildren.length > 0) { endAdornment = adornmentChildren[adornmentChildren.length - 1]; } return ( {loading ? : null} {endAdornment} ), }} onChange={handleInputChange} /> ) }} /> ) } const CloseIcon = () => ( )