diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e066c2161e..9526526e34a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Converted observer utility components to TypeScript ([#2009](https://github.com/elastic/eui/pull/2009)) - Converted tool tip components to TypeScript ([#2013](https://github.com/elastic/eui/pull/2013)) - Converted `EuiCopy` to TypeScript ([#2016](https://github.com/elastic/eui/pull/2016)) +- Convert badge and token components to TypeScript ([#2026](https://github.com/elastic/eui/pull/2026)) **Bug fixes** diff --git a/src/components/badge/__snapshots__/badge.test.js.snap b/src/components/badge/__snapshots__/badge.test.tsx.snap similarity index 100% rename from src/components/badge/__snapshots__/badge.test.js.snap rename to src/components/badge/__snapshots__/badge.test.tsx.snap diff --git a/src/components/badge/badge.test.js b/src/components/badge/badge.test.tsx similarity index 100% rename from src/components/badge/badge.test.js rename to src/components/badge/badge.test.tsx diff --git a/src/components/badge/badge.js b/src/components/badge/badge.tsx similarity index 63% rename from src/components/badge/badge.js rename to src/components/badge/badge.tsx index f5a66790fdc..d67469e785a 100644 --- a/src/components/badge/badge.js +++ b/src/components/badge/badge.tsx @@ -1,13 +1,68 @@ -import React from 'react'; -import PropTypes from 'prop-types'; +import React, { + FunctionComponent, + MouseEventHandler, + HTMLAttributes, +} from 'react'; import classNames from 'classnames'; -import { EuiPropTypes } from '../../utils'; +import { CommonProps, ExclusiveUnion, keysOf, PropsOf, Omit } from '../common'; import { isColorDark, hexToRgb } from '../../services/color'; -import { IconPropType, EuiIcon } from '../icon'; +import { EuiIcon, IconColor, IconType } from '../icon'; -const colorToClassNameMap = { +type IconSide = 'left' | 'right'; + +type WithButtonProps = { + /** + * Will apply an onclick to the badge itself + */ + onClick: MouseEventHandler; + + /** + * Aria label applied to the iconOnClick button + */ + onClickAriaLabel: string; +} & Omit, 'onClick' | 'color'>; + +type WithSpanProps = Omit, 'onClick' | 'color'>; + +interface WithIconOnClick { + /** + * Will apply an onclick to icon within the badge + */ + iconOnClick: MouseEventHandler; + + /** + * Aria label applied to the iconOnClick button + */ + iconOnClickAriaLabel: string; +} + +export type EuiBadgeProps = { + /** + * Accepts any string from our icon library + */ + iconType?: IconType; + + /** + * The side of the badge the icon should sit + */ + iconSide?: IconSide; + + /** + * Accepts either our palette colors (primary, secondary ..etc) or a hex value `#FFFFFF`, `#000`. + */ + color?: IconColor; + + /** + * Props passed to the close button. + */ + closeButtonProps?: PropsOf; +} & CommonProps & + ExclusiveUnion & + ExclusiveUnion; + +const colorToClassNameMap: { [color in IconColor]: string } = { default: 'euiBadge--default', primary: 'euiBadge--primary', secondary: 'euiBadge--secondary', @@ -17,20 +72,20 @@ const colorToClassNameMap = { hollow: 'euiBadge--hollow', }; -export const COLORS = Object.keys(colorToClassNameMap); +export const COLORS = keysOf(colorToClassNameMap); -const iconSideToClassNameMap = { +const iconSideToClassNameMap: { [side in IconSide]: string } = { left: 'euiBadge--iconLeft', right: 'euiBadge--iconRight', }; -export const ICON_SIDES = Object.keys(iconSideToClassNameMap); +export const ICON_SIDES = keysOf(iconSideToClassNameMap); -export const EuiBadge = ({ +export const EuiBadge: FunctionComponent = ({ children, - color, + color = 'default', iconType, - iconSide, + iconSide = 'left', className, onClick, iconOnClick, @@ -39,8 +94,10 @@ export const EuiBadge = ({ closeButtonProps, ...rest }) => { + checkValidColor(color); + let optionalColorClass = null; - let optionalCustomStyles = null; + let optionalCustomStyles = undefined; let textColor = null; if (COLORS.indexOf(color) > -1) { @@ -73,6 +130,11 @@ export const EuiBadge = ({ let optionalIcon = null; if (iconType) { if (iconOnClick) { + if (!iconOnClickAriaLabel) { + console.warn( + 'When passing the iconOnClick props to EuiBadge, you must also provide iconOnClickAriaLabel' + ); + } optionalIcon = (