Skip to content

Commit

Permalink
Fix(web-react): Mandatory href for anchors #DS-661
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj authored and crishpeen committed Nov 19, 2024
1 parent 271af54 commit 3dd8297
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/web-react/src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useStyleProps } from '../../hooks';
import { PolymorphicRef, SpiritLinkProps } from '../../types';
import { useLinkStyleProps } from './useLinkStyleProps';

const defaultProps: Partial<SpiritLinkProps> = {
const defaultProps: Partial<SpiritLinkProps<ElementType>> = {
elementType: 'a',
color: 'primary',
hasVisitedStyleAllowed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('useLinkStyleProps', () => {
});

it('should return link-allowed-visited class', () => {
const props = { color: 'primary', hasVisitedStyleAllowed: true } as SpiritLinkProps;
const props = { href: '#', color: 'primary', hasVisitedStyleAllowed: true } as SpiritLinkProps;
const { result } = renderHook(() => useLinkStyleProps(props));

expect(result.current.classProps).toContain('link-allow-visited-style');
Expand Down
4 changes: 3 additions & 1 deletion packages/web-react/src/components/Link/demo/LinkDefault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import Link from '../Link';

const LinkDefault = () => (
<>
<Link href="#">Link</Link>
<Link>Link</Link>

<Link href="https://www.example.com/" target="_blank" title="Warning">
⚠️ Link with Icon
</Link>

<Link elementType="button">Button</Link>
</>
);

Expand Down
4 changes: 3 additions & 1 deletion packages/web-react/src/components/Link/useLinkStyleProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export interface LinkStyles<E extends ElementType = 'p'> {
props: Partial<LinkProps<E>>;
}

export function useLinkStyleProps<E extends ElementType = 'a', T = void>(props: SpiritLinkProps<E, T>): LinkStyles<E> {
export function useLinkStyleProps<E extends ElementType = 'a', T = void>(
props: Omit<SpiritLinkProps<E, T>, 'href'>,
): LinkStyles<E> {
const { color, hasVisitedStyleAllowed, isDisabled, underlined, ...restProps } = props;

const linkClass = useClassNamePrefix('link');
Expand Down
7 changes: 4 additions & 3 deletions packages/web-react/src/types/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export type LinkTarget = '_blank' | '_self' | '_parent' | '_top';
export type UnderlineOptions = (typeof UNDERLINED_OPTIONS)[keyof typeof UNDERLINED_OPTIONS];

export interface LinkBaseProps<C = void> extends ChildrenProps, StyleProps, TransferProps {
/** Link's href attribute */
href?: string;
/** Link's target attribute */
target?: LinkTarget;
/** Color of the Link */
Expand All @@ -39,5 +37,8 @@ export type LinkProps<E extends ElementType = 'a', C = void> = {
elementType?: E;
} & LinkBaseProps<C>;

export type SpiritLinkProps<E extends ElementType = 'a', C = void> = LinkProps<E, C> &
export type LinkHrefProps<E extends ElementType> = E extends 'a' ? { href: string } : { href?: string };

export type SpiritLinkProps<E extends ElementType = 'a', C = void> = LinkHrefProps<E> &
LinkProps<E, C> &
SpiritPolymorphicElementPropsWithRef<E, LinkProps<E, C>>;

0 comments on commit 3dd8297

Please sign in to comment.