// @flow strict import * as React from 'react'; import {classify} from '../../utils/classify'; import type {IconType} from '../Icon'; import {CloseIcon, Icon} from '../Icon'; import {StatusIndicator} from '../StatusIndicator'; import {Truncate} from '../Truncate'; import css from './Chip.module.css'; type ClassNames = $ReadOnly<{ wrapper?: string, icon?: string, statusIndicator?: string, }>; export const CHIP_SEMANTIC = Object.freeze({ primary: 'primary', information: 'information', success: 'success', warning: 'warning', danger: 'danger', secondary: 'secondary', }); export type ChipSemanticType = $Values; export type BaseChipProps = { classNames?: ClassNames, semantic?: ChipSemanticType, children: React.Node, disabled?: boolean, showStatusIndicator?: boolean, disableHoverState?: boolean, line?: number, wordBreak?: string, onClick?: ?(SyntheticEvent) => mixed, onMouseEnter?: ?(SyntheticEvent) => mixed, onMouseLeave?: ?(SyntheticEvent) => mixed, }; export type LargeChipProps = { ...BaseChipProps, iconName?: string, iconType?: IconType, dismissable?: boolean, onDismiss?: ?(SyntheticEvent) => mixed, size?: 'large', }; export type MediumChipProps = { ...LargeChipProps, size?: 'medium', }; export type SmallChipProps = { ...BaseChipProps, size?: 'small', }; export type ChipProps = LargeChipProps | MediumChipProps | SmallChipProps; export const Chip: React$AbstractComponent = React.forwardRef( ( { classNames, semantic = 'primary', size = 'medium', children, iconName = '', iconType = 'regular', showStatusIndicator, dismissable = false, onDismiss = () => null, onClick, disabled, line = 1, wordBreak, disableHoverState = !onClick, // There is no reason for hover state to be active when there is no click handler attached ...restProps }: ChipProps, ref, ): React.Node => { /** * Note (Nishant): Why we are using a `div` to render a onclick element instead of a `button`? * * Rendering the `Chip` component as a button directly would have been ideal, as it would * have naturally handled interactivity and accessibility for clickable chips(which has an onClick). However, * the `Chip` component includes a `CloseIcon`, which itself is a button. Nesting a `