All files / src/material-toolbox/chip Icon.js

81.82% Statements 9/11
85.71% Branches 6/7
50% Functions 2/4
81.82% Lines 9/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                          5x 15x 15x         15x   15x   15x 15x   15x         15x                                        
// @flow strict
import React, { useEffect, useRef, useReducer } from 'react';
import cx from 'classnames';
 
import { mtClassNamesReducer } from '../reducers';
 
import type { HTMLChipIconElement } from '../mt-types';
 
type Props = {
  className?: string,
  trailing?: boolean,
};
 
const ChipIcon = ({ className, trailing = false, ...rest }: Props) => {
  const rootRef = useRef();
  const [mtClassName, mtClassNameDispatch] = useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  useEffect(() => {
    // flowlint-next-line unclear-type:off
    const root: HTMLChipIconElement = (rootRef.current: any);
 
    Eif (root) {
      root.mtAddClassName = mClassName =>
        mtClassNameDispatch({ type: 'add', className: mClassName });
      root.mtRemoveClassName = mClassName =>
        mtClassNameDispatch({ type: 'remove', className: mClassName });
    }
  }, []);
 
  return (
    <i
      {...rest}
      ref={rootRef}
      tabIndex={trailing ? 0 : undefined} // eslint-disable-line jsx-a11y/no-noninteractive-tabindex
      role={trailing ? 'button' : undefined}
      className={cx(
        'mdc-chip__icon',
        {
          'mdc-chip__icon--leading': !trailing,
          'mdc-chip__icon--trailing': trailing,
        },
        ...Array.from(mtClassName),
        className,
      )}
    />
  );
};
 
export default ChipIcon;