import { RtFrame } from '@dpa/react-frame-intro'; import styles from './rt-chips-action.module.css'; import classNames from 'classnames/bind'; import { RtSymbolAction } from '@dpa/react-symbol-action'; import { RtText } from '@dpa/react-text'; import config from '../styles/component-values-chips.module.css'; import { getPropBySize } from '../utils'; import stylesBaseChips from '../styles/base-chips.module.css'; import { RtChipsActionProps } from './rt-chips-action.types'; import { forwardRef, Fragment, useCallback, useImperativeHandle, useState, } from 'react'; import { RtBaseProps, useRtBaseProps } from '@dpa/react-hoc'; import { RtDropdown, RtDropdownProps } from '@dpa/react-dropdown'; import { ChevronUpTypographyIcon, ChevronDownTypographyIcon, } from '@dpa/react-typography-icons'; import { useGridChild } from '@dpa/react-grid'; const cn = classNames.bind(styles); const cnBaseChips = classNames.bind(stylesBaseChips); export const RtChipsAction = forwardRef< HTMLDivElement, RtBaseProps & RtChipsActionProps >((props, ref) => { const baseProps = useRtBaseProps(props, '0.2.2'); const { defaultLabel, onRemove, slot, disabled, leadingIcon: LeadingIcon, size = 'md', radius = 'primary', modifier, use, labels, isRemovable = false, apiRef, onShowChange, } = props; const [showDropdown, setShowDropdown] = useState(false); const { gridAttributes, gridChildClassname } = useGridChild({ size, radius }); const openDropdown = useCallback(() => { setShowDropdown(true); }, []); const closeDropdown = useCallback(() => { setShowDropdown(false); }, []); useImperativeHandle(apiRef, () => ({ closeDropdown, openDropdown, })); const handleShowChange = useCallback< NonNullable >( (show) => { setShowDropdown(show); onShowChange?.(show); }, [onShowChange] ); const handleRemove = useCallback( (event: React.MouseEvent) => { event.stopPropagation(); setShowDropdown(false); onRemove?.(); }, [onRemove] ); const renderLabels = useCallback(() => { if (!labels?.length) return ( {defaultLabel} ); if (labels.length === 1) { return ( {labels[0]} ); } return labels.map((label, i) => { const isLast = i === labels.length - 1; return ( {label} {!isLast && ( + )} ); }); }, [labels, defaultLabel, size]); const renderChevron = useCallback(() => { if (isRemovable) return; const Icon = showDropdown ? ChevronUpTypographyIcon : ChevronDownTypographyIcon; return ; }, [isRemovable, showDropdown, size]); const renderContent = () => { return (
{LeadingIcon && ( )} {renderLabels()} {renderChevron()} {!disabled && labels?.length && isRemovable ? ( ) : undefined}
); }; return (
{slot}
} closeOnOutsidePress onShowChange={handleShowChange} > {renderContent()}
); }); RtChipsAction.displayName = 'RtChipsAction';