import { useCallback, useEffect, useState } from "react"; import { FC } from "@tarojs/taro"; import { AtFloatLayout, AtInput, AtTabs, AtTabsPane } from "taro-ui"; import { AtFloatLayoutProps } from "taro-ui/types/float-layout"; import { View } from "@tarojs/components"; import { CITY_LIST, SIGHT_LIST } from "../../constants"; import { debounce } from "../../../../utils"; import { getSignListApi } from "../../../../api"; import "./index.scss"; export interface SearchLayoutProps extends AtFloatLayoutProps { openDetail(id: (typeof SIGHT_LIST)[0]): void; } export const SearchLayout: FC = (props) => { const [curTab, setCurTab] = useState(0); const [keyword, setKeyword] = useState(""); const [input, setInput] = useState(""); const [list, setList] = useState({}); const [loading, setLoading] = useState(false); useEffect(() => { if (props.isOpened) { getSignList(); } }, [props.isOpened]); const getSignList = async (idx?: number) => { const _idx = idx ?? curTab; if (list[_idx]) return; try { setLoading(true); const data = await getSignListApi({ region: _idx === 0 ? "" : CITY_LIST[_idx].title, type: "scene", }); setList({ ...list, [_idx]: data, }); } finally { setLoading(false); } }; const handleTabClick = (idx: number) => { setCurTab(idx); getSignList(idx); }; const debounceSearch = useCallback( debounce((v: string) => { setKeyword(v); }, 1000), [] ); const fakeSearch = (v: string) => { return v.match(keyword) !== null; }; return ( {props.isOpened && ( <> { setInput(v as string); debounceSearch(v as string); }} /> 取消 {CITY_LIST.map((item, idx) => { const _list = (list[idx] || []).filter((i) => keyword ? fakeSearch(i.name) : true ); return ( {_list.length ? _list.map((subItem) => ( {subItem.name} {subItem.description.slice(0, 50)} )) : !loading && ( 暂无内容 )} ); })} )} ); };