Skip to content

Commit

Permalink
list component accepting the Custom component on the link tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenji Shiroma committed Oct 17, 2024
1 parent f15c148 commit d45cf6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { styles as itemStyles } from './list-item.styles.js';
import { type ListItemProps } from './list-item.types.js';

export function BaseListItem(
{ className, children, href, target, look, type, spacing, icon, ...props }: ListItemProps,
{ className, linkTag: LinkTag = 'a', children, href, target, look, type, spacing, icon, ...props }: ListItemProps,
ref: Ref<HTMLAnchorElement>,
) {
const state = useContext(ListContext);
Expand Down Expand Up @@ -45,9 +45,9 @@ export function BaseListItem(
<li className={styles.base({ className })} {...props} key={state.nested}>
{bulletToRender()}
{type === 'link' ? (
<a href={href} target={target} className={styles.link()} ref={ref} {...focusProps}>
<LinkTag href={href} target={target} className={styles.link()} ref={ref} {...focusProps}>
{children}
</a>
</LinkTag>
) : (
children
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTMLAttributeAnchorTarget, HTMLAttributes, ReactNode } from 'react';
import { HTMLAttributeAnchorTarget, HTMLAttributes, ReactElement, ReactNode } from 'react';

import { IconProps } from '../../../icon/index.js';

Expand All @@ -15,6 +15,10 @@ export type ListItemProps = {
* The icon for list item
*/
icon?: (props: IconProps) => JSX.Element;
/**
* Link tag to render
*/
linkTag?: keyof JSX.IntrinsicElements | ((...args: any[]) => ReactElement | null);
/**
* The look of the bullet, icon, tick and cross lists
*/
Expand Down
20 changes: 20 additions & 0 deletions packages/ui/src/components/list/list.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,23 @@ export const Spacing = () => (
</List>
</div>
);

const CustomLink = (props: any) => {
// eslint-disable-next-line no-console
console.info('TEST');
return <a {...props}>{props.children}</a>;
};

/**
* Custom Component
*/
export const CustomComponent = () => (
<div>
<h1 className="typography-body-10 mb-2">Title</h1>
<List type="link" className="mb-4" spacing="medium">
<ListItem linkTag={CustomLink}>Styled bullet list</ListItem>
<ListItem linkTag={CustomLink}>Styled bullet list</ListItem>
<ListItem linkTag={CustomLink}>Styled bullet list</ListItem>
</List>
</div>
);

0 comments on commit d45cf6c

Please sign in to comment.