Skip to content

Commit 13deaaa

Browse files
committed
Working nicely
1 parent e9cf905 commit 13deaaa

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

src/SearchSuggestions.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SearchSuggestions = ({
1515
withTheme = false,
1616
id,
1717
onChange,
18+
highlightKeywords = false,
1819
}: Props): JSX.Element => {
1920
const [results, setResults] = React.useState<React.ReactNode[]>(suggestions)
2021
const inputSearchRef = React.useRef<HTMLInputElement>(null)
@@ -61,7 +62,12 @@ const SearchSuggestions = ({
6162
onMouseOver={onResultsHover}
6263
onKeyDown={onResultsKeyDown}
6364
>
64-
{wrapElementText(suggestion, inputSearchRef.current?.value)}
65+
{highlightKeywords
66+
? wrapElementText(
67+
suggestion,
68+
inputSearchRef.current?.value || ''
69+
)
70+
: suggestion}
6571
</li>
6672
))}
6773
</ul>

src/elementText.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,36 @@ export const getElementText = (node: React.ReactNode): string | undefined => {
1818
return undefined
1919
}
2020

21+
const highlightKeyword = (content: string, keyword: string) =>
22+
reactStringReplace(content, keyword, (match, key) => (
23+
<mark key={key}>{match}</mark>
24+
))
25+
26+
const cloneChildren = (children: React.ReactElement[], keyword: string): any =>
27+
React.Children.map(children, child =>
28+
child.props
29+
? React.cloneElement(child, {
30+
children: highlightKeyword(
31+
cloneChildren(child.props.children, keyword),
32+
keyword
33+
),
34+
})
35+
: child
36+
)
37+
2138
export const wrapElementText = (
2239
node: React.ReactNode,
23-
str: string | undefined
40+
keyword: string
2441
): React.ReactElement | React.ReactNode => {
42+
const n = node as React.ReactElement
2543
const {
2644
props: { children },
27-
} = node as React.ReactElement
28-
29-
if (!Array.isArray(children) && typeof children !== 'string') {
30-
return wrapElementText(children, str)
31-
}
45+
} = n
3246

33-
if (Array.isArray(children)) {
34-
return children.map((e: React.ReactElement) => wrapElementText(e, str))
35-
}
36-
37-
return React.cloneElement(node as React.ReactElement, {
38-
children: (
39-
<span>
40-
{reactStringReplace(children, str, (match, key) => (
41-
<mark key={key}>{match}</mark>
42-
))}
43-
</span>
44-
),
47+
return React.cloneElement(n, {
48+
children:
49+
typeof children === 'string'
50+
? highlightKeyword(children, keyword)
51+
: cloneChildren(children, keyword),
4552
})
46-
47-
return node
4853
}

src/example/App.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,18 @@ const suggestions = [
6565
console.log(word)
6666
}}
6767
>
68-
<div>{word}</div>
68+
<div>
69+
<div>{word}</div>
70+
</div>
6971
</a>
7072
))
7173

7274
const App = (): JSX.Element => (
7375
<SearchSuggestions
7476
suggestions={suggestions}
75-
autoFocus={true}
76-
withTheme={true}
77+
autoFocus
78+
withTheme
79+
highlightKeywords
7780
/>
7881
)
7982

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export type Props = {
99
autoFocus?: boolean
1010
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void
1111
withTheme?: boolean
12+
highlightKeywords?: boolean
1213
}

0 commit comments

Comments
 (0)