import React, { ComponentProps } from 'react'; import { TextInput, TextInputProps, View } from 'react-native'; import { Icon, SemanticColor, Typo, TypoIncludingEmoji } from '../../foundation'; import { isIos } from '../../internal/utils/functions'; import { unicodeAwareLength, unicodeAwareSubstring } from '../../internal/utils/stringUtils'; import { formatNumber } from '../../internal/utils/numberUtils'; import { TextAreaCounterStates, TextAreaStates } from './types'; import { LayoutStyleProp } from '../../internal/utils/utilTypes'; type Props = { state: TextAreaStates; variants: 'height_120' | 'height_200'; value: string; label?: string; requiredMark?: boolean; // label 없으면 true 전달이 무의미함 placeholder?: string; description?: string; counter?: | { type: 'max'; limit: number; } | { type: 'min'; limit: number; state: TextAreaCounterStates; }; } & LayoutStyleProp; type AllowedTextInputProps = Omit< TextInputProps, | 'multiline' | 'placeholderTextColor' | 'placeholder' | 'value' | 'style' // onEndEditing 이벤트는 onBlur 이벤트로 대체 가능 (경험상 두 이벤트는 기능구현시 유의미한 차이가 없음) // (참고) https://stackoverflow.com/a/40851395 오래된 답변이지만 아직 유효함 | 'onEndEditing' // 이모지를 포함한 텍스트의 글자 수 세기, 글자수에 기반한 검사 구현을 JS 레벨에서 구현해야 하므로 maxLength 사용을 제한함 | 'maxLength' >; const HEIGHT: Record = { height_120: 120, height_200: 200, }; const TYPO_BODY_16_REGULAR = { fontFamily: 'Pretendard-Regular', fontSize: 16, // HACK: iOS) placeholder 상단에 여백만 생기고, 정작 텍스트 입력 후엔 line height 적용 안됨 lineHeight: isIos() ? undefined : 24, }; // maxLength 미지정시 기본 한도 (개발자 임의 지정. 기본 한도가 없으면 너무 긴 문자열을 입력 후 제출해 서버에 문제를 일으킬 수 있음) const DEFAULT_MAX_LENGTH = 1_000; /** * # TextArea * * multi line 입력 * * - 이 컴포넌트는 디자인이 입혀진 UI 요소를 제공하는 책임만 가지며, 실제 동작은 사용처에서 구현해야 합니다. * (최대 입력 가능한 글자 수 제한 기능만 내장되어 있습니다.) * - RN `TextInput` 컴포넌트 Prop 대부분 사용 가능 * * ## Props * * ### `state` * - 상황에 따른 UI 상태 변화를 나타내는데 사용합니다. **동작은 컴포넌트 사용처에서 직접 구현해야 합니다.** * - `TextAreaStates` 유니언 타입 * - `'enabled' | 'focused' | 'error' | 'disabled' | 'readonly'` * * ### `variants` * - `'height_120'` 텍스트 영역 높이가 120dp * - `'height_200'` 텍스트 영역 높이가 200dp * * ### `value` * - 인풋에 입력된 텍스트 * * ### `label` (optional) * - 인풋 상단에 붙는 제목 * * ### `requiredMark` (optional) * - 필수 항목임을 나타내는 표시를 label 우측에 표시합니다. * * ### `placeholder` (optional) * - 인풋에 입력되지 않은 상태일 때 보여지는 안내 문구 * * ### `description` (optional) * - 인풋 하단에 보여지는 안내 문구 * * ### `counter` (optional) * - 인풋 하단에 보여지는 글자 수 카운터 * - `type: 'max'` 최대 글자 수 제한. (글자 수 계산시 이모지도 1글자로 계산됩니다.) * - `limit` 최대 글자 수 * - `type: 'min'` 최소 글자 수 제한. * - `limit` 최소 글자 수 * - `state` : 검사 상태. **입력값에 따른 상태 변경은 동작은 사용처에서 직접 구현해야 합니다.** * - `TextAreaCounterStates` 유니언 타입 - `'disabled' | 'success' | 'error'` * * * @example * ```tsx * // 최대 길이 제한이 있는 필수 입력 항목 * // ❗️ 조건문 구성, 동작 구현 등은 실제 화면 요구사항에 맞추어 직접 구현해야 합니다. (아래 구현은 컴포넌트 사용을 위한 참고 목적) * function ExampleTextArea() { * const MAX_LENGTH = 500; * const REQUIRED = true; * * const [text, setText] = useState(''); * const [isFocusedOnce, setIsFocusedOnce] = useState(false); * const [isFocused, setIsFocused] = useState(false); * * const inputState = ((): TextAreaStates => { * if (isFocused) return 'focused'; * if (!isFocusedOnce) return 'enabled'; * if (REQUIRED && isEmpty(text)) return 'error'; * return 'enabled'; * })(); * * const description = (() => { * if (REQUIRED && !isFocused && isFocusedOnce && isEmpty(text)) return '반드시 내용을 작성해주세요.'; * return '신뢰감을 주는 설명을 작성해주세요.'; * })(); * * return ( *