import React, { forwardRef, useId, type HTMLAttributes } from 'react' import classnames from 'classnames' import { ClearButton } from '~components/ClearButton' import { type FieldMessageProps } from '~components/FieldMessage' import { Icon } from '~components/Icon' import { RemovableTag } from '~components/__next__' import { type OverrideClassName } from '~components/types/OverrideClassName' import { type MultiSelectOption } from '../../types' import styles from './MultiSelectToggle.module.scss' export type MultiSelectToggleProps = { onClick: React.MouseEventHandler ['aria-labelledby']: string ['aria-controls']: string selectedOptions: MultiSelectOption[] isOpen?: boolean status?: FieldMessageProps['status'] onRemoveOption: (optionValue: MultiSelectOption['value']) => void onRemoveAllOptions: () => void } & OverrideClassName> export const MultiSelectToggle = forwardRef( ( { onClick, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-controls': ariaControls, isOpen = false, classNameOverride, selectedOptions, onRemoveOption, onRemoveAllOptions, status, ...restProps }, ref, ): JSX.Element => { const clearAllId = useId() return ( <> {/* * The inner
{selectedOptions.length > 0 && ( <> {/* list-style: none removes role="list" in Safari */} {/* eslint-disable-next-line jsx-a11y/no-redundant-roles */}
    {selectedOptions.map(({ label, value }) => ( // This stops the underlying toggle collapsing the popover when interactive with Tags // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events
  • e.stopPropagation()}> onRemoveOption(value), }} > {label}
  • ))}
{ e.stopPropagation() onRemoveAllOptions() }} /> )}
) }, ) MultiSelectToggle.displayName = 'MultiSelectToggle'