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

71.43% Statements 15/21
50% Branches 3/6
33.33% Functions 3/9
75% Lines 15/20

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                                          2x   4x 4x         4x   4x 4x                               4x     4x   4x 4x     4x 4x         4x   4x                               2x      
// @flow strict
import React, {
  useReducer,
  useImperativeHandle,
  useEffect,
  useRef,
} from 'react';
import cx from 'classnames';
 
import MDCSelectHelperTextFoundation from '@material/select/helper-text/foundation';
 
import { mtClassNamesReducer, mtAttrsReducer } from '../reducers';
 
import type { HTMLSelectHelperTextElement } from '../mt-types';
 
type Props = {
  text: string,
  persistent?: boolean,
  validation?: boolean,
};
 
const SelectHelperText = React.forwardRef<Props, HTMLSelectHelperTextElement>(
  ({ text, persistent = false, validation = false, ...rest }: Props, ref) => {
    const rootRef = useRef<?HTMLParagraphElement>();
    const [mtClassName, mtClassNameDispatch] = useReducer(
      mtClassNamesReducer,
      new Set<string>(),
    );
 
    const [mtAttrs, mtAttrsDispatch] = useReducer(mtAttrsReducer, {});
 
    useEffect(() => {
      const foundation = new MDCSelectHelperTextFoundation({
        addClass: mClassName =>
          mtClassNameDispatch({ type: 'add', className: mClassName }),
        removeClass: mClassName =>
          mtClassNameDispatch({ type: 'remove', className: mClassName }),
        hasClass: className =>
          rootRef.current && rootRef.current.classList.contains(className),
        setAttr: (attr, value) =>
          mtAttrsDispatch({ type: 'set', name: attr, value }),
        removeAttr: attr => mtAttrsDispatch({ type: 'unset', name: attr }),
        // NOTE.- next is not needed already
        // setContent: (content) => {
        //   this.root_.textContent = content;
        // },
      });
 
      foundation.init();
 
      // flowlint-next-line unclear-type:off
      const root: HTMLSelectHelperTextElement = (rootRef.current: any);
 
      Eif (root) {
        root.foundation = foundation;
      }
 
      return () => {
        foundation.destroy();
      };
    }, []);
 
    // flowlint-next-line unclear-type:off
    useImperativeHandle(ref, () => (rootRef.current: any), []);
 
    return (
      <p
        {...rest}
        {...mtAttrs}
        className={cx('mdc-select-helper-text', ...Array.from(mtClassName), {
          'mdc-select-helper-text--persistent': persistent,
          'mdc-select-helper-text--validation-msg': validation,
        })}
        ref={rootRef}
      >
        {text}
      </p>
    );
  },
);
 
SelectHelperText.displayName = 'SelectHelperText';
 
export default SelectHelperText;