2020-07-10 09:09:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { Icon, closeSmall } from '@wordpress/icons';
|
2020-07-10 09:09:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-12-03 17:23:25 +00:00
|
|
|
import Chip, { ChipProps } from './chip';
|
|
|
|
|
2021-12-08 12:17:49 +00:00
|
|
|
export interface RemovableChipProps extends ChipProps {
|
2021-12-03 17:23:25 +00:00
|
|
|
/**
|
|
|
|
* Aria label content.
|
|
|
|
*/
|
|
|
|
ariaLabel?: string;
|
|
|
|
/**
|
|
|
|
* CSS class used.
|
|
|
|
*/
|
|
|
|
className?: string;
|
|
|
|
/**
|
|
|
|
* Whether action is disabled or not.
|
|
|
|
*/
|
|
|
|
disabled?: boolean;
|
|
|
|
/**
|
|
|
|
* Function to call when remove event is fired.
|
|
|
|
*/
|
|
|
|
onRemove?: () => void;
|
|
|
|
/**
|
|
|
|
* Whether to expand click area for remove event.
|
|
|
|
*/
|
|
|
|
removeOnAnyClick?: boolean;
|
|
|
|
}
|
2020-07-10 09:09:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component used to render a "chip" -- an item containing some text with
|
|
|
|
* an X button to remove/dismiss each chip.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {string} props.ariaLabel Aria label content.
|
|
|
|
* @param {string} props.className CSS class used.
|
|
|
|
* @param {boolean} props.disabled Whether action is disabled or not.
|
|
|
|
* @param {function():any} props.onRemove Function to call when remove event is fired.
|
|
|
|
* @param {boolean} props.removeOnAnyClick Whether to expand click area for remove event.
|
|
|
|
* @param {string} props.text The text for the chip.
|
|
|
|
* @param {string} props.screenReaderText The screen reader text for the chip.
|
|
|
|
* @param {Object} props.props Rest of props passed into component.
|
2020-07-10 09:09:49 +00:00
|
|
|
*/
|
2021-12-08 12:17:49 +00:00
|
|
|
export const RemovableChip = ( {
|
2020-07-10 09:09:49 +00:00
|
|
|
ariaLabel = '',
|
|
|
|
className = '',
|
|
|
|
disabled = false,
|
2021-12-03 17:23:25 +00:00
|
|
|
onRemove = () => void 0,
|
2020-07-10 09:09:49 +00:00
|
|
|
removeOnAnyClick = false,
|
|
|
|
text,
|
|
|
|
screenReaderText = '',
|
|
|
|
...props
|
2021-12-08 12:17:49 +00:00
|
|
|
}: RemovableChipProps ): JSX.Element => {
|
2020-07-10 09:09:49 +00:00
|
|
|
const RemoveElement = removeOnAnyClick ? 'span' : 'button';
|
|
|
|
|
|
|
|
if ( ! ariaLabel ) {
|
|
|
|
const ariaLabelText =
|
|
|
|
screenReaderText && typeof screenReaderText === 'string'
|
|
|
|
? screenReaderText
|
|
|
|
: text;
|
|
|
|
ariaLabel =
|
|
|
|
typeof ariaLabelText !== 'string'
|
|
|
|
? /* translators: Remove chip. */
|
|
|
|
__( 'Remove', 'woo-gutenberg-products-block' )
|
|
|
|
: sprintf(
|
|
|
|
/* translators: %s text of the chip to remove. */
|
|
|
|
__( 'Remove "%s"', 'woo-gutenberg-products-block' ),
|
|
|
|
ariaLabelText
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clickableElementProps = {
|
|
|
|
'aria-label': ariaLabel,
|
|
|
|
disabled,
|
|
|
|
onClick: onRemove,
|
2021-12-03 17:23:25 +00:00
|
|
|
onKeyDown: ( e: React.KeyboardEvent ) => {
|
2020-07-10 09:09:49 +00:00
|
|
|
if ( e.key === 'Backspace' || e.key === 'Delete' ) {
|
|
|
|
onRemove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const chipProps = removeOnAnyClick ? clickableElementProps : {};
|
|
|
|
const removeProps = removeOnAnyClick
|
|
|
|
? { 'aria-hidden': true }
|
|
|
|
: clickableElementProps;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Chip
|
|
|
|
{ ...props }
|
|
|
|
{ ...chipProps }
|
|
|
|
className={ classNames( className, 'is-removable' ) }
|
|
|
|
element={ removeOnAnyClick ? 'button' : props.element }
|
|
|
|
screenReaderText={ screenReaderText }
|
|
|
|
text={ text }
|
|
|
|
>
|
|
|
|
<RemoveElement
|
|
|
|
className="wc-block-components-chip__remove"
|
|
|
|
{ ...removeProps }
|
|
|
|
>
|
2020-07-10 18:20:57 +00:00
|
|
|
<Icon
|
|
|
|
className="wc-block-components-chip__remove-icon"
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ closeSmall }
|
2020-07-10 18:20:57 +00:00
|
|
|
size={ 16 }
|
|
|
|
/>
|
2020-07-10 09:09:49 +00:00
|
|
|
</RemoveElement>
|
|
|
|
</Chip>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RemovableChip;
|