All files / src/material-toolbox/notched-outline index.js

90% Statements 18/20
50% Branches 3/6
75% Functions 6/8
89.47% Lines 17/19

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                                          10x   71x 71x 71x         71x 36x       1x         1x       36x     36x   36x 36x     36x 36x         71x   71x                                           10x      
// @flow strict
import React, {
  useRef,
  useImperativeHandle,
  useEffect,
  useReducer,
} from 'react';
import cx from 'classnames';
 
import MDCNotchedOutlineFoundation from '@material/notched-outline/foundation';
 
import type { Node } from 'react';
 
import { mtClassNamesReducer } from '../reducers';
 
import type { HTMLNotchedOutlineElement } from '../mt-types';
 
type Props = {
  children?: Node,
};
 
const NotchedOutline = React.forwardRef<Props, HTMLNotchedOutlineElement>(
  ({ children }: Props, ref) => {
    const rootRef = useRef<?HTMLDivElement>();
    const notchElementRef = useRef<?HTMLDivElement>();
    const [mtClassName, mtClassNameDispatch] = useReducer(
      mtClassNamesReducer,
      new Set<string>(),
    );
 
    useEffect(() => {
      const foundation = new MDCNotchedOutlineFoundation({
        addClass: mClassName =>
          mtClassNameDispatch({ type: 'add', className: mClassName }),
        removeClass: mClassName =>
          mtClassNameDispatch({ type: 'remove', className: mClassName }),
        setNotchWidthProperty: width =>
          notchElementRef.current &&
          notchElementRef.current.style.setProperty('width', `${width}px`),
        removeNotchWidthProperty: () =>
          notchElementRef.current &&
          notchElementRef.current.style.removeProperty('width'),
      });
 
      foundation.init();
 
      // flowlint-next-line unclear-type:off
      const root: HTMLNotchedOutlineElement = (rootRef.current: any);
 
      Eif (root) {
        root.foundation = foundation;
      }
 
      return () => {
        foundation.destroy();
      };
    }, []);
 
    // flowlint-next-line unclear-type:off
    useImperativeHandle(ref, () => (rootRef.current: any), []);
 
    return (
      <div
        ref={rootRef}
        className={cx(
          'mdc-notched-outline',
          'mdc-notched-outline--upgraded',
          {
            'mdc-notched-outline--no-label': !children,
          },
          ...Array.from(mtClassName),
        )}
      >
        <div className="mdc-notched-outline__leading" />
        <div ref={notchElementRef} className="mdc-notched-outline__notch">
          {children}
        </div>
        <div className="mdc-notched-outline__trailing" />
      </div>
    );
  },
);
 
NotchedOutline.displayName = 'NotchedOutline';
 
export default NotchedOutline;