Skip to content

Commit

Permalink
Merge pull request #20 from Blockchain-Country/matchtext
Browse files Browse the repository at this point in the history
added filter matched text highlight
  • Loading branch information
Blockchain-Country authored Nov 25, 2024
2 parents d8d530c + 94af111 commit de6b6f5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/components/BookListSection/BookListSection.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ ul[data-testid='bookList_container'] {
align-items: center;
width: 100%;
}

span[data-testid='highlighted_text'] {
background-color: orange;
border-radius: 5px;
padding: 0 0.5px;
}
19 changes: 19 additions & 0 deletions src/components/BookListSection/BookListSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ const BookListSection = ({ 'data-testid': testId }) => {
)
})

const highlightFilterMatch = (text, filter) => {
if (!filter) return text

const regex = new RegExp(`(${filter})`, 'gi')

return text.split(regex).map((substring, i) => {
if (substring.toLowerCase() === filter.toLowerCase()) {
return (
<span key={i} data-testid="highlighted_text">
{substring}
</span>
)
}
return substring
})
}

return (
<section data-testid={testId}>
<h3 data-testid="bookList_title">My Book List</h3>
Expand All @@ -54,6 +71,8 @@ const BookListSection = ({ 'data-testid': testId }) => {
onHandleDeleteBook={handleDeleteBook}
onToggleFavoriteBook={toggleFavoriteBook}
data-testid={`bookList_item id=${book.id}`}
bookTitle={highlightFilterMatch(book.title, titleFilter)}
bookAuthor={highlightFilterMatch(book.authors, authorsFilter)}
/>
))
)}
Expand Down
6 changes: 0 additions & 6 deletions src/components/BookListSection/book/Book.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,3 @@ button[data-testid='delete_book_btn'] {
button[data-testid='delete_book_btn']:hover {
background-color: rgb(255, 204, 204);
}

/* .highlight {
background-color: rgba(150, 182, 238, 0.605);
border-radius: 5px;
padding: 0 0.5px;
} */
6 changes: 4 additions & 2 deletions src/components/BookListSection/book/Book.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const Book = ({
index,
onHandleDeleteBook,
onToggleFavoriteBook,
bookTitle,
bookAuthor,
...rest
}) => {
return (
<li {...rest}>
<span data-testid="book_index">{++index}.</span>
<div data-testid="book_title_and_author_wrapper">
<div data-testid="book_title_wrapper">
<span data-testid="book_title">{book.title}</span>
<span data-testid="book_title">{bookTitle}</span>
</div>
<div data-testid="book_author_wrapper">
<em>by</em>
<span data-testid="book_author">{book.authors}</span>
<span data-testid="book_author">{bookAuthor}</span>
</div>
</div>
<div data-testid="book_actions">
Expand Down

0 comments on commit de6b6f5

Please sign in to comment.