import React, { ComponentProps, PropsWithChildren, useMemo } from 'react'; import { Pressable, PressableProps, View } from 'react-native'; import { Icon, SemanticColor, TypoIncludingEmoji } from '../../foundation'; import { LayoutStyleProp } from '../../internal/utils/utilTypes'; import { ThrottleOptions, useThrottledPressHandler } from '../../internal/utils/useThrottledPressHandler'; import { isNotNil, noop } from '../../internal/utils/functions'; import { BulletContent } from './subviews/BulletContent'; import { Title } from './subviews/Title'; type Props = { state?: 'information' | 'warning'; } & (WithTitleProps | NoneTitleProps | BulletProps); type WithTitleProps = { variant: 'with_title'; title: string; description: string; onPress?: () => void; } & ThrottleOptions; interface NoneTitleProps { variant: 'none_title'; description: string; } interface BulletProps { variant: 'bullet'; title: string; bulletList: string[]; } type ThemeConfig = Readonly< Record< Exclude, Readonly<{ backgroundColor: string; icon: ComponentProps['name']; iconColor: ComponentProps['color']; descriptionColor?: ComponentProps['color']; }> > >; /** * # Callout * * ## Props * * ### `state` (optional) * - 'information', 'warning' (기본값 'information') * * * ### `variant` - 'with_title' 전달시 사용 가능한 Props * - `title`: 타이틀 * - `description`: 상세 내용 * - `onPress`: 콜아웃 전체 영역 클릭 시 실행할 함수 * - `throttle`: 클릭 이벤트 쓰로틀링 옵션 (기본 동작: 500ms 쓰로틀) * * * ### `variant` - 'none_title' 전달시 사용 가능한 Props * - `description`: 상세 내용 * * * ### `variant` - 'bullet' 전달시 사용 가능한 Props * - `title`: 타이틀 * - `bulletList`: 각 항목에 표시할 문자열을 배열로 전달합니다. * * * @example * ```tsx * * * * * * * * * * * * ``` */ export function Callout({ state = 'information', ...props }: Props & LayoutStyleProp): React.JSX.Element { const THEME: ThemeConfig = useMemo( () => ({ information: { backgroundColor: SemanticColor.bg_off_white, icon: 'information_circle_filled', iconColor: 'state_text_information', descriptionColor: 'text_secondary', }, warning: { backgroundColor: SemanticColor.state_bg_error, icon: 'caution_triangle_filled', iconColor: 'state_text_error', descriptionColor: 'text_primary', }, }), [], ); const CONTAINER_PADDING = 16; return ( {(() => { if (props.variant === 'with_title') { return ( <TypoIncludingEmoji type={'body_14_regular'} color={THEME[state].descriptionColor}> {props.description} </TypoIncludingEmoji> </View> </ContainerTouchable> ); } else if (props.variant === 'none_title') { return ( <View style={{ flexDirection: 'row', gap: 8 }}> <View style={{ marginTop: 2 }}> <Icon name={THEME[state].icon} size={20} color={THEME[state].iconColor} /> </View> <View style={{ flexShrink: 1 }}> <TypoIncludingEmoji type={'body_14_regular'} color={THEME[state].descriptionColor}> {props.description} </TypoIncludingEmoji> </View> </View> ); } else if (props.variant === 'bullet') { return ( <View style={{ gap: 6 }}> <Title title={props.title} icon={THEME[state].icon} iconColor={THEME[state].iconColor} showButtonIcon={false} /> <BulletContent list={props.bulletList} color={THEME[state].descriptionColor} /> </View> ); } else { return null; } })()} </View> </View> ); } function ContainerTouchable( props: PropsWithChildren<Pick<WithTitleProps, 'onPress' | 'throttle'> & Pick<PressableProps, 'hitSlop'>>, ) { const handlePress = useThrottledPressHandler(props.onPress ?? noop, props.throttle); return ( <Pressable onPress={handlePress} disabled={!props.onPress} hitSlop={props.hitSlop}> {props.children} </Pressable> ); }