All files / src/actions AutoClose.js

56.25% Statements 9/16
16.66% Branches 1/6
40% Functions 2/5
56.25% Lines 9/16

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            2x 2x         2x   2x           2x           2x 2x     4x 4x  
import React, { useState, useEffect } from 'react';
import { cancelBubblingEffect } from '@zohodesk/components/es/utils/Common';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
export default function AutoClose(props) {
  let { Element, isClose: givenClose, onPortalClose, autoClose, hideTime } = props;
  let [isClose, setClose] = useState(givenClose);
  function onClose(e) {
    cancelBubblingEffect(e);
    onPortalClose && onPortalClose();
  }
  useEffect(() => {
    let id;
    Iif (autoClose) {
      id = setTimeout(() => {
        setClose(true);
        setTimeout(onClose, 300); // For animation
      }, hideTime);
    }
    return () => {
      if (autoClose) {
        clearTimeout(id);
      }
    };
  }, []);
  let newProps = Object.assign({}, props, { isClose, onClose });
  return <Element {...newProps} />;
}
 
AutoClose.propTypes = propTypes;
AutoClose.defaultProps = defaultProps;