import React from 'react'; import { TextInput, ViewStyle, StyleProp, TextInputProps, StyleSheet, Pressable, GestureResponderEvent, View, } from 'react-native'; import { Icon, SemanticColor } from '../../foundation'; import { ThrottleOptions, useThrottledPressHandler } from '../../internal/utils/useThrottledPressHandler'; import { isIos, noop } from '../../internal/utils/functions'; type Props = Omit< TextInputProps, // onChange 사용할 경우 clear 버튼이 동작하지 않음. onChangeText 만 있으면 됨. 'onChange' > & { /** * 함수를 전달하면 버튼처럼 사용할 수 있습니다. * (입력 영역이나 클리어 버튼을 눌러도 함수가 실행됩니다.) */ onPress?: (event: GestureResponderEvent) => void; /** * 좌측 돋보기 아이콘을 감춥니다. (기본값 false) */ hideSearchIcon?: boolean; containerStyle?: StyleProp; } & ThrottleOptions; const DEFAULT_MAXLENGTH = 30; /** * ## SearchField 컴포넌트 * * TextInput 컴포넌트처럼 사용하시면 됩니다. * - onChange prop은 지원하지 않습니다. onChangeText를 사용해주세요. * - maxLength : 유저의 악의적인 입력을 예방하기 위해 기본값을 30으로 지정해두었습니다. * * ### Props * - onPress : 함수를 전달하면 버튼처럼 사용할 수 있습니다. (ex. 화면 이동 트리거) * - hideSearchIcon : 좌측 돋보기 아이콘을 감출 수 있습니다. * - containerStyle : 컨테이너 스타일을 지정할 수 있습니다. */ export const SearchField = React.forwardRef((props, ref) => { const { value = '', maxLength = DEFAULT_MAXLENGTH, onPress: handlePress, hideSearchIcon = false, // (prop 마다 한 줄 주기위한 주석) } = props; function handlePressClear() { props.onChangeText?.(''); } const showClearButton = value.length > 0; const isButtonMode = typeof handlePress === 'function'; const throttledPressHandler = useThrottledPressHandler(handlePress ?? noop, isButtonMode ? props.throttle : false); return ( {hideSearchIcon ? null : } {showClearButton && ( )} {/* 버튼으로 사용하기 위해 컴포넌트 전체 영역 터치 이벤트를 컨슘 */} {isButtonMode && ( )} ); }); const PADDING_VERTICAL = 10; const PADDING_HORIZONTAL = 12; const GAP = 8; const styles = StyleSheet.create({ container: { backgroundColor: SemanticColor.bg_secondary, paddingVertical: PADDING_VERTICAL, paddingHorizontal: PADDING_HORIZONTAL, borderRadius: 8, flexDirection: 'row', alignItems: 'center', gap: GAP, height: 44, }, textInput: { flex: 1, backgroundColor: SemanticColor.bg_secondary, height: 24, padding: 0, // android : 좌우 패딩 제거 }, // TextInput 컴포넌트 안에 Typo 컴포넌트를 넣을 수는 없으므로 텍스트 스타일을 직접 부여해야 함. body_16_regular: { fontFamily: 'Pretendard-Regular', fontSize: 16, lineHeight: 24, color: SemanticColor.text_primary, }, }); const hitSlops = { textInput: { // 컴포넌트 패딩 영역까지 확장 top: PADDING_VERTICAL, bottom: PADDING_VERTICAL, }, clearButton: { // 컴포넌트 패딩 영역까지 확장하되 TextInput 영역과 겹치지 않도록 함 left: GAP, top: PADDING_HORIZONTAL, right: PADDING_HORIZONTAL, bottom: PADDING_HORIZONTAL, }, } as const;