import * as React from 'react'; import { TextInput, inputType } from '../../core'; import { getWidthByText, isEmpty } from '../../utility'; import { MultiSelectChipStyles } from '../MultiSelect/MultiSelectChips'; import { Chip } from './Chips'; import { Button } from '../../core'; import { FontAwesomeIcon as Icon } from '@fortawesome/react-fontawesome'; import { useRef } from 'react'; import { faTimes, faSearch } from '@fortawesome/free-solid-svg-icons'; import { keyCode } from '../../utility/Constants'; interface IMultiSelectSearchInputProps { id: string; selectedItems: any[]; searchVal: string; primaryChip?: number; onChange: (event: any) => void; onCrossIconPressed: (event: any) => void; onChipRightClick?: (event: any, label: any, value: any, isPrimary: boolean) => void; onChipCrossButtonClick: (value: any) => void; isDisabled?: boolean; isPartiallyDisabled?: boolean; onMoreButtonClick: () => void; placeholder?: string; autoFocus?: boolean; } const getChipWidth = (text: string) => { return Math.ceil( getWidthByText(text, `${MultiSelectChipStyles.fontSize} ${MultiSelectChipStyles.fontFamily}`) + MultiSelectChipStyles.chipMargin + MultiSelectChipStyles.chipPadding ); }; export function MultiSelectSearchInput({ selectedItems, onChange, searchVal, onCrossIconPressed, id, onChipRightClick, primaryChip, onChipCrossButtonClick, isDisabled, onMoreButtonClick, placeholder, isPartiallyDisabled, autoFocus = false }: IMultiSelectSearchInputProps): JSX.Element { const inputRef = useRef(null); const getSelectedItems = () => { let chipContainerWidth = 0; const allSelectedItems: any = []; const multiSelectSearchButton = document.getElementById('multiselect-search-selector'); const multiSelectSearchButtonWidth = multiSelectSearchButton ? multiSelectSearchButton.clientWidth : 200; selectedItems.forEach(({ label, value, disabled, deactivated }: any) => { chipContainerWidth += getChipWidth(label); if ( multiSelectSearchButtonWidth && chipContainerWidth + getChipWidth(MultiSelectChipStyles.moreChipLabel) < multiSelectSearchButtonWidth ) allSelectedItems.push( ); }); return allSelectedItems; }; const getMoreItemCount = () => { return selectedItems.length - getSelectedItems().length; }; const onInputClick = () => { if (inputRef.current) { inputRef.current.focus(); } }; const moreChipAction = (event: any) => { event.stopPropagation(); onMoreButtonClick(); }; return (
{getSelectedItems()}
{getMoreItemCount() <= 0 ? ( <> ) : (
{ moreChipAction(e); }} onKeyDown={e => { if (e.keyCode === keyCode.Enter) moreChipAction(e); }} > +{getMoreItemCount()} More
)}
{!isDisabled ? (
) : null}
{!isEmpty(selectedItems) && !isPartiallyDisabled ? ( ) : ( )}
); }