2024-11-11 19:29:56 +08:00
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
export declare type SegmentedValue = string | number;
|
|
|
|
|
export declare type SegmentedRawOption = SegmentedValue;
|
|
|
|
|
export interface SegmentedLabeledOption {
|
|
|
|
|
className?: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
label: React.ReactNode;
|
|
|
|
|
value: SegmentedRawOption;
|
|
|
|
|
/**
|
|
|
|
|
* html `title` property for label
|
|
|
|
|
*/
|
|
|
|
|
title?: string;
|
|
|
|
|
}
|
|
|
|
|
declare type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[];
|
|
|
|
|
export interface SegmentedProps
|
|
|
|
|
extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
|
|
|
|
|
options: SegmentedOptions;
|
|
|
|
|
defaultValue?: SegmentedValue;
|
|
|
|
|
value?: SegmentedValue;
|
|
|
|
|
onChange?: (value: SegmentedValue) => void;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
prefixCls?: string;
|
|
|
|
|
direction?: 'ltr' | 'rtl';
|
|
|
|
|
motionName?: string;
|
2025-08-21 09:32:04 +08:00
|
|
|
activeClassName?: string;
|
2024-11-11 19:29:56 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 11:29:48 +08:00
|
|
|
export function Segmented({
|
|
|
|
|
options,
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
className,
|
2025-08-21 09:32:04 +08:00
|
|
|
activeClassName,
|
2024-11-12 11:29:48 +08:00
|
|
|
}: SegmentedProps) {
|
2025-07-11 11:34:36 +08:00
|
|
|
const [selectedValue, setSelectedValue] = React.useState<
|
|
|
|
|
SegmentedValue | undefined
|
|
|
|
|
>(value);
|
|
|
|
|
const handleOnChange = (e: SegmentedValue) => {
|
|
|
|
|
if (onChange) {
|
|
|
|
|
onChange(e);
|
|
|
|
|
}
|
|
|
|
|
setSelectedValue(e);
|
|
|
|
|
};
|
2024-11-11 19:29:56 +08:00
|
|
|
return (
|
2024-11-12 11:29:48 +08:00
|
|
|
<div
|
|
|
|
|
className={cn(
|
2025-08-07 15:19:45 +08:00
|
|
|
'flex items-center rounded-3xl p-1 gap-2 bg-bg-card px-5 py-2.5',
|
2024-11-12 11:29:48 +08:00
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
2024-11-11 19:29:56 +08:00
|
|
|
{options.map((option) => {
|
|
|
|
|
const isObject = typeof option === 'object';
|
|
|
|
|
const actualValue = isObject ? option.value : option;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={actualValue}
|
|
|
|
|
className={cn(
|
2025-08-07 15:19:45 +08:00
|
|
|
'inline-flex items-center px-6 py-2 text-base font-normal rounded-3xl cursor-pointer',
|
2024-12-03 18:59:11 +08:00
|
|
|
{
|
2025-08-20 13:39:23 +08:00
|
|
|
'text-bg-base bg-metallic-gradient border-b-[#00BEB4] border-b-2':
|
|
|
|
|
selectedValue === actualValue,
|
2024-12-03 18:59:11 +08:00
|
|
|
},
|
2025-08-21 09:32:04 +08:00
|
|
|
activeClassName && selectedValue === actualValue
|
|
|
|
|
? activeClassName
|
|
|
|
|
: '',
|
2024-11-11 19:29:56 +08:00
|
|
|
)}
|
2025-07-11 11:34:36 +08:00
|
|
|
onClick={() => handleOnChange(actualValue)}
|
2024-11-11 19:29:56 +08:00
|
|
|
>
|
|
|
|
|
{isObject ? option.label : option}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|