自动补全的 select antd react

自动补全的 select antd react

文档:自动补全的 select antd react.note

链接:http://note.youdao.com/noteshare?id=f5e4a93d2b9d6be8e459edd4eb86323b\&sub=19796E9BC04D4ABD9ACE325FDFF59B0E
添加链接描述

js 复制代码
import React, { useState, useRef } from 'react';
import { AutoComplete, Button, Input } from 'antd';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import TimeUtil from '@/utils/TimeUtil';

interface AutoCompleteSearchBoxProps {
  // history: any; // 这里的类型可以根据你的实际需求进行调整
  // data: any; // 这里的类型可以根据你的实际需求进行调整
  value: any;
  onChange: any;
  // onSelectChange 
}

interface SearchResult {
  label: string;
}
// { value=null, onChange=(val:any)=>{} }
// D:\proj\brain\admin-antd-react\src\components\AutoCompleteSearchInput
// JumpSearchBox
// JumpSearchBox
const AutoCompleteSearchInput: React.FC<AutoCompleteSearchBoxProps> = (props) => {
  const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
  const inputRef = useRef<Input>(null);
  const navigate = useNavigate();
  const [searchVal, setSearchVal] = useState<string | null>(null);

  const handleButtonClick = () => {
    console.log("handleButtonClick")
    // console.log("autoCompleteRef.current")
    console.log("inputRef.current")
    console.log(inputRef.current)
    console.log("searchVal")
    console.log(searchVal)

    // console.log(autoCompleteRef.current)
    // if (autoCompleteRef.current) {
    //   const searchData = autoCompleteRef.current.getData(); // 获取AutoCompleteSearchInput组件的数据
    //   console.log(searchData);
    // }
  };
  // "前一周",
  const options = ["前三天",  "前一个月", "前一年"];

  function getSearchLabels(options) {
    let list = []
    for (let option of options) {
      list.push(
        {
          keywords: option,
          label: option
        },
      )

    }

    // console.log("list")
    // console.log(list)

    return list
  }


  // ts 获取 前一周的 时间戳

  // <TimeUtil className="wee"> </TimeUtil>


  let SearchLabels = [
    {
      keywords: "dadad",
      label: "dadad"
    },
    {
      label: "ada"
    },
    {
      label: "前一年",
      // timestamp: TimeUtil.yesterdayMillis(-3)
    },
    
    {
      keywords: "week,前一周,周,zhou,1",
      label: "前一周",
      timestamp: TimeUtil.weekAddTimeStamp(-1)
    },

    ...getSearchLabels(options)
  ];

  // TimeUtil.weekAddTimeStamp(-3)

  function strIsLike(str: string, str2: string) {
    return str.includes(str2) || str2.includes(str)
  }

  function findLikeStrs(str, SearchLabels) {
    let resList = []
    for (let obj of SearchLabels) {
      // obj.label
      if (
        // strIsLike(str, obj.label)
        strIsLike(str, obj.keywords||obj.label)
      ) {
        resList.push(obj)
      }

    }

    return resList
  }

  const handleSearch = (value: string) => {
    if (!value) {
      setSearchResults(SearchLabels);
      return
    }
    props?.onChange(value)

    // 发送远程搜索请求
    axios
      .get(`https://api.example.com/search?q=${value}`)
      .then((response) => {
        const searchResults = response.data.results;
        setSearchResults(searchResults);
      })
      .catch((error) => {
        console.error('Error fetching search results:', error);
        // const searchResults: SearchResult[] = SearchLabels
        // setSearchResults(searchResults);

        //  setSearchResults(SearchLabels);
        setSearchResults(
          findLikeStrs(value, SearchLabels)
        );

      });

    // if (value) {

    // } else {
    //   setSearchResults([]);
    // }
  };

  const handleSelect = (value: string) => {
    setSearchVal(value)
    props.onChange(value)

    console.log("inputRef.current");
    console.log(inputRef.current);
    inputRef.current?.focus();
    // console.log(    "inputRef.current?.input.setValue");
    // console.log(    inputRef.current?.input.setValue);

    // inputRef.current?.input.setValue(value);

    let inputItem = inputRef.current?.input
    inputItem.value = value
    const path = `/details/${value}`;
    // navigate(path);
    console.log("props");
    console.log(props);

    console.log("props.history");
    console.log(props.history);
  };

  return (
    <div>
      <AutoComplete
        style={{ width: 200 }}
        options={searchResults.map((result) => ({
          value: result.label,
          label: result.label,
        }))}
        onSearch={handleSearch}
        onSelect={handleSelect}
      >
        <Input
          ref={inputRef}
          placeholder="请输入关键字"
        />
      </AutoComplete>

      {/* <Button type="primary" onClick={handleButtonClick} >AutoCompleteSearchInputButton</Button> */}
    </div>
  );
};

export default AutoCompleteSearchInput;
js 复制代码
import AutoCompleteSearchInput from '@/components/AutoCompleteSearchInput';


  <div>
        AutoCompleteSearchInput
        <AutoCompleteSearchInput onChange={onSelectChange} ref={autoCompleteRef} history={undefined} ></AutoCompleteSearchInput>

  {/* 点击按钮 获取 AutoCompleteSearchInput的 数据  */}
        <Button type="primary" onClick={handleButtonClick} >AutoCompleteSearchInputButton</Button>
      </div>
相关推荐
迷雾漫步者1 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
向前看-1 小时前
验证码机制
前端·后端
燃先生._.2 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
m0_748235243 小时前
前端实现获取后端返回的文件流并下载
前端·状态模式
m0_748240254 小时前
前端如何检测用户登录状态是否过期
前端
black^sugar4 小时前
纯前端实现更新检测
开发语言·前端·javascript
寻找沙漠的人5 小时前
前端知识补充—CSS
前端·css
GISer_Jing5 小时前
2025前端面试热门题目——计算机网络篇
前端·计算机网络·面试