diff --git a/CHANGELOG.md b/CHANGELOG.md index d1ca4c54673..1d65f32f700 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [`master`](https://github.com/elastic/eui/tree/master) +- Converted `EuiFacetButton` to TypeScript ([#2226](https://github.com/elastic/eui/pull/2226)) + +**Bug fixes** + - Fixed `EuiSwitch` semantics to align with aria roles ([#2193](https://github.com/elastic/eui/pull/2193)) - Removed Firefox's focus ring to match other browsers ([#2193](https://github.com/elastic/eui/pull/2193)) diff --git a/src/components/facet/__snapshots__/facet_button.test.js.snap b/src/components/facet/__snapshots__/facet_button.test.tsx.snap similarity index 100% rename from src/components/facet/__snapshots__/facet_button.test.js.snap rename to src/components/facet/__snapshots__/facet_button.test.tsx.snap diff --git a/src/components/facet/facet_button.test.js b/src/components/facet/facet_button.test.tsx similarity index 100% rename from src/components/facet/facet_button.test.js rename to src/components/facet/facet_button.test.tsx diff --git a/src/components/facet/facet_button.js b/src/components/facet/facet_button.tsx similarity index 70% rename from src/components/facet/facet_button.js rename to src/components/facet/facet_button.tsx index e35e0664fa4..795c2c72d0b 100644 --- a/src/components/facet/facet_button.js +++ b/src/components/facet/facet_button.tsx @@ -1,18 +1,49 @@ -import React from 'react'; -import PropTypes from 'prop-types'; +import React, { + FunctionComponent, + HTMLAttributes, + MouseEventHandler, + ReactNode, +} from 'react'; import classNames from 'classnames'; +import { CommonProps, RefCallback } from '../common'; + import { EuiNotificationBadge } from '../badge'; import { EuiLoadingSpinner } from '../loading'; -export const EuiFacetButton = ({ +export interface EuiFacetButtonProps { + buttonRef?: RefCallback; + children: ReactNode; + /** + * Any node, but preferrably a `EuiIcon` or `EuiAvatar` + */ + icon?: ReactNode; + isDisabled?: boolean; + /** + * Adds/swaps for loading spinner & disables + */ + isLoading?: boolean; + /** + * Changes visual of button to indicate it's currently selected + */ + isSelected?: boolean; + onClick?: MouseEventHandler; + /** + * Adds a notification indicator for displaying the quantity provided + */ + quantity?: number; +} + +export const EuiFacetButton: FunctionComponent< + CommonProps & HTMLAttributes & EuiFacetButtonProps +> = ({ children, className, icon, - isDisabled, - isLoading, - isSelected, + isDisabled = false, + isLoading = false, + isSelected = false, quantity, buttonRef, ...rest @@ -50,9 +81,9 @@ export const EuiFacetButton = ({ // Add an icon to the button if one exists. let buttonIcon; - if (icon) { + if (React.isValidElement<{ className?: string }>(icon)) { buttonIcon = React.cloneElement(icon, { - className: 'euiFacetButton__icon', + className: classNames(icon.props.className, 'euiFacetButton__icon'), }); } @@ -78,37 +109,3 @@ export const EuiFacetButton = ({ ); }; - -EuiFacetButton.propTypes = { - children: PropTypes.node.isRequired, - className: PropTypes.string, - isDisabled: PropTypes.bool, - onClick: PropTypes.func, - buttonRef: PropTypes.func, - - /** - * Any node, but preferrably a `EuiIcon` or `EuiAvatar` - */ - icon: PropTypes.node, - - /** - * Adds/swaps for loading spinner & disables - */ - isLoading: PropTypes.bool, - - /** - * Changes visual of button to indicate it's currently selected - */ - isSelected: PropTypes.bool, - - /** - * Adds a notification indicator for displaying the quantity provided - */ - quantity: PropTypes.number, -}; - -EuiFacetButton.defaultProps = { - isDisabled: false, - isLoading: false, - isSelected: false, -}; diff --git a/src/components/facet/index.d.ts b/src/components/facet/index.d.ts index 52c33881563..721f430bd8b 100644 --- a/src/components/facet/index.d.ts +++ b/src/components/facet/index.d.ts @@ -1,13 +1,9 @@ -import { - ButtonHTMLAttributes, - HTMLAttributes, - ReactNode, - MouseEventHandler, - FunctionComponent, -} from 'react'; -import { CommonProps, RefCallback } from '../common'; +import { ButtonHTMLAttributes, HTMLAttributes, FunctionComponent } from 'react'; +import { CommonProps } from '../common'; /// +import { EuiFacetButtonProps as ButtonProps } from './facet_button'; + declare module '@elastic/eui' { /** * Facet button type defs @@ -15,16 +11,8 @@ declare module '@elastic/eui' { * @see './facet_button.js' */ - export interface EuiFacetButtonProps { - children: ReactNode; - icon?: ReactNode; - isDisabled?: boolean; - onClick?: MouseEventHandler; - isLoading?: boolean; - isSelected?: boolean; - quantity: number; - buttonRef: RefCallback; - } + export type EuiFacetButtonProps = ButtonProps; + export const EuiFacetButton: FunctionComponent< CommonProps & ButtonHTMLAttributes & EuiFacetButtonProps >;