import React, { ComponentProps, useState } from 'react'; import { Pressable, TextInput, View } from 'react-native'; import { TextField } from './TextField'; import { Typo } from '../../foundation'; import { NativeSyntheticEvent } from 'react-native/Libraries/Types/CoreEventTypes'; import { TextInputFocusEventData } from 'react-native/Libraries/Components/TextInput/TextInput'; import { isNil } from '../../internal/utils/functions'; type SpecifiedTextFieldProps = Omit< ComponentProps, | 'currencyInput' | 'currencyInputHitSlop' | 'RightButtonComponent' | 'RightSlotComponent' // 컴포넌트 사용처에서는 maxLength 만 알면 됨. 내부적으로는 maxLengthNative 사용 | 'maxLengthNative' // 컴포넌트 내부에서 처리함 | 'secureTextEntry' // 비밀번호 입력에 글자수 카운터는 불필요함 | 'showCounter' >; type PasswordSpecificProps = { formatter?: (text: string) => string; }; // maxLength 미지정시 기본 한도 (개발자 임의 지정. 기본 한도가 없으면 너무 긴 문자열을 입력 후 제출해 서버에 문제를 일으킬 수 있음) const DEFAULT_MAX_LENGTH = 30; /** * # TextFieldPassword * * 비밀번호 입력 용도로 특화된 TextField 컴포넌트 * * - 이 컴포넌트는 디자인이 입혀진 UI 요소를 제공하는 책임만 가지며, 실제 동작은 사용처에서 구현해야 합니다. * (최대 입력 가능한 글자 수 제한, 표시/숨김 기능이 내장되어 있습니다.) * * * ## Props * * 여기에 기재하지 않은 RN `TextInput` 컴포넌트 Prop 대부분 사용 가능 (자동완성에 나타나지 않으면 사용 불가한 것입니다.) * * ### `state` (required) * 이 상태의 변경은 컴포넌트 사용처에서 요구사항에 따라 구현해야 합니다. (현재 일관된 제품 정책이 없고 다양한 요구사항이 존재하여 컴포넌트 내부적으로 처리할 수 없음) * - `'enabled'`: 보통 상태 * - `'focused'`: 테두리에 강조표시 * - `'error'`: 테두리 및 디스크립션에 빨간색 강조표시 * - `'disabled'`: 비활성 상태 (터치, 입력 불가) * * * ### `value` (required) * 입력된 문자열 * * * ### `label` * 입력 필드의 이름 (좌 상단에 붙음) * * * ### `requiredMark` * label 뒤에 '*' 표시하려면 true 전달 * (label 없으면 보이지 않음) * * * ### `placeholder` * 입력값이 없을 때 입력 필드 내에 보여줄 문자열 * * * ### `description` * 입력 필드 하단에 보여줄 설명글 * (error 상태일 때는 빨간색으로 표시됨. 그 외엔 회색) * * * ### `maxLength` * 비밀번호로 입력 가능한 최대 글자 수 (기본값 30) * * * ### `clearButton` * 입력 필드 우측에 입력값을 한번에 지우는 클리어 버튼 표시 여부 (기본값 true) * - '인풋에 포커스돼있고, 입력값이 있을 때' 표시됩니다. ('focused' state 와 무관하여, 'error' 상태 스타일일 때도 표시됩니다.) * * * ### `layoutStyle` * 컴포넌트 배치 변경에 사용할 수 있는 ViewStyle 일부 * * * ### `formatter` * 입력값을 변형하기 위한 함수 (onChangeText 이벤트에서 호출) * - 예를들어, 사용자가 이모지를 입력할 수 없도록 String.replace 메서드와 정규식으로 이모지를 제거하여 리턴할 수 있습니다. * - 기본적으로는 입력값 제한이나 변형을 하지 않습니다. * * * ## Example * 컴포넌트의 사용 방법은 RN TextInput 컴포넌트와 유사하지만 * `state` prop 값을 요구사항에 맞춰 결정할 수 있도록 직접 구현해야 합니다. onFocus, onBlur 이벤트를 활용하세요. * * (예시 코드) * https://github.com/Soomgo-Mobile/ui/blob/master/example/components/Component/TextField/examples/TextFieldPasswordExample.tsx */ export const TextFieldPassword = React.forwardRef( (props, ref): React.JSX.Element => { const [isSecured, setIsSecured] = useState(true); const [isEditing, setIsEditing] = useState(false); function handleChangeText(text: string) { const formattedText = isNil(props.formatter) ? text : props.formatter(text); props.onChangeText?.(formattedText); } function handleFocus(e: NativeSyntheticEvent) { setIsEditing(true); props.onFocus?.(e); } function handleBlur(e: NativeSyntheticEvent) { setIsEditing(false); props.onBlur?.(e); } return ( setIsSecured((v) => !v)} /> } onChangeText={handleChangeText} onFocus={handleFocus} onBlur={handleBlur} autoComplete={'password'} maxLengthNative={props.maxLength ?? DEFAULT_MAX_LENGTH} /> ); }, ); function ShowOrHideButton({ isSecured, onPress }: { isSecured: boolean; onPress: () => void }) { return ( {isSecured ? '표시' : '숨김'} ); }