All files / atoms/Keyboard/mobile Keyboard.native.tsx

77.19% Statements 44/57
71.18% Branches 42/59
90.9% Functions 10/11
85.1% Lines 40/47

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                1x                                       14x 14x     14x           14x 14x   14x 14x   14x 14x       14x 1x 1x       14x   4x                       14x   8x   8x     8x 8x 8x 7x 1x 6x 1x 1x 5x 1x 1x 4x       4x   8x   8x                           14x 13x                                                                                             1x               1x                 14x 1x         14x                          
import { cn } from '@sb/libs';
import { View } from 'react-native';
import { Button } from '../../Button/index.native';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import { FC, ReactNode, useCallback, useEffect, useState } from 'react';
import { KeyboardProps, KeySize, Themes } from './Keyboard.native.types';
import { CustomKeyboard, NumericKeyboard, SkeletonLoader } from './Keyboard.native.utils';
 
const Keyboard: FC<KeyboardProps> = ({
  id = 'on-screen-keyboard',
  style,
  onClear,
  onChange,
  onSubmit,
  onKeyPress,
  disabledKeys = [],
  isLoading = false,
  customColorScheme,
  layout = 'numeric',
  value: currentValue,
  showActions = false,
  includeComma = true,
  includeIncrements = false,
  keyColorScheme = 'default',
  keySize: buttonSize = 'medium',
  rows = layout === 'numeric' ? 4 : 2,
  columns = layout === 'numeric' ? 6 : includeIncrements ? 4 : 3,
}) => {
  const { themedColors } = useThemeColors();
  const [keyboardLoading, setKeyboardLoading] = useState<boolean>(isLoading);
 
  // Key Size
  const KEY_SIZE_MAP: Record<KeySize, string> = {
    small: 'h-10',
    medium: 'h-12',
    large: 'h-14',
  };
 
  const keySize = KEY_SIZE_MAP[buttonSize];
  const keyboardTheme = customColorScheme ? customColorScheme : Themes[keyColorScheme];
 
  useEffect(() => {
    const loadingTimeout = setTimeout(() => setKeyboardLoading(false), 2000);
 
    return () => {
      clearTimeout(loadingTimeout);
    };
  }, []);
 
  const handleComma = useCallback((): string => {
    Iif (currentValue === '' || !currentValue) return '';
    Eif (currentValue.includes(',')) return currentValue;
    return currentValue + ',';
  }, [currentValue]);
 
  const concatKeyValue = useCallback(
    (value: string): string => {
      Eif (currentValue === '' || currentValue === '0') return value;
 
      if (currentValue.includes(',')) {
        const afterComma = currentValue.split(',')[1];
        if (afterComma.length > 1) return currentValue;
      }
 
      return currentValue + value;
    },
    [currentValue]
  );
 
  const handleKeyPress = useCallback(
    (keyValue: string) => {
      Iif (disabledKeys?.includes(keyValue)) return;
 
      let newValue = currentValue;
 
      // Modify the value
      Iif (keyValue.startsWith('+')) newValue = keyValue.replace('+', '');
      else Iif (keyValue === '0') newValue = concatKeyValue(keyValue);
      else if (keyValue === ',') newValue = handleComma();
      else if (keyValue === '⌫')
        newValue = currentValue.length > 1 ? currentValue.slice(0, -1) : '';
      else if (keyValue === 'clear') {
        newValue = '';
        onClear?.();
      } else if (keyValue === 'submit') {
        newValue = '';
        onSubmit?.();
      } else Iif (keyValue === 'OK') {
        // Clear input value
        newValue = '';
        onSubmit?.();
      } else newValue = concatKeyValue(keyValue);
 
      onChange(newValue);
 
      Eif (onKeyPress) onKeyPress?.(keyValue);
    },
    [
      currentValue,
      disabledKeys,
      onClear,
      onSubmit,
      onKeyPress,
      onChange,
      handleComma,
      concatKeyValue,
    ]
  );
 
  const RenderKeyboard = (): ReactNode => {
    return (
      <View
        style={{
          borderColor: themedColors.colorSecondary,
        }}
        className="border-[1px] border-b-0"
      >
        {layout === 'custom' ? (
          <CustomKeyboard
            rows={rows}
            columns={columns}
            theme={keyboardTheme}
            keySize={keySize}
            disabledKeys={disabledKeys}
            onKeyPress={handleKeyPress}
            includeComma={includeComma}
            includeIncrements={includeIncrements}
          />
        ) : (
          <NumericKeyboard
            rows={rows}
            columns={columns}
            theme={keyboardTheme}
            includeComma={includeComma}
            disabledKeys={disabledKeys}
            keySize={keySize}
            onKeyPress={handleKeyPress}
          />
        )}
 
        {/* Action Buttons */}
        {showActions && (
          <View
            role="group"
            aria-label="Keyboard actions"
            style={{
              backgroundColor: themedColors.colorPrimary,
            }}
            className="mt-[1px] flex flex-row gap-0.5"
          >
            <Button
              className="flex-1 rounded-none py-3"
              textStyle="uppercase"
              id="keyboard-clear-button"
              variant="secondary"
              label="klar"
              aria-label="Clear input"
              onPress={() => handleKeyPress('clear')}
            />
            <Button
              className="flex-1 rounded-none py-3"
              textStyle="uppercase"
              id="keyboard-submit-button"
              variant="primary"
              label="einreichen"
              onPress={() => handleKeyPress('submit')}
              aria-label="Submit input"
            />
          </View>
        )}
      </View>
    );
  };
 
  const RenderSkeletonLoader = (): ReactNode => {
    return (
      <SkeletonLoader theme={keyboardTheme} keySize={keySize} layout={layout} columns={columns} />
    );
  };
 
  return (
    <View
      id={id}
      testID="on-screen-keyboard"
      className={cn('bg-primary w-full', style)}
      aria-label="on-screen-keyboard"
    >
      {keyboardLoading ? <RenderSkeletonLoader /> : <RenderKeyboard />}
    </View>
  );
};
 
export { Keyboard };