Skip to content

Commit e9cf905

Browse files
committed
basically working badly
1 parent 1636abe commit e9cf905

File tree

10 files changed

+82
-55
lines changed

10 files changed

+82
-55
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"@emotion/react": "^11.8.1",
4242
"@emotion/styled": "^11.8.1",
4343
"react": "^17.0.2",
44-
"react-dom": "^17.0.2"
44+
"react-dom": "^17.0.2",
45+
"react-string-replace": "^1.0.0"
4546
},
4647
"devDependencies": {
4748
"@adhamu/zero": "^3.0.1",

public/index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
<style>
1313
body {
14-
padding: 3rem;
1514
max-width: 880px;
15+
padding: 3rem;
1616
margin: 0 auto;
1717
font-family: 'Source Sans Pro', sans-serif;
1818
}
@@ -24,6 +24,12 @@
2424
.searchSuggestions ul {
2525
top: calc(100% - 3px);
2626
}
27+
28+
mark {
29+
background: transparent;
30+
font-weight: bold;
31+
text-decoration: underline;
32+
}
2733
</style>
2834
</head>
2935

src/SearchSuggestions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22

33
import type { Props } from './types'
44

5-
import { getElementText, wrapElementText } from './getElementText'
5+
import { getElementText, wrapElementText } from './elementText'
66
import { Styled } from './styled'
77
import { useSuggestions } from './useSuggestions'
88

src/__tests__/SearchSuggestions.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { render, screen } from '@testing-library/react'
44
import userEvent from '@testing-library/user-event'
55

66
import SearchSuggestions from '../SearchSuggestions'
7-
import * as getElementText from '../getElementText'
7+
import * as getElementText from '../elementText'
88

99
const suggestions = ['reddit', 'facebook', 'twitter'].map(word => (
1010
<a key={word} href={`https://${word}.com`}>

src/__tests__/getElementText.test.tsx renamed to src/__tests__/elementText.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22

3-
import { getElementText } from '../getElementText'
3+
import { getElementText } from '../elementText'
44

55
describe('getElementText', () => {
66
const Test = ({ children }: { children?: React.ReactNode }) => (

src/elementText.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react'
2+
3+
import reactStringReplace from 'react-string-replace'
4+
5+
export const getElementText = (node: React.ReactNode): string | undefined => {
6+
if (['string', 'number'].includes(typeof node)) {
7+
return node as string
8+
}
9+
10+
if (node instanceof Array) {
11+
return [...new Set(node.map(getElementText))].join(' ')
12+
}
13+
14+
if (typeof node === 'object' && node) {
15+
return getElementText((node as React.ReactElement).props.children)
16+
}
17+
18+
return undefined
19+
}
20+
21+
export const wrapElementText = (
22+
node: React.ReactNode,
23+
str: string | undefined
24+
): React.ReactElement | React.ReactNode => {
25+
const {
26+
props: { children },
27+
} = node as React.ReactElement
28+
29+
if (!Array.isArray(children) && typeof children !== 'string') {
30+
return wrapElementText(children, str)
31+
}
32+
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+
),
45+
})
46+
47+
return node
48+
}

src/example/App.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ const suggestions = [
5454
'pressure',
5555
'cooperate',
5656
].map(word => (
57-
<a key={word} href={`https://www.google.co.uk/search?q=${word}`}>
58-
{word}
57+
<a
58+
key={word}
59+
onKeyDown={e => {
60+
if (e.key === 'Enter') {
61+
console.log(word)
62+
}
63+
}}
64+
onClick={() => {
65+
console.log(word)
66+
}}
67+
>
68+
<div>{word}</div>
5969
</a>
6070
))
6171

src/getElementText.tsx

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/styled.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const Styled = styled.div<{ withTheme?: boolean }>`
77
width: 100%;
88
}
99
10+
mark {
11+
display: inline;
12+
padding: 0;
13+
}
14+
1015
ul {
1116
position: absolute;
1217
top: 100%;
@@ -15,11 +20,6 @@ export const Styled = styled.div<{ withTheme?: boolean }>`
1520
list-style-type: none;
1621
overflow-y: auto;
1722
18-
li mark {
19-
display: inline;
20-
padding: 0;
21-
}
22-
2323
li > * {
2424
display: block;
2525
cursor: pointer;
@@ -28,7 +28,6 @@ export const Styled = styled.div<{ withTheme?: boolean }>`
2828
&:focus {
2929
border: 0;
3030
box-shadow: 0;
31-
font-weight: bold;
3231
outline: 0;
3332
}
3433
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,6 +5113,11 @@ react-is@^17.0.1:
51135113
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
51145114
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
51155115

5116+
react-string-replace@^1.0.0:
5117+
version "1.0.0"
5118+
resolved "https://registry.yarnpkg.com/react-string-replace/-/react-string-replace-1.0.0.tgz#a003f94792147509107af9d376436460234911b1"
5119+
integrity sha512-+iLsyE4AeSmnfctgswXOf1PmKRgns6wJ4LVb+8ADMU6mDK3jvBE11QzfMQf7CYtPUUiBCDjZ9ZppzXOIYrzCRg==
5120+
51165121
react@^17.0.2:
51175122
version "17.0.2"
51185123
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"

0 commit comments

Comments
 (0)