Skip to content

Commit

Permalink
Fixing an issue with Highlight (#6644)
Browse files Browse the repository at this point in the history
* Fixing an issue with highlight

* Simplifying an object property

* docs: add changelog

* Fixing tests

Co-authored-by: Segun Adebayo <joseshegs@gmail.com>
  • Loading branch information
SpencerKaiser and segunadebayo committed Sep 8, 2022
1 parent 8d0ca11 commit a9b004c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-chicken-marry.md
@@ -0,0 +1,5 @@
---
"@chakra-ui/layout": patch
---

Fixing an issue with `Highlight`
2 changes: 1 addition & 1 deletion packages/components/layout/src/highlight.tsx
Expand Up @@ -36,7 +36,7 @@ function buildRegex(query: string[]) {
function highlightWords({ text, query }: Options): Chunk[] {
const regex = buildRegex(Array.isArray(query) ? query : [query])
if (!regex) {
return []
return [{ text, match: false}]
}
const result = text.split(regex).filter(Boolean)
return result.map((str) => ({ text: str, match: regex.test(str) }))
Expand Down
11 changes: 7 additions & 4 deletions packages/components/layout/tests/layout.test.tsx
Expand Up @@ -177,15 +177,18 @@ describe("<Divider />", () => {

describe("<Highlight/>", () => {
test.each([[], ""])(
"useHighlight returns no matches if queries is empty %p ",
"useHighlight returns no matches if queries is empty but returns original value",
(query) => {
const text = "this is an ordinary text which should not have any matches"
const { result } = hooks.render(() =>
useHighlight({
query: query,
text: "this is an ordinary text which should not have any matches",
query,
text,
}),
)
expect(result.current).toHaveLength(0)
expect(result.current).toHaveLength(1)
expect(result.current[0].match).toBe(false)
expect(result.current[0].text).toBe(text)
},
)

Expand Down

1 comment on commit a9b004c

@vercel
Copy link

@vercel vercel bot commented on a9b004c Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.