Skip to content

Commit

Permalink
fix(:bug)
Browse files Browse the repository at this point in the history
Icons with selectors that contain `type` as a substring weren't getting cloned. Moving a word boundary search
  • Loading branch information
atomicpages committed Sep 29, 2020
1 parent b928a38 commit 9326927
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/hooks/__tests__/useIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ describe('useIcon hook tests', () => {

expect(result.current.icon).toEqual(instance);
});

it('should work when icon contain the type as a substring', () => {
const { result } = renderHook(() =>
useIcon(
React.createElement('i', { className: 'myicon myicon-star' }) as React.ReactElement<
any,
any
>
)
);

expect(result.current).toMatchObject({ iconType: 'icon' });

// @ts-ignore
expect(result.current.icon.props.className).toContain('icon');
});
});
6 changes: 5 additions & 1 deletion src/hooks/useIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ export const useIcon = (icon: PCRCheckboxRadioProps['icon']) => {
type = 'svg';
}

// icons that contain `type` substring
// won't fail with a word boundary search
const re = new RegExp(`\\b${type}\\b`);

return {
iconType: icon.props['data-type'] || type,
icon:
icon.props.className && !icon.props.className.includes(type)
icon.props.className && !re.test(icon.props.className)
? React.cloneElement(icon, {
...icon.props,
className: classNames(icon.props.className, type),
Expand Down

0 comments on commit 9326927

Please sign in to comment.