import { AutoComplete, Badge, Input, Select, Space, Typography } from "antd"; import type { SizeType } from "antd/lib/config-provider/SizeContext" import { useState } from "react"; import { get } from "../../request"; import type { ISearchView } from "./FullTextSearchResult" const { Text } = Typography; const { Option } = Select; export interface IWordIndexData { word: string; count: number; bold: number; } export interface IWordIndexListResponse { ok: boolean; message: string; data: { rows: IWordIndexData[]; count: number; }; } interface ValueType { key?: string; label: React.ReactNode; value: string | number; } interface IWidget { value?: string; tags?: string[]; book?: number; para?: number; size?: SizeType; width?: string | number; view?: ISearchView; onSearch?: Function; onSplit?: Function; onPageTypeChange?: Function; } const FullSearchInputWidget = ({ value, ___onSplit, tags, size = "middle", width, onSearch, view = "pali", onPageTypeChange, }: IWidget) => { const [options, setOptions] = useState([]); const [input, setInput] = useState(value); const renderItem = (word: string, count: number, bold: number) => ({ value: word, label: (
{bold > 0 ? {word} : word}
), }); const search = (value: string) => { console.log("search", value); if (value === "") { return; } let url: string = ""; switch (view) { case "pali": url = `/v2/pali-word-index?view=key&key=${value}`; break; case "title": url = `/v2/search-title-index?&key=${value}`; break; default: break; } get(url).then((json) => { const words: ValueType[] = json.data.rows.map((item) => { return renderItem(item.word, item.count, item.bold); }); setOptions(words); }); }; const selectBefore = ( ); return ( {view === "page" ? selectBefore : undefined} { console.log("input", value); setInput(value); }} onSearch={(value: string) => { console.log("auto complete on search", value, tags); if (value.indexOf(" ") >= 0 || value.indexOf(";") >= 0) { const valueLast = value.split(/[ ]|;/).slice(-1); search(valueLast[0]); } else { search(value); } }} onSelect={(value: string, _option: ValueType) => { if (typeof onSearch !== "undefined") { if ( typeof input === "string" && (input.indexOf(" ") >= 0 || input.indexOf(";") >= 0) ) { const last1 = input.lastIndexOf(" "); const last2 = input.lastIndexOf(";"); let searchString = ""; if (last1 > last2) { searchString = input.slice(0, last1 + 1) + value; } else { searchString = input.slice(0, last2 + 1) + value; } onSearch(searchString); setInput(searchString); } else { onSearch(value); } } }} > { console.log("on search", value, tags); if (typeof onSearch !== "undefined") { onSearch(value); } }} /> ); }; export default FullSearchInputWidget;