Skip to content

Commit

Permalink
fix(button): update types hasIconOnly & kind !== danger (#17272)
Browse files Browse the repository at this point in the history
Co-authored-by: Taylor Jones <[email protected]>
  • Loading branch information
ariellalgilmore and tay1orjones authored Sep 12, 2024
1 parent cc7cc7f commit a6f1b23
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
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 @@ export interface ButtonBaseProps
/**
* 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 @@ Button.propTypes = {
/**
* 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(
`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

0 comments on commit a6f1b23

Please sign in to comment.