All files / src/material-toolbox/radio index.js

92.86% Statements 13/14
66.67% Branches 2/3
75% Functions 3/4
100% Lines 12/12

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                            7x   38x 38x   38x           38x 38x   38x 38x       38x   38x         38x                                               7x      
// @flow strict
import React, { useRef, useMemo, useEffect, useImperativeHandle } from 'react';
import cx from 'classnames';
 
import { useRipple } from '../ripple';
 
import type { Style, HTMLInputElementWithRipple } from '../mt-types';
 
type Props = {
  disabled?: boolean,
  className?: string,
  style?: Style,
};
 
const Radio = React.forwardRef<Props, ?HTMLInputElement>(
  ({ disabled = false, className, style, ...rest }: Props, ref) => {
    const rootRef = useRef<?HTMLDivElement>();
    const radioRef = useRef<?HTMLInputElement>();
 
    const { rippleStyle, rippleClassName, ripple } = useRipple({
      surface: rootRef,
      activator: radioRef,
      unbounded: true,
    });
 
    useEffect(() => {
      Eif (radioRef.current) {
        // flowlint-next-line unclear-type:off
        const radio: HTMLInputElementWithRipple = (radioRef.current: any);
        radio.ripple = ripple;
      }
    }, [ripple]);
 
    useImperativeHandle(ref, () => radioRef.current, []);
 
    const memoizedStyle = useMemo(() => ({ ...style, ...rippleStyle }), [
      rippleStyle,
      style,
    ]);
 
    return (
      <div
        style={memoizedStyle}
        ref={rootRef}
        className={cx('mdc-radio', className, ...Array.from(rippleClassName), {
          'mdc-radio--disabled': disabled,
        })}
      >
        <input
          ref={radioRef}
          {...(rest: mixed)}
          type="radio"
          className="mdc-radio__native-control"
          disabled={disabled}
        />
        <div className="mdc-radio__background">
          <div className="mdc-radio__outer-circle" />
          <div className="mdc-radio__inner-circle" />
        </div>
      </div>
    );
  },
);
 
Radio.displayName = 'Radio';
 
export default Radio;