All files / molecules/DialogBox/mobile DialogBox.native.tsx

82.5% Statements 33/40
75.8% Branches 47/62
90% Functions 9/10
81.57% Lines 31/38

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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                  5x                                                           31x     2x 1x 1x 1x     1x     31x 30x 1x 1x     1x 1x         31x 31x 21x     31x 25x   19x                                                             19x 1x                                 18x 1x           1x 1x               17x                                 31x                           31x 22x                   22x       31x                                                                                                                                                                            
import { cn } from '@sb/libs/utils';
import { FC, ReactNode, useEffect } from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { ModalShadows, getDialogBoxTitleColor } from './utils';
import { X } from '@sb/ui/components/atoms/Icons/index.native';
import { DialogBoxNativeProps } from './DialogBox.native.types';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import { Modal, Pressable, Text, TouchableWithoutFeedback, View } from 'react-native';
 
export const DialogBox: FC<DialogBoxNativeProps> = ({
  id,
  icon,
  style,
  title,
  isOpen,
  onClose,
  content,
  onCancel,
  onConfirm,
  className,
  headerStyle,
  footerStyle,
  headerTemplate,
  footerTemplate,
  size = 'default',
  closable = true,
  autoCloseTimeout,
  cancelButtonLabel,
  confirmButtonLabel,
  animation = 'none',
  variant = 'default',
  footerTemplateOutside,
  paddingSize = 'default',
  addContentSpacing = true,
  showCloseIcon = closable,
  showFooterButtons = true,
  backdropClickClose = closable,
  ...rest
}) => {
  const { theme, themedColors } = useThemeColors();
 
  function onClosableClick() {
    if (onCancel) {
      onCancel();
      onClose();
      return;
    }
 
    onClose();
  }
 
  useEffect(() => {
    if (autoCloseTimeout) {
      const timeout = setTimeout(() => {
        onClose();
      }, autoCloseTimeout);
 
      return () => {
        clearTimeout(timeout);
      };
    }
  }, []);
 
  const RenderModalContent = (): ReactNode => {
    if (typeof content === 'string') return <Text className="my-2 font-normal">{content}</Text>;
    else return content;
  };
 
  const RenderModalFooter = (): ReactNode => {
    if (footerTemplate) return footerTemplate;
 
    Iif (onCancel && onConfirm) {
      return (
        <>
          <Pressable
            style={{ backgroundColor: themedColors.colorAccent }}
            onPress={onClosableClick}
            accessibilityLabel="Cancel Button"
            className="mt-1.5 flex-1 rounded-md p-2.5"
          >
            <Text style={{ color: themedColors.colorPrimary }} className="font-Bold text-center">
              {cancelButtonLabel ?? 'Cancel'}
            </Text>
          </Pressable>
 
          <Pressable
            style={{ backgroundColor: themedColors.colorPrimary }}
            accessibilityLabel="Confirm Button"
            className="mt-1.5 flex-1 rounded-md p-2.5"
            onPress={() => {
              onConfirm();
              onClose();
            }}
          >
            <Text style={{ color: themedColors.colorText_Main }} className="font-Bold text-center">
              {confirmButtonLabel ?? 'Confirm'}
            </Text>
          </Pressable>
        </>
      );
    }
 
    if (onCancel) {
      return (
        <Pressable
          onPress={onClosableClick}
          className="mt-1.5 flex-1 rounded-md px-2.5 py-3"
          style={{ backgroundColor: themedColors.colorPrimary }}
          accessibilityLabel={cancelButtonLabel ?? 'Cancel Button'}
        >
          <Text
            style={{ color: themedColors.colorText_Main }}
            className="text-center text-lg font-semibold"
          >
            {cancelButtonLabel ?? 'Cancel'}
          </Text>
        </Pressable>
      );
    }
 
    if (onConfirm) {
      return (
        <Pressable
          accessibilityLabel={confirmButtonLabel ?? 'Confirm Button'}
          style={{ backgroundColor: themedColors.colorStatus_Success }}
          className="mt-1.5 flex-1 rounded-md p-2.5"
          onPress={() => {
            onConfirm();
            onClose();
          }}
        >
          <Text className="text-center font-semibold">{confirmButtonLabel ?? 'Confirm'}</Text>
        </Pressable>
      );
    }
 
    return closable ? (
      <Pressable
        onPress={onClosableClick}
        style={{ backgroundColor: themedColors.colorPrimary }}
        accessibilityLabel={cancelButtonLabel ?? 'Cancel Button'}
        className="mt-1.5 flex-1 rounded-md px-2.5 py-3"
      >
        <Text
          style={{ color: themedColors.colorText_Main }}
          className="text-center text-lg font-semibold"
        >
          {cancelButtonLabel ?? 'Close'}
        </Text>
      </Pressable>
    ) : null;
  };
 
  const ModalClasses = cn(
    'relative rounded-xl bg-white elevation-10',
    {
      'w-[60%]': size === 'small',
      'w-[80%]': size === 'default',
      'w-[90%]': size === 'large',
      'p-0': paddingSize === 'none',
      'p-2': paddingSize === 'small',
      'p-3.5': paddingSize === 'default',
      'p-5': paddingSize === 'large',
    },
    className
  );
 
  const closeButtonColor = (): string => {
    switch (variant) {
      case 'error':
        return themedColors.toastError;
      case 'info':
        return themedColors.colorPrimary;
      case 'success':
        return themedColors.toastSuccess;
      case 'warning':
        return themedColors.toastWarning;
      default:
        return themedColors.colorPrimary;
    }
  };
 
  return (
    <Modal
      id={id}
      style={style}
      visible={isOpen}
      transparent={true}
      onRequestClose={onClose}
      animationType={animation}
    >
      <Pressable
        testID="dialog-box-overlay"
        onPress={backdropClickClose ? onClose : undefined}
        style={{ backgroundColor: themedColors.shadowDark }}
        className={cn('h-full flex-1 items-center justify-center')}
      >
        <TouchableWithoutFeedback accessible={true} {...rest}>
          <SafeAreaView className={ModalClasses} style={{ ...ModalShadows[variant] }}>
            {showCloseIcon && (
              <Pressable
                className={cn(
                  `absolute right-3 top-3 z-10 flex size-6 items-center justify-center rounded-full`
                )}
                onPress={onClose}
                testID="close-icon"
                style={{
                  opacity: 0.75,
                  borderWidth: 1,
                  borderRadius: 50,
                  borderColor: closeButtonColor(),
                }}
                accessibilityLabel={id ? `Close ${id} Dialog Box` : 'Close Dialog Box'}
              >
                <X size={8} color={closeButtonColor()} />
              </Pressable>
            )}
 
            {headerTemplate ? (
              <View accessibilityLabel="Dialog Box Title" className={headerStyle}>
                {headerTemplate}
              </View>
            ) : (
              <View
                accessibilityLabel="Dialog Box Title"
                className={cn('flex flex-row gap-4', headerStyle)}
              >
                {icon && <View className="flex flex-col justify-center">{icon}</View>}
                <Text
                  style={{ color: getDialogBoxTitleColor(theme, variant) }}
                  className="font-Bold text-xl"
                >
                  {title}
                </Text>
              </View>
            )}
 
            <View
              accessibilityLabel="Dialog Box Content"
              className={cn({
                'my-1.5': addContentSpacing,
              })}
            >
              {<RenderModalContent />}
            </View>
 
            {showFooterButtons && (
              <View
                accessibilityLabel="Dialog Box Footer"
                className={cn('flex flex-row gap-2', footerStyle)}
              >
                <RenderModalFooter />
              </View>
            )}
            {footerTemplateOutside && (
              <View
                accessibilityLabel="External Dialog Box Footer"
                className={cn('mt-5', footerTemplateOutside)}
              >
                {footerTemplateOutside}
              </View>
            )}
          </SafeAreaView>
        </TouchableWithoutFeedback>
      </Pressable>
    </Modal>
  );
};