-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: highlight search results in single-category filter menus (#546)
- Loading branch information
Showing
8 changed files
with
116 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...-explorer-ui/src/components/Filter/components/HighlightedLabel/highlightedLabel.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { css } from "@emotion/react"; | ||
import styled from "@emotion/styled"; | ||
|
||
interface MatchHighlightProps { | ||
leftOpen: boolean; | ||
rightOpen: boolean; | ||
} | ||
|
||
export const MatchHighlight = styled.mark<MatchHighlightProps>` | ||
background: ${({ theme }) => theme.palette.warning.light}; | ||
color: inherit; | ||
padding: 2px 0; | ||
${({ leftOpen }) => | ||
leftOpen && | ||
css` | ||
padding-left: 2px; | ||
margin-left: -2px; | ||
`} | ||
${({ rightOpen }) => | ||
rightOpen && | ||
css` | ||
padding-right: 2px; | ||
margin-right: -2px; | ||
`} | ||
`; |
44 changes: 44 additions & 0 deletions
44
...s/data-explorer-ui/src/components/Filter/components/HighlightedLabel/highlightedLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from "react"; | ||
import { FilterMenuSearchMatchRange } from "../../../../components/Filter/common/entities"; | ||
import { MatchHighlight } from "./highlightedLabel.styles"; | ||
|
||
interface HighlightedLabelProps { | ||
label: string; | ||
ranges?: FilterMenuSearchMatchRange[]; | ||
} | ||
|
||
export const HighlightedLabel = ({ | ||
label, | ||
ranges, | ||
}: HighlightedLabelProps): JSX.Element => { | ||
const items = []; | ||
if (ranges) { | ||
ranges = ranges.slice().sort(({ start: a }, { start: b }) => a - b); | ||
let prevIndex = 0; | ||
for (let i = 0; i < ranges.length; i++) { | ||
const { start } = ranges[i]; | ||
let { end } = ranges[i]; | ||
// Consolidate overlapping ranges | ||
while (i + 1 < ranges.length && ranges[i + 1].start <= end) { | ||
i++; | ||
end = Math.max(end, ranges[i].end); | ||
} | ||
const leftChar = label[start - 1]; | ||
const rightChar = label[end]; | ||
const leftOpen = !leftChar || /\s/.test(leftChar); | ||
const rightOpen = !rightChar || /\s/.test(rightChar); | ||
const matchItems = [ | ||
label.substring(prevIndex, start), | ||
<MatchHighlight key={start} leftOpen={leftOpen} rightOpen={rightOpen}> | ||
{label.substring(start, end)} | ||
</MatchHighlight>, | ||
]; | ||
prevIndex = end; | ||
items.push(matchItems); | ||
} | ||
items.push(label.substring(prevIndex)); | ||
} else { | ||
items.push(label); | ||
} | ||
return <span>{items}</span>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.