import React, { ComponentRef } from 'react'; import { View, StyleSheet, Pressable } from 'react-native'; import { Icon, SemanticColor, Typo } from '../../foundation'; import { LayoutStyleProp } from '../../internal/utils/utilTypes'; /** * `SelectField` 컴포넌트의 상태 타입 정의 * * - `'enabled'`: 보통 상태 * - `'focused'`: 필드를 눌렀을 때 (바텀시트 등을 열었을 때) * - `'error'`: 에러 상태 (필수 입력인데 입력 안했거나, 잘못된 값이 들어갔을 때, ..) * - `'disabled'`: 입력 비활성 * - `'readOnly'`: 읽기 전용 (입력은 불가하지만, 선택된 값이 있을 때) */ export type SelectFieldStates = 'enabled' | 'focused' | 'error' | 'disabled' | 'readOnly'; type Props = { state: SelectFieldStates; label?: string; showRequiredMark?: boolean; text?: string | null; placeholder?: string; onPress?: () => void; description?: string; } & LayoutStyleProp; /** * ## SelectField * * 액션시트, 바텀시트, 휠 피커 등의 선택형 인풋 UI와 함께 사용될 수 있습니다. * * * ## Props * * * ### `state` (required) * - 이 상태의 변경은 컴포넌트 사용처에서 요구사항에 따라 구현해야 합니다. * - `'enabled'`: 보통 상태 * - `'focused'`: 필드를 눌렀을 때 (바텀시트 등을 열었을 때) * - `'error'`: 에러 상태 (필수 입력인데 입력 안했거나, 잘못된 값이 들어갔을 때, ..) * - `'disabled'`: 입력 비활성 * - `'readOnly'`: 읽기 전용 (입력은 불가하지만, 선택된 값이 있을 때) * * * ### `label` * - 입력 필드의 이름 (좌 상단에 붙음) * * * ### `showRequiredMark` * - label 뒤에 '*' 표시하려면 true 전달 * * * ### `text` * - 선택한 값에 해당하는 문자열 * * * ### `placeholder` * - 입력값이 없을 때(text 값이 falsy) 입력 필드 내에 보여줄 문자열 * * * ### `onPress` * - 필드를 눌렀을 때 호출되는 함수 * * * ### `description` * - 입력 필드 하단에 보여줄 설명글 * - 에러 상태일 때 빨간색으로 변합니다. * * * ### `style` * - 컴포넌트 레이아웃 조정에 사용할 수 있습니다. 하지만 디자인 수정 용도로는 사용하지 마세요. * * * * ## 사용 예시 * * https://github.com/Soomgo-Mobile/ui/tree/master/example/components/Component/SelectField/SelectFieldStory.tsx * */ export const SelectField = React.forwardRef, Props>((props, ref) => { const isError = props.state === 'error'; const isFocused = props.state === 'focused'; const isDisabled = props.state === 'disabled'; const isReadOnly = props.state === 'readOnly'; const isEditable = !(isDisabled || isReadOnly); const borderColor = (() => { if (!isEditable) return SemanticColor.border_secondary; if (isError) return SemanticColor.state_text_error; if (isFocused) return SemanticColor.border_active; return SemanticColor.border_secondary; })(); const backgroundColor = isEditable ? SemanticColor.bg_primary : SemanticColor.bg_off_white; const hasSelectedItem = !!props.text; const fieldTextColor = (() => { if (isDisabled) return 'text_tertiary'; if (isError) return 'text_primary'; if (hasSelectedItem) return 'text_primary'; return 'text_tertiary'; })(); const chevronIcon = isFocused ? 'chevron_up_outlined' : 'chevron_down_outlined'; const chevronIconColor = isEditable ? 'text_tertiary' : 'text_quaternary'; const descriptionTextColor = isError ? 'state_text_error' : 'text_tertiary'; function handlePress() { if (!isEditable) return; props.onPress?.(); } return ( {/* 라벨 */} {props.label ? ( {props.label} {props.showRequiredMark ? ( {'*'} ) : null} ) : null} {/* 필드 */} {props.text ? ( {props.text} ) : null} {!props.text && props.placeholder ? ( {props.placeholder} ) : null} {/* 디스크립션 */} {props.description ? ( {props.description} ) : null} ); }); const styles = StyleSheet.create({ field: { height: 48, borderWidth: 1, borderRadius: 8, flexDirection: 'row', gap: 8, alignItems: 'center', paddingHorizontal: 16, }, });