import React, {useState} from 'react'; import type {StyleProp, TextStyle, ViewStyle} from 'react-native'; import {Text, View} from 'react-native'; import type {DoobooTheme} from '@dooboo-ui/theme'; import {useTheme} from '@dooboo-ui/theme'; import styled, {css} from '@emotion/native'; import CustomPressable from '../CustomPressable'; const ButtonGroupContainer = styled.View` flex-direction: row; justify-content: space-around; align-items: center; border-radius: 6px; overflow: hidden; border-color: ${({theme}) => theme.role.border}; border-width: 1px; `; const Button = styled(CustomPressable)` flex: 1; padding: 10px; justify-content: center; align-items: center; background-color: ${({theme}) => theme.bg.basic}; `; const ButtonText = styled(Text)` font-size: 14px; font-family: Pretendard-Bold; `; const Divider = styled.View` width: 1px; align-self: stretch; `; type Styles = { container?: ViewStyle; button?: ViewStyle; buttonContainer?: ViewStyle; buttonText?: TextStyle; divider?: ViewStyle; }; type ButtonGroupProps = { testID?: string; options: string[]; startElement?: JSX.Element; endElement?: JSX.Element; initialValue?: string; onValueChange?: (selected: string) => void; color?: Omit; style?: StyleProp; styles?: Styles; }; export function ButtonGroup({ testID, options, initialValue, onValueChange, color = 'primary', startElement, endElement, style, styles, }: ButtonGroupProps): JSX.Element { if (typeof initialValue !== 'string') { // eslint-disable-next-line no-console console.warn('initialValue must be string'); } else if (!options.includes(initialValue)) { // eslint-disable-next-line no-console console.warn('initialValue must be one of the options'); } const {theme} = useTheme(); const colorType = color as keyof DoobooTheme['button']; const [selectedValue, setSelectedValue] = useState( initialValue, ); const onPressButton = (option: string): void => { setSelectedValue(option); onValueChange && onValueChange(option); }; return ( {options.map((option, index) => { const isSelected = selectedValue === option; return ( {index !== options.length - 1 ? ( ) : null} ); })} ); }