Skip to content

Commit

Permalink
Merge pull request #773 from Shopify/fix-757
Browse files Browse the repository at this point in the history
Ensure Buttons with a url prop output valid HTML
  • Loading branch information
BPScott authored Jan 15, 2019
2 parents d33a272 + ef69a24 commit 110a1b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Bug fixes

- Ensure disabled `Button` components with a `url` prop output valid HTML ([#773](https://github.com/Shopify/polaris-react/pull/773))

### Documentation

- Added deprecation guidelines ([#853](https://github.com/Shopify/polaris-react/pull/853))
Expand Down
42 changes: 26 additions & 16 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,32 @@ function Button({

const type = submit ? 'submit' : 'button';

return url ? (
<UnstyledLink
id={id}
url={url}
external={external}
onClick={onClick}
onFocus={onFocus}
onBlur={onBlur}
onMouseUp={handleMouseUpByBlurring}
className={className}
disabled={isDisabled}
aria-label={accessibilityLabel}
>
{content}
</UnstyledLink>
) : (
if (url) {
return isDisabled ? (
// Render an `<a>` so toggling disabled/enabled state changes only the
// `href` attribute instead of replacing the whole element.
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a id={id} className={className} aria-label={accessibilityLabel}>
{content}
</a>
) : (
<UnstyledLink
id={id}
url={url}
external={external}
onClick={onClick}
onFocus={onFocus}
onBlur={onBlur}
onMouseUp={handleMouseUpByBlurring}
className={className}
aria-label={accessibilityLabel}
>
{content}
</UnstyledLink>
);
}

return (
<button
id={id}
type={type}
Expand Down
12 changes: 6 additions & 6 deletions src/components/Button/tests/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,30 @@ describe('<Button />', () => {
});

describe('disabled', () => {
it('sets the disabled attribute on the button', () => {
it('disable without a url renders <button disabled>', () => {
const button = shallowWithAppProvider(<Button disabled />);
expect(button.find('button').prop('disabled')).toBeTruthy();
});

it('sets the disabled attribute on the link', () => {
it('disable with a url renders <a> without an href (as <a disabled> is invalid HTML)>', () => {
const button = shallowWithAppProvider(
<Button disabled url="http://google.com" />,
);
expect(button.find(UnstyledLink).prop('disabled')).toBeTruthy();
expect(button.find('a').prop('href')).toBeFalsy();
});
});

describe('loading', () => {
it('sets the disabled attribute on the button', () => {
it('loading without a url renders <button disabled>', () => {
const button = shallowWithAppProvider(<Button loading />);
expect(button.find('button').prop('disabled')).toBe(true);
});

it('sets the disabled attribute on the link', () => {
it('loading with a url renders <a> without an href (as <a disabled> is invalid HTML)', () => {
const button = shallowWithAppProvider(
<Button loading url="http://google.com" />,
);
expect(button.find(UnstyledLink).prop('disabled')).toBeTruthy();
expect(button.find('a').prop('href')).toBeFalsy();
});

it('renders a spinner into the button', () => {
Expand Down

0 comments on commit 110a1b6

Please sign in to comment.