Skip to content
Closed
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
6 changes: 3 additions & 3 deletions packages/components/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const Button = forwardRef((originProps: ButtonProps, ref: React.RefObject<HTMLEl
}, [theme, variant]);

const renderTag = useMemo(() => {
if (!tag && href && !disabled) return 'a';
if (!tag && disabled) return 'div';
return tag || 'button';
}, [tag, href, disabled]);
const resolvedTag = tag || (href ? 'a' : 'button');
return resolvedTag === 'a' && (disabled || loading) ? 'div' : resolvedTag;
}, [tag, href, disabled, loading]);

return React.createElement(
renderTag,
Expand Down
125 changes: 125 additions & 0 deletions packages/components/button/__tests__/button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fireEvent, render, vi } from '@test/utils';

import { Button } from '..';

import type { ButtonProps } from '../Button';
import type { TdButtonProps } from '../type';

describe('Button', () => {
Expand Down Expand Up @@ -52,6 +53,80 @@ describe('Button', () => {
expect(container).toMatchSnapshot();
});

describe('href with disabled or loading', () => {
const inactiveStates: Array<{
name: string;
props: Pick<ButtonProps, 'disabled' | 'loading'>;
}> = [
{ name: 'disabled', props: { disabled: true } },
{ name: 'loading', props: { loading: true } },
{
name: 'disabled and loading',
props: { disabled: true, loading: true },
},
];

test.each(inactiveStates)('href with $name does not render a navigable link', ({ props }) => {
([undefined, 'a'] as TdButtonProps['tag'][]).forEach((tag) => {
const { container } = render(
<Button {...props} tag={tag} href="#button-link">
Text
</Button>,
);
const element = container.firstElementChild;

expect(element).not.toBeNull();
expect(element.tagName).toBe('DIV');
expect(container.querySelector('a[href]')).toBeNull();
if (props.disabled) expect(element).toHaveClass('t-is-disabled');
if (props.loading) expect(element).toHaveClass('t-is-loading');
});
});

test.each(inactiveStates)('tag[a] with an empty href and $name is non-navigable', ({ props }) => {
const { container } = render(
<Button {...props} tag="a" href="">
Text
</Button>,
);

expect(container.firstElementChild).not.toBeNull();
expect(container.firstElementChild.tagName).toBe('DIV');
expect(container.querySelector('a[href]')).toBeNull();
});

test.each<TdButtonProps['tag']>(['button', 'div'])('tag[%s] is preserved when inactive', (tag) => {
inactiveStates.forEach(({ props }) => {
const { container } = render(
<Button {...props} tag={tag} href="#button-link" type="submit">
Text
</Button>,
);

expect(container.firstElementChild).not.toBeNull();
expect(container.firstElementChild.tagName).toBe(tag.toUpperCase());
expect(container.querySelector('a[href]')).toBeNull();
if (tag === 'button') {
expect(container.firstElementChild).toBeDisabled();
expect(container.firstElementChild).toHaveAttribute('type', 'submit');
}
});
});

test('loading without href retains native button semantics', () => {
const { getByRole } = render(
<Button loading type="submit">
Text
</Button>,
);
const button = getByRole('button', { name: 'Text' });

expect(button.tagName).toBe('BUTTON');
expect(button).toBeDisabled();
expect(button).toHaveAttribute('type', 'submit');
});
});

test('loading[boolean]', () => {
const { container: container1 } = render(<Button>Text</Button>);
expect(container1.querySelector(`.${'t-is-loading'}`)).toBeFalsy();
Expand Down Expand Up @@ -166,4 +241,54 @@ describe('Button', () => {
expect(fn.mock.calls[0][0].type).toBe('click');
});
});

describe('scenarios', () => {
test.each<TdButtonProps['tag']>([undefined, 'a'])(
'href restores navigation, onClick and ref after inactive states with tag=%s',
(tag) => {
const ref = React.createRef<HTMLElement>();
const onClick = vi.fn((event: React.MouseEvent<HTMLElement>) => event.preventDefault());
const { container, rerender, unmount, getByRole, queryByRole } = render(
<Button tag={tag} href="#button-link" ref={ref} onClick={onClick}>
Text
</Button>,
);

[
{ disabled: false, loading: false },
{ disabled: false, loading: true },
{ disabled: true, loading: true },
{ disabled: true, loading: false },
{ disabled: false, loading: false },
].forEach((props) => {
rerender(
<Button {...props} tag={tag} href="#button-link" ref={ref} onClick={onClick}>
Text
</Button>,
);
const element = container.firstElementChild;
const inactive = props.disabled || props.loading;

expect(element).not.toBeNull();
expect(ref.current).toBe(element);
if (inactive) {
expect(element.tagName).toBe('DIV');
expect(container.querySelector('a[href]')).toBeNull();
expect(queryByRole('link')).toBeNull();
} else {
expect(element.tagName).toBe('A');
expect(element).toHaveAttribute('href', '#button-link');
expect(getByRole('link', { name: 'Text' })).toBe(element);
}

onClick.mockClear();
fireEvent.click(element);
expect(onClick).toHaveBeenCalledTimes(inactive ? 0 : 1);
});

unmount();
expect(ref.current).toBeNull();
},
);
});
});
Loading