import React, { ComponentProps } from 'react'; import { Pressable, StyleSheet, View } from 'react-native'; import { Icon, SemanticColor, Typo } from '../../foundation'; import { useThrottledPressHandler } from '../../internal/utils/useThrottledPressHandler'; // (참고) discriminated union 작성시 토스트 매니저 구현 복잡도가 높아짐. // - 따라서 속성들을 옵셔널로 정의하고, 디자인시스템 규칙은 런타임에 검사하여 무단 커스터마이즈를 방지함. // - 토스트 호출은 토스트 매니저를 통해야 하므로, 매니저의 메서드에서 디자인시스템 규칙을 타입으로 강제하면 됨. type Props = { type?: 'action' | 'message'; state?: 'default' | 'success' | 'warning'; text: string; buttonLabel?: string; onPressButton?: () => void; }; /** * # Toast 컴포넌트 * * * ## Props * * ### `type` optional * - 'message', 'action' * - 'action' 타입이면 `buttonLabel`, `onPressButton` prop 전달 필수 * * ### `state` optional * - 'default', 'success', 'warning' * - 'action' 타입이면 state 지정 불가 (default 고정) * * ### `text` * - 토스트 메시지 내용 * * ### `buttonLabel` * - 버튼 텍스트 * * ### `onPressButton` * - 버튼 클릭 시 실행할 함수 * * * @deprecated 이 컴포넌트는 단독으로 사용하지 않습니다. `ToastManageView` 컴포넌트를 통해 사용됩니다. * * * @example * ```tsx * // 메시지만 표시 (type, state prop 생략 가능) * * * // 성공 아이콘 표시 * * * // 버튼이 있는 토스트 * * ``` */ export function Toast(props: Props): React.JSX.Element { const leftIcon: null | Pick, 'name' | 'color'> = (() => { if (props.type === 'action') return null; switch (props.state) { case 'success': return { name: 'success_circle_filled', color: 'state_text_success' }; case 'warning': return { name: 'caution_triangle_filled', color: 'state_text_warning' }; default: return null; } })(); return ( {leftIcon ? : null} {props.text} {props.type === 'action' && props.buttonLabel && props.onPressButton ? ( ) : null} ); } function ActionButton(props: { onPress: () => void; label: string }) { const handlePress = useThrottledPressHandler(props.onPress); return ( {props.label} ); } const styles = StyleSheet.create({ container: { marginHorizontal: 16, maxWidth: 500, // (참고) 폴더블 기기에서 토스트가 너무 길어지는 것을 방어 alignSelf: 'center', }, body: { flexDirection: 'row', width: '100%', backgroundColor: SemanticColor.overlay_black_90, borderRadius: 8, paddingHorizontal: 16, paddingVertical: 12, alignItems: 'center', gap: 12, }, flex1: { flex: 1, }, });