import React, { ReactElement } from 'react'; import { DimensionValue, Pressable, View } from 'react-native'; import { Icon, SemanticColor, Typo } from '../../foundation'; type RadioButtonWithLabelProps = { title: string; description?: string; /** * @deprecated 직접 사용하지 말것, RadioButtonList 컴포넌트에서 자동으로 설정됨 */ onPress?: (title: string) => void; /** * @deprecated 직접 사용하지 말것, 초기 선택이 필요한 경우에 RadioButtonList 컴포넌트에서 설정해 주세요 */ selected?: boolean; disabled?: boolean; /** * @deprecated 직접 사용하지 말것, 테두리 적용이 필요한 경우에 RadioButtonList 컴포넌트에서 설정해 주세요 */ border?: boolean; extraComponent?: ReactElement; width?: DimensionValue; flex?: number; /** * 본인 크기만큼만 차지하기 위한 옵션 * - 이 옵션을 사용할 경우, 긴 텍스트가 화면 밖으로 잘릴 수 있습니다. 따라서 부모 컴포넌트에 flexWrap: 'wrap' 처리가 필요하며, * RadioButtonWithLabelList 컴포넌트의 isFitToContent 속성을 true로 설정해 주세요 */ isFitToContent?: boolean; }; type RadioButtonWithLabelListProps = { children: ReactElement[]; onChange: (index: number, title: string) => void; gap?: number; border?: boolean; selectedIndex: number | null | undefined; row?: boolean; flex?: number; width?: DimensionValue; isFitToContent?: boolean; }; export const RadioButtonWithLabelList: React.FC = (props) => { const { children, onChange, gap = 20, border = false, selectedIndex, row, flex, width, isFitToContent } = props; const childrenWithProps = React.Children.map(children, (child, index) => { if (React.isValidElement(child)) { if (child.type !== RadioButtonWithLabel) { throw new Error(`RadioButtonList 의 children 으로 ${RadioButtonWithLabel.name}을 사용하세요`); } const newProps: RadioButtonWithLabelProps = { ...child.props, selected: selectedIndex === index, border: border, onPress: (title: string) => { // 선택된 항목의 식별자를 상위 컴포넌트에게 전달 onChange(index, title); }, }; return React.cloneElement(child, newProps); } return child; }); return ( {childrenWithProps} ); }; export const RadioButtonWithLabel: React.FC = (props) => { const { onPress, title, description, disabled, selected = false, border = false, extraComponent, width, flex, isFitToContent = false, } = props; const radioIcon = (() => { if (selected === false) { return ; } if (selected === true && disabled === true) { return ; } return ; })(); const handlePress = () => { if (onPress) { onPress(title); } }; const borderStyle = border ? { borderWidth: 1, borderColor: SemanticColor.border_tertiary, padding: 12, borderRadius: 8, backgroundColor: SemanticColor.bg_primary, } : undefined; return ( {radioIcon} {title} {description && ( {description} )} {extraComponent && {extraComponent}} ); };