Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React, {
  type ChangeEvent,
  type KeyboardEvent,
  useCallback,
  useEffect,
  useImperativeHandle,
  useRef,
  useState,
} from "react";
 
import "./TaxonomySearch.scss";
import { cn } from "../../utils/bem";
import type { AntTaxonomyItem } from "./NewTaxonomy";
import debounce from "lodash/debounce";
 
type TaxonomySearchProps = {
  treeData: AntTaxonomyItem[];
  onChange: (list: AntTaxonomyItem[], expandedKeys: React.Key[] | null) => void;
};
 
export type TaxonomySearchRef = {
  resetValue: () => void;
  focus: () => void;
};
 
const TaxonomySearch = React.forwardRef<TaxonomySearchRef, TaxonomySearchProps>(({ treeData, onChange }, ref) => {
  useImperativeHandle(ref, (): TaxonomySearchRef => {
    return {
      resetValue() {
        setInputValue("");
        onChange(treeData, []);
      },
      focus() {
        return inputRef.current?.focus();
      },
    };
  });
 
  const inputRef = useRef<HTMLInputElement>();
  const [inputValue, setInputValue] = useState("");
 
  useEffect(() => {
    const _filteredData = filterTreeData(treeData, inputValue);
 
    onChange(_filteredData.filteredDataTree, null);
  }, [treeData]);
 
  // When the treeNode has additional formatting because of `hint` or `color` props,
  // the `treeNode.title` is not a string but a react component,
  // so we have to look for the title in children (1 or 2 levels deep)
  const getTitle = useCallback((treeNodeTitle: any): string => {
    if (typeof treeNodeTitle === "string") return treeNodeTitle;
 
    if (typeof treeNodeTitle.props.children === "object") return getTitle(treeNodeTitle.props.children);
 
    return treeNodeTitle.props.children;
  }, []);
 
  // To filter the treeData items that match with the searchValue
  const filterTreeNode = useCallback((searchValue: string, treeNode: AntTaxonomyItem) => {
    const lowerSearchValue = String(searchValue).toLowerCase();
    const lowerResultValue = getTitle(treeNode.title);
 
    if (!lowerSearchValue) {
      return false;
    }
 
    return String(lowerResultValue).toLowerCase().includes(lowerSearchValue);
  }, []);
 
  // It's running recursively through treeData and its children filtering the content that match with the search value
  const filterTreeData = useCallback((treeData: AntTaxonomyItem[], searchValue: string) => {
    const _expandedKeys: React.Key[] = [];
 
    if (!searchValue) {
      return {
        filteredDataTree: treeData,
        expandedKeys: _expandedKeys,
      };
    }
 
    const dig = (list: AntTaxonomyItem[], keepAll = false) => {
      return list.reduce<AntTaxonomyItem[]>((total, dataNode) => {
        const children = dataNode.children;
 
        const match = keepAll || filterTreeNode(searchValue, dataNode);
        const childList = children?.length ? dig(children, match) : undefined;
 
        if (match || childList?.length) {
          if (!keepAll && dataNode.children?.length) _expandedKeys.push(dataNode.key);
 
          total.push({
            ...dataNode,
            isLeaf: !childList?.length,
            children: childList,
          });
        }
 
        return total;
      }, []);
    };
 
    return {
      filteredDataTree: dig(treeData),
      expandedKeys: _expandedKeys,
    };
  }, []);
 
  const handleSearch = useCallback(
    debounce(async (e: ChangeEvent<HTMLInputElement>) => {
      const _filteredData = filterTreeData(treeData, e.target.value);
 
      onChange(_filteredData.filteredDataTree, _filteredData.expandedKeys);
    }, 300),
    [treeData],
  );
 
  return (
    <input
      ref={inputRef as any}
      value={inputValue}
      onChange={(e: ChangeEvent<HTMLInputElement>) => {
        setInputValue(e.target.value);
        handleSearch(e);
      }}
      onKeyDown={(e: KeyboardEvent<HTMLInputElement>) => {
        // to prevent selected items from being deleted
        if (e.key === "Backspace" || e.key === "Delete") e.stopPropagation();
      }}
      placeholder={"搜索"}
      data-testid={"taxonomy-search"}
      name={"taxonomy-search-input"}
      className={cn("taxonomy-search-input").toClassName()}
    />
  );
});
 
export { TaxonomySearch };