Skip to content

Commit

Permalink
feat(Tag): add forwardRef to Tag component
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrillant authored and meriouma committed Oct 31, 2022
1 parent e2cebb0 commit adf9fb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/react/src/components/tag/tag.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
ButtonHTMLAttributes,
DetailedHTMLProps,
forwardRef,
MouseEventHandler,
Ref,
SVGProps,
useCallback,
VoidFunctionComponent,
} from 'react';
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { useTranslation } from '../../i18n/use-translation';
Expand Down Expand Up @@ -194,14 +195,14 @@ const Container = styled.span<ContainerProps>`
${focus};
`;

export const Tag: VoidFunctionComponent<TagProps> = ({
export const Tag = forwardRef(({
className,
iconName,
onClick,
onDelete,
size = 'medium',
value,
}) => {
}: TagProps, ref: Ref<HTMLElement>) => {
if (onClick && onDelete) {
throw new Error('Only one of onClick or onDelete can be provided.');
}
Expand All @@ -221,6 +222,7 @@ export const Tag: VoidFunctionComponent<TagProps> = ({

return (
<Container
ref={ref}
as={onClick ? 'button' : 'span'}
className={className}
onClick={handleClick}
Expand Down Expand Up @@ -261,4 +263,6 @@ export const Tag: VoidFunctionComponent<TagProps> = ({
)}
</Container>
);
};
});

Tag.displayName = 'Tag';
13 changes: 12 additions & 1 deletion packages/storybook/stories/tag.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Tag, TagProps, TagValue } from '@equisoft/design-elements-react';
import { Story } from '@storybook/react';
import { ReactElement, useState } from 'react';
import { ReactElement, useRef, useState } from 'react';
import { rawCodeParameters } from './utils/parameters';

export default {
title: 'Notification/Tag',
Expand Down Expand Up @@ -81,3 +82,13 @@ export const Clickable: Story = () => {
</>
);
};

export const WithRef: Story = () => {
const ref = useRef(null);

return (
<Tag ref={ref} key="small" iconName="copy" size="small" value={{ label: 'Tag 1' }} />
);
};

WithRef.parameters = rawCodeParameters;

0 comments on commit adf9fb5

Please sign in to comment.