Skip to content

Commit

Permalink
feat: improve Link props and export LabelFactory for Select
Browse files Browse the repository at this point in the history
refs: (#98)
  • Loading branch information
gnekoz authored Jul 27, 2022
1 parent 1bbf612 commit 025fa48
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
11 changes: 5 additions & 6 deletions src/components/basic/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { screen } from '@testing-library/dom';
import React from 'react';
import { faker } from '@faker-js/faker';
import { render } from '../../test-utils';
import { Link } from './Link';

describe('Link', () => {
const text = faker.lorem.words(3);
test('Render a Link', () => {
const { container } = render(
<Link weight="bold" size="large" color="warning" underlined>
const text = 'some content';
render(
<Link weight="bold" size="large" color="warning" underlined href="https://test-link.test">
{text}
</Link>
);
expect(container.querySelector('a')).toBeInTheDocument();
expect(container.querySelector('a')).toHaveTextContent(text);
expect(screen.getByRole('link', { name: text })).toBeVisible();
});
});
24 changes: 8 additions & 16 deletions src/components/basic/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import React, { useMemo, useCallback, AnchorHTMLAttributes } from 'react';
import React, { useMemo, useCallback } from 'react';
import styled from 'styled-components';
import type { ThemeObj } from '../../theme/theme';
import { getColor } from '../../theme/theme-utils';

import { Text, TextProps } from './Text';
import { useKeyboard, getKeyboardPreset } from '../../hooks/useKeyboard';
import { useCombinedRefs } from '../../hooks/useCombinedRefs';

const StyledLink = styled(Text).attrs({
const StyledLink = styled(Text).attrs(() => ({
forwardedAs: 'a'
})<{
}))<{
$underlined: boolean;
}>`
cursor: pointer;
Expand All @@ -29,14 +28,14 @@ const StyledLink = styled(Text).attrs({
}
`;

interface LinkProps extends TextProps {
type LinkProps = {
/** Whether the link should be underlined */
underlined?: boolean;
color?: string | keyof ThemeObj['palette'];
}
} & React.AnchorHTMLAttributes<HTMLAnchorElement> &
TextProps;

const Link = React.forwardRef<HTMLDivElement, LinkProps>(function LinkFn(
{ children, size, underlined = false, color = 'primary', ...rest },
{ children, underlined = false, color = 'primary', ...rest },
ref
) {
const linkRef = useCombinedRefs<HTMLDivElement>(ref);
Expand All @@ -46,14 +45,7 @@ const Link = React.forwardRef<HTMLDivElement, LinkProps>(function LinkFn(
useKeyboard(linkRef, keyEvents);

return (
<StyledLink
ref={linkRef}
size={size}
$underlined={underlined}
color={color}
tabIndex={0}
{...rest}
>
<StyledLink ref={linkRef} $underlined={underlined} color={color} tabIndex={0} {...rest}>
{children}
</StyledLink>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/inputs/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,4 @@ const Select = React.forwardRef<HTMLDivElement, SelectProps>(function SelectFn(
);
});

export { Select, SelectProps, SelectItem };
export { Select, SelectProps, SelectItem, LabelFactoryProps };

0 comments on commit 025fa48

Please sign in to comment.