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

89.74% Statements 35/39
70.59% Branches 12/17
63.64% Functions 7/11
89.47% Lines 34/38

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                6x 6x   6x                     10x   10x 10x   10x 5x                 5x 5x       9x   5x 5x           10x 10x     24x 5x         10x     10x     10x     10x   10x   10x   10x           10x   10x 10x   10x         10x         10x           10x       10x               10x                                   6x      
// @flow strict
import React, { useEffect, useRef, useImperativeHandle } from 'react';
import cx from 'classnames';
 
import MDCChipSetFoundation from '@material/chips/chip-set/foundation';
import MDCChipFoundation from '@material/chips/chip/foundation';
 
import type { HTMLChipElement } from '../mt-types';
 
type Props = {
  className?: string,
  input?: boolean,
  choice?: boolean,
  filter?: boolean,
};
 
const { strings } = MDCChipSetFoundation;
const chipStrings = MDCChipFoundation.strings;
 
const ChipSet = React.forwardRef<Props, _>(
  (
    {
      className,
      input = false,
      choice = false,
      filter = false,
      ...rest
    }: Props,
    ref,
  ) => {
    const rootRef = useRef<?HTMLDivElement>();
 
    useEffect(() => {
      const root = rootRef.current;
 
      const foundation = new MDCChipSetFoundation({
        hasClass: mClassName => root && root.classList.contains(mClassName),
        // removeChip: chipId => {
        //   const index = this.findChipIndex_(chipId);
        //   if (index >= 0) {
        //     this.chips_[index].destroy();
        //     this.chips_.splice(index, 1);
        //   }
        // },
        setSelected: (chipId, selected) => {
          Eif (root) {
            const chipElements: HTMLChipElement[] = [].slice.call(
              root.querySelectorAll(strings.CHIP_SELECTOR),
            );
 
            const chip = chipElements.find(c => c.id === chipId);
 
            Eif (chip) {
              chip.foundation.setSelected(selected);
            }
          }
        },
      });
 
      Eif (root) {
        [].slice
          .call(root.querySelectorAll(strings.CHIP_SELECTOR))
          .forEach(chip => {
            if (chip.defaultSelected === true) {
              foundation.select(chip.id);
            }
          });
      }
 
      const handleChipInteraction = evt =>
        foundation.handleChipInteraction(evt.detail.chipId);
 
      const handleChipSelection = evt =>
        foundation.handleChipSelection(evt.detail.chipId, evt.detail.selected);
 
      const handleChipRemoval = evt =>
        foundation.handleChipRemoval(evt.detail.chipId);
 
      Eif (root) {
        // $FlowFixMe
        root.addEventListener(chipStrings.SELECTION_EVENT, handleChipSelection);
        // $FlowFixMe
        root.addEventListener(chipStrings.REMOVAL_EVENT, handleChipRemoval);
        // $FlowFixMe
        root.addEventListener(
          chipStrings.INTERACTION_EVENT,
          handleChipInteraction,
        );
      }
 
      foundation.init();
 
      return () => {
        Eif (root) {
          // $FlowFixMe
          root.removeEventListener(
            chipStrings.SELECTION_EVENT,
            handleChipSelection,
          );
          // $FlowFixMe
          root.removeEventListener(
            chipStrings.REMOVAL_EVENT,
            handleChipRemoval,
          );
          // $FlowFixMe
          root.removeEventListener(
            chipStrings.INTERACTION_EVENT,
            handleChipInteraction,
          );
        }
 
        foundation.destroy();
      };
    }, []);
 
    useImperativeHandle(
      ref,
      () => ({
        root: rootRef.current,
      }),
      [],
    );
 
    return (
      <div
        {...rest}
        ref={rootRef}
        className={cx(
          'mdc-chip-set',
          {
            'mdc-chip-set--input': input,
            'mdc-chip-set--choice': choice,
            'mdc-chip-set--filter': filter,
          },
          className,
        )}
      />
    );
  },
);
 
ChipSet.displayName = 'ChipSet';
 
export default ChipSet;