File size: 1,964 Bytes
f46b416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { AutoComplete, Input } from "antd";
import { DefaultOptionType } from "antd/es/select";
import { useEffect, useState } from "react";

export class DropdownDataEntry {
    // `key` field is used for query
    readonly key: string;
    readonly data: string;

    constructor(key: string, data: string) {
        this.key = key;
        this.data = data;
    }
}

const searchForResults = (key: string, dataEntries: DropdownDataEntry[]) => {
    return dataEntries.filter(entry => entry.key.toLowerCase().includes(key.toLocaleLowerCase()));
}

export default function DropdownInput({
    dataEntries,
    onSelect,
    placeholder = 'Input here...',
    defaultValue = '',
    disabled = false,
}: {
    dataEntries: DropdownDataEntry[];
    onSelect: (value: string) => void;
    placeholder?: string;
    defaultValue?: string;
    disabled?: boolean;
}) {
    const [currResults, setCurrResults] = useState<DefaultOptionType[]>([]);
    const [currQuery, setCurrQuery] = useState<string>('');

    useEffect(() => {
        if (dataEntries.length === 0) return; 
        const newResults: DropdownDataEntry[] = currQuery ? searchForResults(currQuery, dataEntries) : dataEntries;
        const newOptions = newResults.map(entry => {return {
            label: entry.key,
            value: entry.data
        }})
        setCurrResults(newOptions);
    }, [dataEntries, currQuery])

    const handleSearch = (query: string) => {
        setCurrQuery(query);
    }

    return (
        <div>
            <AutoComplete
                dropdownMatchSelectWidth 
                options={currResults}
                onSearch={handleSearch}
                onSelect={(value: string) => {
                    onSelect(value);
                }}
                disabled={disabled}
                defaultValue={defaultValue}
            >
                <Input.Search placeholder={placeholder} enterButton />
            </AutoComplete>
        </div>
    )
}