-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextHighlight: Convert component to TypeScript #41698
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c193a1
TextHighlight: Convert component to TypeScript
c2bf95f
Update CHANGELOG.md
5b82074
Remove return type
f55b742
Merge branch 'trunk' into refactor/text-highlight-to-typescript
1f1dbcd
Format files after wp-prettier upgrade
48014df
Apply suggestions from code review
6f99bc6
Merge branch 'trunk' into refactor/text-highlight-to-typescript
c2e6ca2
Update CHANGELOG.md
ff32cd6
Update comments
ee9c11a
Add defaults
ab029d8
Merge branch 'trunk' into refactor/text-highlight-to-typescript
7f43fe5
Merge branch 'trunk' into refactor/text-highlight-to-typescript
f686b63
Move changelog entry to unreleased
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { escapeRegExp } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createInterpolateElement } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { TextHighlightProps } from './types'; | ||
|
||
/** | ||
* Highlights occurrences of a given string within another string of text. Wraps | ||
* each match with a `<mark>` tag which provides browser default styling. | ||
* | ||
* ```jsx | ||
* import { TextHighlight } from '@wordpress/components'; | ||
* | ||
* const MyTextHighlight = () => ( | ||
* <TextHighlight | ||
* text="Why do we like Gutenberg? Because Gutenberg is the best!" | ||
* highlight="Gutenberg" | ||
* /> | ||
* ); | ||
* ``` | ||
*/ | ||
export const TextHighlight = ( props: TextHighlightProps ) => { | ||
const { text = '', highlight = '' } = props; | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const trimmedHighlightText = highlight.trim(); | ||
|
||
if ( ! trimmedHighlightText ) { | ||
return <>{ text }</>; | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const regex = new RegExp( | ||
`(${ escapeRegExp( trimmedHighlightText ) })`, | ||
'gi' | ||
); | ||
|
||
return createInterpolateElement( text.replace( regex, '<mark>$&</mark>' ), { | ||
mark: <mark />, | ||
} ); | ||
}; | ||
|
||
export default TextHighlight; |
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import TextHighlight from '..'; | ||
|
||
const meta: ComponentMeta< typeof TextHighlight > = { | ||
component: TextHighlight, | ||
title: 'Components/TextHighlight', | ||
parameters: { | ||
controls: { | ||
expanded: true, | ||
}, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template: ComponentStory< typeof TextHighlight > = ( args ) => { | ||
return <TextHighlight { ...args } />; | ||
}; | ||
|
||
export const Default: ComponentStory< typeof TextHighlight > = Template.bind( | ||
{} | ||
); | ||
Default.args = { | ||
text: 'We call the new editor Gutenberg. The entire editing experience has been rebuilt for media rich pages and posts.', | ||
highlight: 'Gutenberg', | ||
}; |
This file was deleted.
Oops, something went wrong.
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,93 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import TextHighlight from '..'; | ||
|
||
const getMarks = ( container: Element ) => | ||
// Use querySelectorAll because the `mark` role is not officially supported | ||
// yet. This should be changed to `getByRole` when it is. | ||
Array.from( container.querySelectorAll( 'mark' ) ); | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const defaultText = | ||
'We call the new editor Gutenberg. The entire editing experience has been rebuilt for media rich pages and posts.'; | ||
|
||
describe( 'TextHighlight', () => { | ||
describe( 'Basic rendering', () => { | ||
it.each( [ [ 'Gutenberg' ], [ 'media' ] ] )( | ||
'should highlight the singular occurance of the text "%s" in the text if it exists', | ||
( highlight ) => { | ||
const { container } = render( | ||
<TextHighlight | ||
text={ defaultText } | ||
highlight={ highlight } | ||
/> | ||
); | ||
|
||
const highlightedEls = getMarks( container ); | ||
|
||
highlightedEls.forEach( ( el ) => { | ||
expect( el ).toHaveTextContent( | ||
new RegExp( `^${ highlight }$` ) | ||
); | ||
} ); | ||
} | ||
); | ||
|
||
it( 'should highlight multiple occurances of the string every time it exists in the text', () => { | ||
const highlight = 'edit'; | ||
|
||
const { container } = render( | ||
<TextHighlight text={ defaultText } highlight={ highlight } /> | ||
); | ||
|
||
const highlightedEls = getMarks( container ); | ||
|
||
expect( highlightedEls ).toHaveLength( 2 ); | ||
|
||
highlightedEls.forEach( ( el ) => { | ||
expect( el.textContent ).toEqual( | ||
expect.stringContaining( highlight ) | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'should highlight occurances of a string regardless of capitalisation', () => { | ||
// Note that `The` occurs twice in the default text, once in | ||
// lowercase and once capitalized. | ||
const highlight = 'The'; | ||
|
||
const { container } = render( | ||
<TextHighlight text={ defaultText } highlight={ highlight } /> | ||
); | ||
|
||
const highlightedEls = getMarks( container ); | ||
|
||
expect( highlightedEls ).toHaveLength( 2 ); | ||
|
||
// Make sure the matcher is case insensitive, since the test should | ||
// match regardless of the case of the string. | ||
const regex = new RegExp( highlight, 'i' ); | ||
|
||
highlightedEls.forEach( ( el ) => { | ||
expect( el.innerHTML ).toMatch( regex ); | ||
} ); | ||
} ); | ||
|
||
it( 'should not highlight a string that is not in the text', () => { | ||
const highlight = 'Antidisestablishmentarianism'; | ||
|
||
const { container } = render( | ||
<TextHighlight text={ defaultText } highlight={ highlight } /> | ||
); | ||
|
||
const highlightedEls = getMarks( container ); | ||
|
||
expect( highlightedEls ).toHaveLength( 0 ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting example of how ref forwarding and using the
WordPressComponentProps
type don't make much sense with the current way the component is written (cc @mirka )