Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions react/src/components/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { Button } from './Button';

describe('Button Component', () => {
it('renders children text correctly', () => {
render(<Button>Click me</Button>);
expect(screen.getByRole('button', { name: /click me/i })).toBeInTheDocument();
});

it('applies custom variant classes matching tokens', () => {
const { rerender } = render(<Button variant="danger">Danger</Button>);
expect(screen.getByRole('button')).toHaveClass('bg-red-600');

rerender(<Button variant="outline">Outline</Button>);
expect(screen.getByRole('button')).toHaveClass('border-gray-300');
});

it('triggers onClick handler when clicked', () => {
const handleClick = jest.fn();
render(<Button onClick={handleClick}>Action</Button>);

fireEvent.click(screen.getByRole('button'));
expect(handleClick).toHaveBeenCalledTimes(1);
});

it('handles loading state accessibility and disables interaction safely', () => {
const handleClick = jest.fn();
render(<Button isLoading onClick={handleClick}>Submit</Button>);

const button = screen.getByRole('button');
expect(button).toBeDisabled();
expect(button).toHaveAttribute('aria-disabled', 'true');
expect(screen.getByText('Loading...')).toBeInTheDocument();

fireEvent.click(button);
expect(handleClick).not.toHaveBeenCalled();
});

it('supports passing traditional forwarding refs correctly', () => {
const ref = React.createRef<HTMLButtonElement>();
render(<Button ref={ref}>Ref Button</Button>);
expect(ref.current).toBeInstanceOf(HTMLButtonElement);
});
});
79 changes: 79 additions & 0 deletions react/src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { forwardRef } from 'react';

export type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'outline';
export type ButtonSize = 'sm' | 'md' | 'lg';

export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: ButtonVariant;
size?: ButtonSize;
isLoading?: boolean;
}

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
children,
variant = 'primary',
size = 'md',
isLoading = false,
disabled,
className = '',
type = 'button',
...props
},
ref
) => {
// Base design tokens including structural, layout, and accessible focus outlines
const baseStyles =
'inline-flex items-center justify-center font-medium rounded-md transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none';

const variants: Record<ButtonVariant, string> = {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
outline: 'border border-gray-300 bg-transparent text-gray-700 hover:bg-gray-50 focus:ring-blue-500',
};

const sizes: Record<ButtonSize, string> = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg',
};

const isInteractionDisabled = disabled || isLoading;

return (
<button
ref={ref}
type={type}
disabled={isInteractionDisabled}
aria-disabled={isInteractionDisabled ? true : undefined}
className={`${baseStyles} ${variants[variant]} ${sizes[size]} ${className}`}
{...props}
>
{isLoading ? (
<>
<svg
className="animate-spin -ml-1 mr-2 h-4 w-4 text-current"
fill="none"
viewBox="0 0 24 24"
aria-hidden="true"
>
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
<span>Loading...</span>
</>
) : (
children
)}
</button>
);
}
);

Button.displayName = 'Button';
6 changes: 6 additions & 0 deletions react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export type { AlertProps, AlertVariant } from './Alert';

export { Badge } from './Badge';
export type { BadgeProps, BadgeVariant, BadgeSize } from './Badge';

export { Button } from './Button';
export type { ButtonProps, ButtonVariant, ButtonSize } from './Button';

export { Modal } from './Modal';
export type { ModalProps } from './Modal';