2025-04-18 16:53:43 +08:00
|
|
|
import React from 'react'
|
2025-04-29 10:44:03 +08:00
|
|
|
import cn from '@/utils/classnames'
|
2025-04-18 16:53:43 +08:00
|
|
|
import type { RemixiconComponentType } from '@remixicon/react'
|
|
|
|
import Divider from '../divider'
|
2025-04-29 10:44:03 +08:00
|
|
|
import type { VariantProps } from 'class-variance-authority'
|
|
|
|
import { cva } from 'class-variance-authority'
|
|
|
|
import './index.css'
|
|
|
|
|
|
|
|
type SegmentedControlOption<T> = {
|
|
|
|
value: T
|
|
|
|
text?: string
|
|
|
|
Icon: RemixiconComponentType
|
|
|
|
count?: number
|
|
|
|
}
|
2025-04-18 16:53:43 +08:00
|
|
|
|
|
|
|
type SegmentedControlProps<T extends string | number | symbol> = {
|
2025-04-29 10:44:03 +08:00
|
|
|
options: SegmentedControlOption<T>[]
|
2025-04-18 16:53:43 +08:00
|
|
|
value: T
|
|
|
|
onChange: (value: T) => void
|
|
|
|
className?: string
|
2025-04-29 10:44:03 +08:00
|
|
|
activeClassName?: string
|
2025-04-18 16:53:43 +08:00
|
|
|
}
|
|
|
|
|
2025-04-29 10:44:03 +08:00
|
|
|
const SegmentedControlVariants = cva(
|
|
|
|
'segmented-control',
|
|
|
|
{
|
|
|
|
variants: {
|
|
|
|
size: {
|
|
|
|
regular: 'segmented-control-regular',
|
|
|
|
small: 'segmented-control-small',
|
|
|
|
large: 'segmented-control-large',
|
|
|
|
},
|
|
|
|
padding: {
|
|
|
|
none: 'no-padding',
|
|
|
|
with: 'padding',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultVariants: {
|
|
|
|
size: 'regular',
|
|
|
|
padding: 'with',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
const SegmentedControlItemVariants = cva(
|
|
|
|
'segmented-control-item disabled:segmented-control-item-disabled',
|
|
|
|
{
|
|
|
|
variants: {
|
|
|
|
size: {
|
|
|
|
regular: ['segmented-control-item-regular', 'system-sm-medium'],
|
|
|
|
small: ['segmented-control-item-small', 'system-xs-medium'],
|
|
|
|
large: ['segmented-control-item-large', 'system-md-semibold'],
|
|
|
|
},
|
|
|
|
activeState: {
|
|
|
|
default: '',
|
|
|
|
accent: 'accent',
|
|
|
|
accentLight: 'accent-light',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultVariants: {
|
|
|
|
size: 'regular',
|
|
|
|
activeState: 'default',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
const ItemTextWrapperVariants = cva(
|
|
|
|
'item-text',
|
|
|
|
{
|
|
|
|
variants: {
|
|
|
|
size: {
|
|
|
|
regular: 'item-text-regular',
|
|
|
|
small: 'item-text-small',
|
|
|
|
large: 'item-text-large',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultVariants: {
|
|
|
|
size: 'regular',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2025-04-18 16:53:43 +08:00
|
|
|
export const SegmentedControl = <T extends string | number | symbol>({
|
|
|
|
options,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
className,
|
2025-04-29 10:44:03 +08:00
|
|
|
size,
|
|
|
|
padding,
|
|
|
|
activeState,
|
|
|
|
activeClassName,
|
|
|
|
}: SegmentedControlProps<T>
|
|
|
|
& VariantProps<typeof SegmentedControlVariants>
|
|
|
|
& VariantProps<typeof SegmentedControlItemVariants>
|
|
|
|
& VariantProps<typeof ItemTextWrapperVariants>) => {
|
2025-04-18 16:53:43 +08:00
|
|
|
const selectedOptionIndex = options.findIndex(option => option.value === value)
|
|
|
|
|
|
|
|
return (
|
2025-04-29 10:44:03 +08:00
|
|
|
<div className={cn(
|
|
|
|
SegmentedControlVariants({ size, padding }),
|
2025-04-18 16:53:43 +08:00
|
|
|
className,
|
|
|
|
)}>
|
|
|
|
{options.map((option, index) => {
|
2025-04-29 10:44:03 +08:00
|
|
|
const { Icon, text, count } = option
|
2025-04-18 16:53:43 +08:00
|
|
|
const isSelected = index === selectedOptionIndex
|
|
|
|
const isNextSelected = index === selectedOptionIndex - 1
|
|
|
|
const isLast = index === options.length - 1
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
key={String(option.value)}
|
2025-04-29 10:44:03 +08:00
|
|
|
className={cn(
|
|
|
|
isSelected ? 'active' : 'default',
|
|
|
|
SegmentedControlItemVariants({ size, activeState: isSelected ? activeState : 'default' }),
|
|
|
|
isSelected && activeClassName,
|
2025-04-18 16:53:43 +08:00
|
|
|
)}
|
|
|
|
onClick={() => onChange(option.value)}
|
|
|
|
>
|
2025-04-30 14:16:13 +08:00
|
|
|
<Icon className='size-4 shrink-0' />
|
2025-04-29 10:44:03 +08:00
|
|
|
{text && (
|
|
|
|
<div className={cn('inline-flex items-center gap-x-1', ItemTextWrapperVariants({ size }))}>
|
|
|
|
<span>{text}</span>
|
|
|
|
{count && size === 'large' && (
|
|
|
|
<div className='system-2xs-medium-uppercase inline-flex h-[18px] min-w-[18px] items-center justify-center rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-[5px] text-text-tertiary'>
|
|
|
|
{count}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2025-04-18 16:53:43 +08:00
|
|
|
{!isLast && !isSelected && !isNextSelected && (
|
|
|
|
<div className='absolute right-[-1px] top-0 flex h-full items-center'>
|
|
|
|
<Divider type='vertical' className='mx-0 h-3.5' />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2025-04-29 10:44:03 +08:00
|
|
|
export default React.memo(SegmentedControl)
|