All files / src/material-toolbox/textfield CharacterCounter.js

100% Statements 16/16
50% Branches 2/4
100% Functions 5/5
100% Lines 15/15

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                  8x       17x   17x 9x   8x 8x         9x     9x   9x 9x     9x 9x         17x   17x     8x      
// @flow strict
import React, { useEffect, useRef, useImperativeHandle } from 'react';
 
import MDCTextFieldCharacterCounterFoundation from '@material/textfield/character-counter/foundation';
 
import type { HTMLTextFieldCharacterCounterElement } from '../mt-types';
 
type Props = {};
 
const TextFieldCharacterCounter = React.forwardRef<
  Props,
  HTMLTextFieldCharacterCounterElement,
>((props: Props, ref) => {
  const rootRef = useRef<?HTMLDivElement>();
 
  useEffect(() => {
    const foundation = new MDCTextFieldCharacterCounterFoundation({
      setContent: content => {
        Eif (rootRef.current) {
          rootRef.current.textContent = content;
        }
      },
    });
 
    foundation.init();
 
    // flowlint-next-line unclear-type:off
    const root: HTMLTextFieldCharacterCounterElement = (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="mdc-text-field-character-counter" />;
});
 
TextFieldCharacterCounter.displayName = 'TextFieldCharacterCounter';
 
export default TextFieldCharacterCounter;