import type { ComponentProps } from 'react'; import React from 'react'; import Badge from '../Badge'; import Icon from '../Icon'; import type Typography from '../Typography'; import { StyledSegmentedItem, StyledSegmentedItemLabelWrapper, StyledSegmentedItemText, StyledSegmentedItemWrapper, } from './StyledSegmentedControl'; import type { CounterBadgeType, SegmentedControlItemConfig, StatusBadgeType, } from './types'; import Box from '../Box'; const DOT_CHAR = '\u00B7'; type TypographyIntent = ComponentProps['intent']; type IconIntent = ComponentProps['intent']; export interface SegmentedItemProps extends SegmentedControlItemConfig { /** * Whether the segment option is selected. */ selected?: boolean; /** * The size of the segment option. */ size?: 'medium' | 'large'; /** * The callback function to be called when the segment option is pressed. */ onPress: () => void; } const SegmentedItemBadge = ({ children, badge, testID, }: { children?: React.ReactNode; badge?: StatusBadgeType | CounterBadgeType; testID?: string; }) => { if (!badge) return children; if (badge.type === 'status') { return ( {children} ); } if (badge.type === 'counter') { return ( <> {children} ); } }; const getTextAndIconIntent = ({ selected, disabled, }: { selected?: boolean; disabled?: boolean; }): { iconIntent: IconIntent; textIntent: TypographyIntent; subTextIntent: TypographyIntent; } => { if (disabled) { return { iconIntent: 'disabled-text', textIntent: 'disabled', subTextIntent: 'disabled', }; } if (selected) { return { iconIntent: 'text', textIntent: 'body', subTextIntent: 'muted', }; } return { iconIntent: 'inactive', textIntent: 'inactive', subTextIntent: 'inactive', }; }; const SegmentedItem = ({ label, prefix, suffix, selected = false, size = 'medium', testID, onPress, disabled, badge, subText, }: SegmentedItemProps) => { const { iconIntent, textIntent, subTextIntent } = getTextAndIconIntent({ selected, disabled, }); const shouldShowSuffix = !!label && !!suffix; return ( {/* Prefix */} {prefix && ( )} {/* Label and subtext */} {!!label && ( {label && ( {label} )} {subText && ( <> {DOT_CHAR} {subText} )} )} {/* Suffix, only shown if there is a label and suffix */} {shouldShowSuffix && ( )} ); }; export default SegmentedItem;