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

buttons now have type button by default #2103

Open
wants to merge 1 commit into
base: v1.x-2022-07
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,7 @@ export function BaseButton<AsType extends React.ElementType = 'button'>(
defaultOnClick,
children,
buttonRef,
type,
...passthroughProps
} = props;

Expand All @@ -56,9 +57,15 @@ export function BaseButton<AsType extends React.ElementType = 'button'>(
);

const Component = as || 'button';
const componentType = type || (Component === 'button' ? 'button' : undefined);

return (
<Component ref={buttonRef} onClick={handleOnClick} {...passthroughProps}>
<Component
ref={buttonRef}
onClick={handleOnClick}
type={componentType}
{...passthroughProps}
>
{children}
</Component>
);
Expand Down
Expand Up @@ -99,5 +99,34 @@ describe('<BaseButton/>', () => {
expect(mockDefaultOnClick).not.toBeCalled();
});
});

describe('base button type attribute', () => {
it('is a button by default given the component is a button', () => {
render(<BaseButton>Base Button</BaseButton>, {
wrapper: ShopifyTestProviders,
});

expect(screen.getByRole('button')).toHaveAttribute('type', 'button');
});

it('is not a button by default given the component is not a button', () => {
render(<BaseButton as="p">Base Button</BaseButton>, {
wrapper: ShopifyTestProviders,
});

expect(screen.getByText('Base Button')).not.toHaveAttribute(
'type',
'button'
);
});

it('respects user provided type', () => {
render(<BaseButton type="submit">Base Button</BaseButton>, {
wrapper: ShopifyTestProviders,
});

expect(screen.getByRole('button')).toHaveAttribute('type', 'submit');
});
});
});
});