Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Toc): correctly display content of no items.length and add event forward #1939

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/Toc/Toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export interface TocProps extends QAProps {
items: TocItemType[];
value?: string;
onUpdate?: (value: string) => void;
onItemClick?: (event: React.MouseEvent) => void;
}

export const Toc = React.forwardRef<HTMLElement, TocProps>(function Toc(props, ref) {
const {value: activeValue, items, className, onUpdate, qa} = props;
const {value: activeValue, items, className, onUpdate, onItemClick, qa} = props;

return (
<nav className={b(null, className)} ref={ref} data-qa={qa}>
Expand All @@ -31,8 +32,9 @@ export const Toc = React.forwardRef<HTMLElement, TocProps>(function Toc(props, r
href={href}
active={activeValue === value}
onClick={onUpdate}
onItemClick={onItemClick}
/>
{childrenItems?.length && (
{childrenItems && childrenItems.length > 0 && (
<ul className={b('subsections')}>
{childrenItems?.map(
({
Expand All @@ -51,6 +53,7 @@ export const Toc = React.forwardRef<HTMLElement, TocProps>(function Toc(props, r
childItem={true}
active={activeValue === childrenValue}
onClick={onUpdate}
onItemClick={onItemClick}
/>
</li>
),
Expand Down
23 changes: 14 additions & 9 deletions src/components/Toc/TocItem/TocItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ export interface TocItemProps extends TocItemType {
childItem?: boolean;
active?: boolean;
onClick?: (value: string) => void;
onItemClick?: (event: React.MouseEvent) => void;
}

export const TocItem = (props: TocItemProps) => {
const {active = false, childItem = false, content, href, value, onClick} = props;

const handleClick = React.useCallback(() => {
if (value === undefined || !onClick) {
return;
}

onClick(value);
}, [onClick, value]);
const {active = false, childItem = false, content, href, value, onClick, onItemClick} = props;

const handleClick = React.useCallback(
(event: React.MouseEvent) => {
onItemClick?.(event);
if (value === undefined || !onClick) {
return;
}

onClick(value);
},
[onClick, onItemClick, value],
);

const {onKeyDown} = useActionHandlers(handleClick);

Expand Down
22 changes: 16 additions & 6 deletions src/components/Toc/__tests__/Toc.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ describe('Toc', () => {
const nextValue = defaultItems[0].value;
const nextTitle = defaultItems[0].content;
const onUpdateFn = jest.fn();
const onItemClickFn = jest.fn();
const user = userEvent.setup();

render(<Toc value={defaultValue} items={defaultItems} onUpdate={onUpdateFn} />);
render(
<Toc
value={defaultValue}
items={defaultItems}
onUpdate={onUpdateFn}
onItemClick={onItemClickFn}
/>,
);
const nextItem = screen.getByText(nextTitle);
await user.click(nextItem);

expect(onUpdateFn).toBeCalledWith(nextValue);
expect(onUpdateFn).toHaveBeenCalledWith(nextValue);
expect(onItemClickFn).toHaveBeenCalledWith(expect.objectContaining({}));
});

test('calls onUpdate with correct item with link', async () => {
Expand All @@ -71,13 +80,14 @@ describe('Toc', () => {
const nextItem = screen.getByText(nextTitle);
await user.click(nextItem);

expect(onUpdateFn).toBeCalledWith(nextValue);
expect(onUpdateFn).toHaveBeenCalledWith(nextValue);
});

test('accessible for keyboard', async () => {
const firstTitle = defaultItems[0].content;
const secondValue = defaultItems[1].value;
const onUpdateFn = jest.fn();

const user = userEvent.setup();

render(<Toc value={defaultValue} items={defaultItems} onUpdate={onUpdateFn} />);
Expand All @@ -86,7 +96,7 @@ describe('Toc', () => {
await user.tab();
await user.keyboard('{Enter}');

expect(onUpdateFn).toBeCalledWith(secondValue);
expect(onUpdateFn).toHaveBeenCalledWith(secondValue);
});

test('accessible for keyboard with links', async () => {
Expand All @@ -101,7 +111,7 @@ describe('Toc', () => {
await user.tab();
await user.keyboard('{Enter}');

expect(onUpdateFn).toBeCalledWith(secondValue);
expect(onUpdateFn).toHaveBeenCalledWith(secondValue);
});

test('add className', () => {
Expand Down Expand Up @@ -148,6 +158,6 @@ describe('Toc', () => {

const currentItem = screen.getByRole('listitem', {current: true});

expect(currentItem.textContent).toContain(content);
expect(currentItem.textContent).toBe(content);
});
});
Loading