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

88.88% Statements 32/36
80.51% Branches 62/77
83.33% Functions 5/6
93.33% Lines 28/30

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                                      1x                                                 20x   20x 20x 20x   20x 20x     20x   20x 16x 1x       20x 1x 1x   1x 1x     20x 1x 1x     20x 1x 1x     20x         20x           20x   20x   20x                 20x                                                                                                                                                                                                                          
import { FC, useEffect, useRef, useState } from "react";
import {
  Text,
  View,
  TextInput,
  TouchableOpacity,
  NativeSyntheticEvent,
  TextInputFocusEventData,
} from "react-native";
import { cn } from "@sb/libs/utils";
import { Tooltip } from "@sb/ui/components/atoms/Tooltip";
import { TextAreaNativeProps } from "./TextArea.native.types";
import { X } from "@sb/ui/components/atoms/Icons/index.native";
import { useThemeColors } from "@sb/hooks/Utilities/useThemeColors";
import {
  getClearButtonBgColor,
  getTextInputBorderColor,
} from "./TextArea.native.theme";
 
export const TextArea: FC<TextAreaNativeProps> = ({
  id,
  placeholder,
  value,
  defaultValue,
  size = "default",
  disabled = false,
  readOnly = false,
  onChange,
  onFocus,
  onBlur,
  autoFocus = false,
  allowClear = false,
  minLength,
  maxLength,
  showCount = false,
  prefix,
  suffix,
  addonBefore,
  addonAfter,
  status,
  tooltipContent,
  tooltipOptions,
  ...rest
}) => {
  const { theme, themedColors } = useThemeColors();
 
  const inputRef = useRef<TextInput>(null);
  const [isFocused, setIsFocused] = useState(false);
  const rows = size === "small" ? 2 : size === "large" ? 5 : 3;
 
  const isControlled = value !== undefined;
  const [internalValue, setInternalValue] = useState<string>(
    defaultValue || ""
  );
  const inputValue = isControlled ? value : internalValue;
 
  useEffect(() => {
    if (autoFocus && inputRef.current) {
      inputRef.current.focus();
    }
  }, [autoFocus]);
 
  const handleChange = (text: string) => {
    Iif (minLength && text.length < minLength) return;
    Iif (maxLength && text.length > maxLength) return;
 
    Eif (!isControlled) setInternalValue(text);
    Eif (onChange) onChange(text);
  };
 
  const handleFocus = (e: NativeSyntheticEvent<TextInputFocusEventData>) => {
    setIsFocused(true);
    Eif (onFocus) onFocus(e);
  };
 
  const handleBlur = (e: NativeSyntheticEvent<TextInputFocusEventData>) => {
    setIsFocused(false);
    Eif (onBlur) onBlur(e);
  };
 
  const handleClear = () => {
    handleChange("");
    inputRef.current?.focus();
  };
 
  const accessibilityState = {
    disabled,
    selected: isFocused,
    busy: false,
  };
 
  const isDisabledOrReadOnly = disabled || readOnly;
  const clearButtonColor =
    status === "none" ? themedColors.colorPrimary : "white";
 
  const textInputAreaClasses = cn(
    "relative flex-1 rounded-[3px] p-2 text-align-top font-normal placeholder:text-primary",
    {
      "pl-[16%]": prefix,
      "pr-[8%]": !suffix && allowClear,
      "pr-[20%]": suffix && allowClear,
    }
  );
 
  return (
    <Tooltip
      id={id ? id : "Text Area Tooltip"}
      content={tooltipContent}
      {...tooltipOptions}
    >
      <View
        accessible={true}
        accessibilityLabel={id ?? `Text Area Input Field`}
      >
        {addonBefore && <View className="my-4">{addonBefore}</View>}
        <View className="relative flex w-full flex-row items-center overflow-hidden">
          {prefix && (
            <View
              style={{
                backgroundColor: themedColors.colorInput_Disabled_Background,
              }}
              pointerEvents="none"
              accessibilityLabel=""
              className="absolute left-0.5 top-0.5 z-1 flex h-[96%] w-[14%] items-center justify-center rounded-bl-sm rounded-tl-sm"
            >
              {prefix}
            </View>
          )}
          <TextInput
            ref={inputRef}
            style={{
              minHeight: rows * 30,
              color: isDisabledOrReadOnly
                ? themedColors.colorInput_Disabled_Text
                : themedColors.colorPrimary,
              backgroundColor: isDisabledOrReadOnly
                ? themedColors.colorInput_Disabled_Background
                : themedColors.colorInput_Addon_Background,
              borderWidth: isFocused ? 2 : 1,
              borderColor: isDisabledOrReadOnly
                ? themedColors.colorInput_Disabled_Background
                : isFocused
                ? getTextInputBorderColor(theme, status)
                : themedColors.colorInput_Disabled_Background,
            }}
            className={textInputAreaClasses}
            placeholder={placeholder}
            value={inputValue}
            editable={!disabled && !readOnly}
            onChangeText={handleChange}
            onFocus={handleFocus}
            onBlur={handleBlur}
            textAlignVertical="top"
            maxLength={maxLength}
            multiline={true}
            numberOfLines={rows}
            accessibilityLabel={placeholder}
            accessibilityState={accessibilityState}
            accessible={true}
            readOnly={readOnly}
            textBreakStrategy="highQuality"
            lineBreakStrategyIOS="hangul-word"
            {...rest}
          />
          {allowClear && inputValue && !disabled && !readOnly && (
            <TouchableOpacity
              className={cn(
                `absolute right-2 top-2 size-6 items-center justify-center rounded-full`,
                {
                  "right-[16%]": suffix,
                }
              )}
              style={{
                backgroundColor: getClearButtonBgColor(theme, status),
              }}
              onPress={handleClear}
              accessibilityRole="button"
              accessibilityLabel="Clear text area input"
            >
              <X size={8} color={clearButtonColor} />
            </TouchableOpacity>
          )}
          {suffix && (
            <View
              pointerEvents="none"
              style={{
                backgroundColor: themedColors.colorInput_Disabled_Background,
              }}
              className="absolute right-0.5 top-0.5 z-1 flex h-[96%] w-[14%] items-center justify-center rounded-br-sm rounded-tr-sm"
            >
              {suffix}
            </View>
          )}
        </View>
        {addonAfter && <View className="my-4">{addonAfter}</View>}
 
        {showCount && (
          <Text
            style={{
              color: themedColors.colorInput_Disabled_Background,
            }}
            accessibilityLabel={`Text Area Word Count: ${inputValue?.length}/${maxLength}`}
            className="mt-1 text-right text-xs font-normal"
          >
            {maxLength
              ? `${inputValue?.length || 0}/${maxLength}`
              : inputValue?.length}
          </Text>
        )}
      </View>
    </Tooltip>
  );
};