|
| 1 | +import React, { ChangeEvent, KeyboardEvent, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import './TaxonomySearch.styl'; |
| 4 | +import { Block } from '../../utils/bem'; |
| 5 | +import { AntTaxonomyItem } from './NewTaxonomy'; |
| 6 | +import { debounce } from 'lodash'; |
| 7 | + |
| 8 | +type TaxonomySearchProps = { |
| 9 | + treeData: AntTaxonomyItem[], |
| 10 | + onChange: (list: AntTaxonomyItem[], expandedKeys: React.Key[] | null) => void, |
| 11 | +} |
| 12 | + |
| 13 | +export type TaxonomySearchRef = { |
| 14 | + resetValue: () => void, |
| 15 | + focus: () => void, |
| 16 | +} |
| 17 | + |
| 18 | +const TaxonomySearch = React.forwardRef<TaxonomySearchRef, TaxonomySearchProps>(({ |
| 19 | + treeData, |
| 20 | + onChange, |
| 21 | +}, ref) => { |
| 22 | + useImperativeHandle(ref, (): TaxonomySearchRef => { |
| 23 | + return { |
| 24 | + resetValue() { |
| 25 | + setInputValue(''); |
| 26 | + onChange(treeData, []); |
| 27 | + }, |
| 28 | + focus() { |
| 29 | + return inputRef.current?.focus(); |
| 30 | + }, |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + const inputRef = useRef<HTMLInputElement>(); |
| 35 | + const [inputValue, setInputValue] = useState(''); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + const _filteredData = filterTreeData(treeData, inputValue); |
| 39 | + |
| 40 | + onChange(_filteredData.filteredDataTree, null); |
| 41 | + }, [treeData]); |
| 42 | + |
| 43 | + // When the treeNode has additional formatting because of `hint` or `color` props, |
| 44 | + // the `treeNode.title` is not a string but a react component, |
| 45 | + // so we have to look for the title in children (1 or 2 levels deep) |
| 46 | + const getTitle = useCallback((treeNodeTitle: any): string => { |
| 47 | + if (typeof treeNodeTitle === 'string') return treeNodeTitle; |
| 48 | + |
| 49 | + if (typeof treeNodeTitle.props.children === 'object') |
| 50 | + return getTitle(treeNodeTitle.props.children); |
| 51 | + |
| 52 | + return treeNodeTitle.props.children; |
| 53 | + }, []); |
| 54 | + |
| 55 | + // To filter the treeData items that match with the searchValue |
| 56 | + const filterTreeNode = useCallback((searchValue: string, treeNode: AntTaxonomyItem) => { |
| 57 | + const lowerSearchValue = String(searchValue).toLowerCase(); |
| 58 | + const lowerResultValue = getTitle(treeNode.title); |
| 59 | + |
| 60 | + if (!lowerSearchValue) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + return String(lowerResultValue).toLowerCase().includes(lowerSearchValue); |
| 65 | + }, []); |
| 66 | + |
| 67 | + // It's running recursively through treeData and its children filtering the content that match with the search value |
| 68 | + const filterTreeData = useCallback((treeData: AntTaxonomyItem[], searchValue: string) => { |
| 69 | + const _expandedKeys: React.Key[] = []; |
| 70 | + |
| 71 | + if (!searchValue) { |
| 72 | + return { |
| 73 | + filteredDataTree: treeData, |
| 74 | + expandedKeys: _expandedKeys, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + const dig = (list: AntTaxonomyItem[], keepAll = false) => { |
| 79 | + return list.reduce<AntTaxonomyItem[]>((total, dataNode) => { |
| 80 | + const children = dataNode['children']; |
| 81 | + |
| 82 | + const match = keepAll || filterTreeNode(searchValue, dataNode); |
| 83 | + const childList = children?.length ? dig(children, match) : undefined; |
| 84 | + |
| 85 | + if (match || childList?.length) { |
| 86 | + if (!keepAll) |
| 87 | + _expandedKeys.push(dataNode.key); |
| 88 | + |
| 89 | + total.push({ |
| 90 | + ...dataNode, |
| 91 | + isLeaf: !childList?.length, |
| 92 | + children: childList, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + return total; |
| 97 | + }, []); |
| 98 | + }; |
| 99 | + |
| 100 | + return { |
| 101 | + filteredDataTree: dig(treeData), |
| 102 | + expandedKeys: _expandedKeys, |
| 103 | + }; |
| 104 | + }, []); |
| 105 | + |
| 106 | + const handleSearch = useCallback(debounce(async (e: ChangeEvent<HTMLInputElement>) => { |
| 107 | + const _filteredData = filterTreeData(treeData, e.target.value); |
| 108 | + |
| 109 | + onChange(_filteredData.filteredDataTree, _filteredData.expandedKeys); |
| 110 | + }, 300), [treeData]); |
| 111 | + |
| 112 | + return ( |
| 113 | + <Block |
| 114 | + ref={inputRef} |
| 115 | + value={inputValue} |
| 116 | + tag={'input'} |
| 117 | + onChange={(e: ChangeEvent<HTMLInputElement>) => { |
| 118 | + setInputValue(e.target.value); |
| 119 | + handleSearch(e); |
| 120 | + }} |
| 121 | + onKeyDown={(e: KeyboardEvent<HTMLInputElement>) => { |
| 122 | + // to prevent selected items from being deleted |
| 123 | + if (e.key === 'Backspace' || e.key === 'Delete') e.stopPropagation(); |
| 124 | + }} |
| 125 | + placeholder={'Search'} |
| 126 | + data-testid={'taxonomy-search'} |
| 127 | + name={'taxonomy-search-input'} |
| 128 | + /> |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +export { TaxonomySearch }; |
0 commit comments