-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(clerk-js,types,localization): Introduce LinkRenderer
- Loading branch information
Showing
6 changed files
with
131 additions
and
42 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
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, { memo, useMemo } from 'react'; | ||
|
||
import { Link } from '../customizables'; | ||
import type { PropsOfComponent } from '../styledSystem'; | ||
|
||
interface LinkRendererProps extends Omit<PropsOfComponent<typeof Link>, 'href' | 'children'> { | ||
text: string; | ||
} | ||
|
||
const LINK_REGEX = /\[([^\]]+)\]\(([^)]+)\)/g; // parses [text](url) | ||
|
||
export const LinkRenderer: React.FC<LinkRendererProps> = memo(({ text, ...linkProps }) => { | ||
const memoizedLinkProps = useMemo(() => linkProps, [linkProps]); | ||
|
||
const renderedContent = useMemo(() => { | ||
const parts: (string | JSX.Element)[] = []; | ||
let lastIndex = 0; | ||
|
||
text.replace(LINK_REGEX, (match, linkText, url, offset) => { | ||
if (offset > lastIndex) { | ||
parts.push(text.slice(lastIndex, offset)); | ||
} | ||
parts.push( | ||
<Link | ||
key={offset} | ||
href={url} | ||
{...memoizedLinkProps} | ||
> | ||
{linkText} | ||
</Link>, | ||
); | ||
lastIndex = offset + match.length; | ||
return match; | ||
}); | ||
|
||
if (lastIndex < text.length) { | ||
parts.push(text.slice(lastIndex)); | ||
} | ||
|
||
return parts; | ||
}, [text, memoizedLinkProps]); | ||
|
||
return renderedContent; | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/clerk-js/src/ui/elements/__tests__/LinkRenderer.test.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 { describe, it } from '@jest/globals'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { bindCreateFixtures } from '../../utils/test/createFixtures'; | ||
import { LinkRenderer } from '../LinkRenderer'; | ||
|
||
const { createFixtures } = bindCreateFixtures('UserProfile'); | ||
|
||
describe('LinkRenderer', () => { | ||
it('renders a simple link', async () => { | ||
const { wrapper } = await createFixtures(); | ||
|
||
const screen = render(<LinkRenderer text='I agree to the [Terms of Service](https://example.com/terms)' />, { | ||
wrapper, | ||
}); | ||
|
||
expect(screen.queryByRole('link', { name: 'Terms of Service' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders multiple links', async () => { | ||
const { wrapper } = await createFixtures(); | ||
|
||
const screen = render( | ||
<LinkRenderer text='I agree to the [Terms of Service](https://example.com/terms) and [Privacy Policy](https://example.com/privacy)' />, | ||
{ wrapper }, | ||
); | ||
|
||
expect(screen.queryByRole('link', { name: 'Terms of Service' })).toBeInTheDocument(); | ||
expect(screen.queryByRole('link', { name: 'Privacy Policy' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render links with broken format', async () => { | ||
const { wrapper } = await createFixtures(); | ||
|
||
const screen = render( | ||
<LinkRenderer text='I agree to the [Terms of Service]https://example.com/terms) and [Privacy Policy](https://example.com/privacy)' />, | ||
{ wrapper }, | ||
); | ||
|
||
screen.findByText('[Terms of Service]https://example.com/terms)'); | ||
expect(screen.queryByRole('link', { name: 'Privacy Policy' })).toBeInTheDocument(); | ||
}); | ||
}); |
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