All files / src/ToastMessage ToastMessage.js

71.42% Statements 15/21
39.72% Branches 29/73
40% Functions 2/5
71.42% Lines 15/21

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                              1x                                                       1x                               1x 1x 1x 1x 1x 1x 1x 1x             1x 1x                                                                                                                                                                                                                                                                               1x     1x   1x              
import React, { useState } from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
//components
import { cancelBubblingEffect } from '@zohodesk/components/es/utils/Common';
import { Icon } from '@zohodesk/icons';
import { Container, Box } from '@zohodesk/components/lib/Layout';
import { useZIndex } from '@zohodesk/components/lib/Provider/ZindexProvider';
import { useUniqueId } from '@zohodesk/components/lib/Provider/IdProvider';
import AutoClose from '../actions/AutoClose';
import a11yStyle from '@zohodesk/components/lib/semantic/Button/semanticButton.module.css';
//style
import style from './ToastMessage.module.css';
 
function ToastMessageUI(props) {
  let [messageState, setMessageState] = useState(props.messageState);
  let {
    type,
    subject,
    info,
    submitText,
    cancelText,
    iconName,
    iconSize,
    submitType,
    isIconMid,
    needClose,
    iconPalette,
    isMore,
    message,
    isAnimate,
    getLeftContainer,
    getInnerBottomContainer,
    getBottomContainer,
    getSubmitContainer,
    onSubmit,
    onCancel,
    dataId,
    dataSelectorId,
    isClose,
    onClose,
    needAutoZindex,
    a11y
  } = props;
  function onClickSubmit(e) {
    onSubmit && onSubmit();
    onClose && onClose();
  }
 
  function onClickCancel(e) {
    onCancel && onCancel();
    onClose && onClose();
  }
 
  function changeMessageState(e) {
    cancelBubblingEffect(e);
    setMessageState(!messageState);
  }
 
  let animateClass = isClose ? style.rightanimate : style.leftanimate;
  let animateRemoveClass = isClose ? style.rightanimate : style.leftnoanimate;
  let channelIcon = <Icon iconClass={style[iconPalette]} name={iconName} size={iconSize} />;
  let getNextIndex = useZIndex();
  let Zindex = isAnimate ? getNextIndex() : null,
    getNextId = useUniqueId(),
    titleId = getNextId(),
    infoId = getNextId();
  let {
    role = 'alert',
    ariaLive = 'assertive',
    ariaAtomic = true,
    ariaDescribedby = infoId,
    ariaLabelledby = titleId
  } = a11y;
  return (
    <Container
      className={`${style.container} ${isAnimate ? animateClass : animateRemoveClass} ${
        style[`alert${type}`] ? style[`alert${type}`] : ''
      }`}
      isCover={false}
      dataId={dataId}
      style={needAutoZindex && isAnimate ? { zIndex: `${Zindex}` } : {}}
      role={role}
      aria-live={ariaLive}
      aria-atomic={ariaAtomic}
      aria-describedby={ariaDescribedby}
      aria-labelledby={ariaLabelledby}
      dataSelectorId={dataSelectorId}
    >
      {type !== 'custom' ? (
        <Box className={style.alertifychild} flexible>
          <Container alignBox='row' className={style.alertifybanner}>
            <Box className={style.alertleft} flexible>
              <Container align={isIconMid ? 'vertical' : ''} alignBox='row'>
                {type != 'success' && !getLeftContainer ? (
                  <Box className={`${style.alertico}`}>
                    <span>{channelIcon}</span>
                  </Box>
                ) : getLeftContainer ? (
                  <Box className={`${style.alertico}`}>{getLeftContainer()}</Box>
                ) : null}
                <Box flexible>
                  <Container align='vertical' alignBox='row' isCover={false}>
                    {type == 'success' ? <Box className={style.alertTitleLeft}>{channelIcon}</Box> : null}
                    <Box className={style.alertTitle} flexible data-title={subject} id={titleId}>
                      {subject}
                    </Box>
                  </Container>
                  {info ? (
                    <p className={style.alertMessage} data-title={info} id={infoId}>
                      {info}
                    </p>
                  ) : null}
                  {getInnerBottomContainer ? <div>{getInnerBottomContainer()}</div> : null}
                </Box>
                {type == 'information' && needClose ? (
                  <Box
                    className={`${a11yStyle.buttonReset} ${style.closeIconPrimary}`}
                    onClick={onClose}
                    dataId={`${dataId}_close`}
                    tagName='button'
                  >
                    <Icon isBold name='ZD-close' size='18' />
                  </Box>
                ) : null}
              </Container>
            </Box>
            {type != 'information' ? (
              <Box className={style.alertright}>
                <Container className={style.alerttab}>
                  {needClose ? (
                    <button
                      className={`${a11yStyle.buttonReset} ${style.closeIcon}`}
                      onClick={onClose}
                      data-id={`${dataId}_closeRight`}
                      data-test-id={`${dataId}_closeRight`}
                    >
                      <Icon name='ZD-close' size='18' />
                    </button>
                  ) : null}
                  {submitText ? (
                    <Box
                      className={`${
                        getSubmitContainer ? style.sumbitBorder : cancelText ? style.notSolo : style.mainbutton
                      } ${getSubmitContainer ? '' : style[submitType]} `}
                      flexible
                      onClick={onClickSubmit}
                      dataId={`${dataId}_submitButton`}
                    >
                      <Container align='vertical' alignBox='row'>
                        {getSubmitContainer ? (
                          <Box flexible>{getSubmitContainer()}</Box>
                        ) : (
                          <Box className={`${a11yStyle.buttonReset} ${style.buttonText}`} flexible tagName='button'>
                            {submitText}
                          </Box>
                        )}
                      </Container>
                    </Box>
                  ) : null}
                  {cancelText ? (
                    <Box
                      className={`${style.cancelButton}`}
                      flexible
                      onClick={onClose}
                      dataId={`${dataId}_cancelButton`}
                    >
                      <Container align='vertical' alignBox='row'>
                        <Box
                          className={`${a11yStyle.buttonReset} ${style.buttonText}`}
                          flexible
                          onClick={onClickCancel}
                          dataId={`${dataId}_cancelButtonInner`}
                          tagName='button'
                        >
                          {cancelText}
                        </Box>
                        {isMore ? (
                          <Box
                            className={`${a11yStyle.buttonReset} ${style.dropdown}`}
                            onClick={changeMessageState}
                            dataId={`${dataId}_moreIcon`}
                            tagName='button'
                            aria-expanded={messageState ? true : false}
                            aria-haspopup={true}
                          >
                            <Icon name='ZD-down' size='7' />
                          </Box>
                        ) : null}
                      </Container>
                    </Box>
                  ) : null}
                </Container>
              </Box>
            ) : null}
          </Container>
        </Box>
      ) : null}
      {isMore ? (
        <Box className={`${style.message} ${messageState ? '' : style.messageOff}`} tagName='p'>
          {message}
        </Box>
      ) : null}
 
      {getBottomContainer ? <Box>{getBottomContainer()}</Box> : null}
    </Container>
  );
}
 
export default function ToastMessage(props) {
  return <AutoClose {...props} Element={ToastMessageUI} />;
}
 
ToastMessage.propTypes = propTypes;
 
ToastMessage.defaultProps = defaultProps;
 
// if (__DOCS__) {
//   ToastMessage.docs = {
//     componentGroup: 'Atom'
//   };
// }