style(ui): 优化 SwitchButtonGroup 交互样式并清理测试用例

This commit is contained in:
2026-07-13 20:42:19 +08:00
parent bc851414b4
commit e252b9065b
2 changed files with 11 additions and 28 deletions
+7 -4
View File
@@ -23,10 +23,12 @@ const SIZE_CLASSES = {
large: 'text-base h-11 px-4 py-2 rounded-lg', large: 'text-base h-11 px-4 py-2 rounded-lg',
} as const; } as const;
const INTERACTIVE_CLASSES =
'active:scale-[0.98] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-muted';
const SELECTED_CLASSES = const SELECTED_CLASSES =
'bg-background text-foreground shadow-sm font-semibold fade-in-zoom-95 hover:shadow-md active:scale-[0.98] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-muted'; 'bg-background text-foreground shadow-sm font-semibold hover:bg-background hover:text-foreground';
const UNSELECTED_CLASSES = const UNSELECTED_CLASSES = 'text-muted-foreground hover:bg-accent hover:text-accent-foreground';
'hover:bg-background/50 hover:text-foreground/80 active:scale-[0.98] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-muted';
export default function SwitchButtonGroup<T extends string | number = string>({ export default function SwitchButtonGroup<T extends string | number = string>({
value, value,
@@ -39,7 +41,7 @@ export default function SwitchButtonGroup<T extends string | number = string>({
return ( return (
<div <div
className={cn( className={cn(
'inline-flex w-full items-center justify-center rounded-lg bg-muted text-muted-foreground p-1', 'inline-flex w-full items-center justify-center rounded-lg bg-muted p-1',
className, className,
)} )}
{...props} {...props}
@@ -53,6 +55,7 @@ export default function SwitchButtonGroup<T extends string | number = string>({
className={cn( className={cn(
'flex-1 transition-all', 'flex-1 transition-all',
SIZE_CLASSES[size], SIZE_CLASSES[size],
INTERACTIVE_CLASSES,
value === option.value ? SELECTED_CLASSES : UNSELECTED_CLASSES, value === option.value ? SELECTED_CLASSES : UNSELECTED_CLASSES,
)} )}
> >
@@ -22,7 +22,7 @@ describe('SwitchButtonGroup 组件', () => {
const buttonB = screen.getByRole('button', { name: /选项B/i }); const buttonB = screen.getByRole('button', { name: /选项B/i });
expect(buttonA).toHaveClass('bg-background', 'text-foreground', 'shadow-sm'); expect(buttonA).toHaveClass('bg-background', 'text-foreground', 'shadow-sm');
expect(buttonB).toHaveClass('hover:bg-background/50'); expect(buttonB).toHaveClass('hover:bg-accent', 'hover:text-accent-foreground');
}); });
it('点击未选中按钮时应触发 onChange 并传入选中值', () => { it('点击未选中按钮时应触发 onChange 并传入选中值', () => {
@@ -34,11 +34,12 @@ describe('SwitchButtonGroup 组件', () => {
expect(handleChange).toHaveBeenCalledWith('b'); expect(handleChange).toHaveBeenCalledWith('b');
}); });
it('点击已选中按钮时不应触发 onChange', () => { it('点击已选中按钮时仍会触发 onChange 并传回原值', () => {
const handleChange = vi.fn(); const handleChange = vi.fn();
render(<SwitchButtonGroup value="a" options={options} onChange={handleChange} />); render(<SwitchButtonGroup value="a" options={options} onChange={handleChange} />);
fireEvent.click(screen.getByRole('button', { name: /选项A/i })); fireEvent.click(screen.getByRole('button', { name: /选项A/i }));
expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledWith('a'); expect(handleChange).toHaveBeenCalledWith('a');
}); });
@@ -58,13 +59,6 @@ describe('SwitchButtonGroup 组件', () => {
expect(button).toHaveClass('text-xs'); expect(button).toHaveClass('text-xs');
}); });
it('应支持 buttonSx 自定义按钮样式', () => {
render(<SwitchButtonGroup value="a" options={options} onChange={vi.fn()} />);
const button = screen.getByRole('button', { name: /选项A/i });
expect(button).toBeInTheDocument();
});
it('应支持 ReactNode 类型的 label', () => { it('应支持 ReactNode 类型的 label', () => {
const nodeOptions = [{ value: 'x', label: <span data-testid="custom-label"></span> }]; const nodeOptions = [{ value: 'x', label: <span data-testid="custom-label"></span> }];
render(<SwitchButtonGroup value="x" options={nodeOptions} onChange={vi.fn()} />); render(<SwitchButtonGroup value="x" options={nodeOptions} onChange={vi.fn()} />);
@@ -72,20 +66,6 @@ describe('SwitchButtonGroup 组件', () => {
expect(screen.getByTestId('custom-label')).toBeInTheDocument(); expect(screen.getByTestId('custom-label')).toBeInTheDocument();
}); });
it('默认按钮样式应禁止文字换行', () => {
render(<SwitchButtonGroup value="a" options={options} onChange={vi.fn()} />);
const button = screen.getByRole('button', { name: /选项A/i });
expect(button).toHaveClass('whitespace-nowrap');
});
it('buttonSx 传入时应覆盖默认换行样式', () => {
render(<SwitchButtonGroup value="a" options={options} onChange={vi.fn()} />);
const button = screen.getByRole('button', { name: /选项A/i });
expect(button).toBeInTheDocument();
});
describe('number 类型支持', () => { describe('number 类型支持', () => {
const numberOptions = [ const numberOptions = [
{ value: 2, label: '2' }, { value: 2, label: '2' },
@@ -105,7 +85,7 @@ describe('SwitchButtonGroup 组件', () => {
const button2 = screen.getByRole('button', { name: /^2$/i }); const button2 = screen.getByRole('button', { name: /^2$/i });
const button4 = screen.getByRole('button', { name: /^4$/i }); const button4 = screen.getByRole('button', { name: /^4$/i });
expect(button2).toHaveClass('hover:bg-background/50'); expect(button2).toHaveClass('hover:bg-accent', 'hover:text-accent-foreground');
expect(button4).toHaveClass('bg-background', 'text-foreground', 'shadow-sm'); expect(button4).toHaveClass('bg-background', 'text-foreground', 'shadow-sm');
}); });