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

85.19% Statements 23/27
60% Branches 12/20
70% Functions 7/10
85.19% Lines 23/27

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                    2x               2x           2x 2x 2x         2x 2x 2x                           4x   4x         2x   2x 2x     2x   2x   2x 2x 2x       2x 2x   2x 2x       2x                      
// @flow strict
import React, { useReducer, useEffect, useRef } from 'react';
import cx from 'classnames';
 
import MDCSelectIconFoundation from '@material/select/icon/foundation';
 
import { mtAttrsReducer } from '../reducers';
 
import type { HTMLSelectIconElement } from '../mt-types';
 
const noOp = () => {};
 
type Props = {
  clickable?: boolean,
  className?: string,
  onAction?: () => void,
};
 
const SelectIcon = ({
  className,
  clickable = false,
  onAction = noOp,
  ...rest
}: Props) => {
  const rootRef = useRef<?HTMLElement>();
  const foundationRef = useRef<?MDCSelectIconFoundation>();
  const [mtAttrs, mtAttrDispatch] = useReducer(mtAttrsReducer, {
    tabIndex: clickable ? 0 : -1,
    role: clickable ? MDCSelectIconFoundation.strings.ICON_ROLE : undefined,
  });
 
  useEffect(() => {
    const foundation = new MDCSelectIconFoundation({
      getAttr: attr => rootRef.current && rootRef.current.getAttribute(attr),
      setAttr: (attr, value) => {
        const name = { tabindex: 'tabIndex' }[attr] || attr;
        mtAttrDispatch({ type: 'set', name, value });
      },
      removeAttr: attr => {
        const name = { tabindex: 'tabIndex' }[attr] || attr;
        mtAttrDispatch({ type: 'unset', name });
      },
      // NOTE.- react will handle this so no need to enable
      // setContent: (content) => {
      //   this.root_.textContent = content;
      // },
      registerInteractionHandler: (evtType, handler) =>
        rootRef.current && rootRef.current.addEventListener(evtType, handler),
      deregisterInteractionHandler: (evtType, handler) =>
        rootRef.current &&
        rootRef.current.removeEventListener(evtType, handler),
    });
 
    // flowlint-next-line unclear-type:off
    const root: HTMLSelectIconElement = (rootRef.current: any);
 
    Eif (root) {
      root.foundation = foundation;
    }
 
    foundation.init();
 
    foundationRef.current = foundation;
 
    return () => {
      foundation.destroy();
      foundationRef.current = null;
    };
  }, []);
 
  useEffect(() => {
    const foundation = foundationRef.current;
 
    Eif (foundation) {
      foundation.adapter_.notifyIconAction = onAction;
    }
  }, [onAction]);
 
  return (
    <i
      {...rest}
      {...mtAttrs}
      ref={rootRef}
      className={cx('mdc-select__icon', className)}
    />
  );
};
 
export default SelectIcon;