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(button): update types hasIconOnly & kind !== danger #17272

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
16 changes: 1 addition & 15 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,7 @@ Map {
"isSelected": Object {
"type": "bool",
},
"kind": Object {
"args": Array [
Array [
"primary",
"secondary",
"danger",
"ghost",
"danger--primary",
"danger--ghost",
"danger--tertiary",
"tertiary",
],
],
"type": "oneOf",
},
"kind": [Function],
"onBlur": Object {
"type": "func",
},
Expand Down
26 changes: 22 additions & 4 deletions packages/react/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import PropTypes from 'prop-types';
import React, { useRef } from 'react';
import { IconButton, IconButtonKind } from '../IconButton';
import { IconButton, IconButtonKind, IconButtonKinds } from '../IconButton';
import { composeEventHandlers } from '../../tools/events';
import { PolymorphicProps } from '../../types/common';
import { PopoverAlignment } from '../Popover';
Expand Down Expand Up @@ -74,7 +74,9 @@
/**
* Specify the kind of Button you want to create
*/
kind?: ButtonKind;
kind?: ButtonBaseProps['hasIconOnly'] extends true
? IconButtonKind
: ButtonKind;

/**
* Optional prop to allow overriding the icon rendering.
Expand Down Expand Up @@ -266,8 +268,24 @@
/**
* Specify the kind of Button you want to create
*/
// TODO: this should be either ButtonKinds or IconButtonKinds based on the value of "hasIconOnly"
kind: PropTypes.oneOf(ButtonKinds),
kind: (props, propName, componentName) => {
const { hasIconOnly } = props;
const validKinds = hasIconOnly ? IconButtonKinds : ButtonKinds;

if (props[propName] === undefined) {
return null;
}

if (!validKinds.includes(props[propName])) {
return new Error(

Check warning on line 280 in packages/react/src/components/Button/Button.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/Button/Button.tsx#L280

Added line #L280 was not covered by tests
`Invalid prop \`${propName}\` supplied to \`${componentName}\`. Expected one of ${validKinds.join(
', '
)}.`
);
}

return null;
},

/**
* Provide an optional function to be called when the button element
Expand Down
Loading