-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { render } from '@testing-library/react' | ||
import React from 'react' | ||
|
||
import { Input } from '@/components/input' | ||
|
||
describe('Input', () => { | ||
it('should default to `type="text"`', () => { | ||
const { queryByLabelText } = render(<Input aria-label="test input" />) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).toHaveAttribute('type', 'text') | ||
}) | ||
|
||
it('should not have the `disabled` attribute and `aria-disabled="false"` if `loading` is false', () => { | ||
const { queryByLabelText } = render(<Input aria-label="test input" />) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).not.toHaveAttribute('disabled') | ||
expect(queryByLabelText('test input')).toHaveAttribute('aria-disabled', 'false') | ||
expect(queryByLabelText('test input')).not.toBeDisabled() | ||
}) | ||
|
||
it('should have the `border-base` class by default', () => { | ||
const { queryByLabelText } = render(<Input aria-label="test input" />) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).toHaveClass('border-base') | ||
}) | ||
|
||
it('should have the `pl-12` class when the `icon` variant is passed', () => { | ||
const { queryByLabelText } = render(<Input aria-label="test input" icon={<div />} />) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).toHaveClass('pl-12') | ||
}) | ||
|
||
it('should have the `bg-disabled` and `border-transparent` classes when the `disabled` variant is passed', () => { | ||
const { queryByLabelText } = render(<Input aria-label="test input" disabled />) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).toHaveClass('bg-disabled') | ||
expect(queryByLabelText('test input')).toHaveClass('border-transparent') | ||
}) | ||
|
||
it('should have the `aria-invalid` and `aria-describedby` attributes if errorMessage is present', () => { | ||
const { queryByLabelText, queryByText } = render( | ||
<Input aria-label="test input" errorMessage="some error" />, | ||
) | ||
|
||
expect(queryByLabelText('test input')).toBeInTheDocument() | ||
expect(queryByLabelText('test input')).toHaveAttribute('aria-invalid') | ||
expect(queryByLabelText('test input')).toHaveAttribute('aria-describedby') | ||
expect(queryByText('some error')).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
|
||
import { Switch } from '@/components/switch' | ||
|
||
describe('Switch', () => { | ||
it('renders without crashing', () => { | ||
render(<Switch />) | ||
expect(screen.getByRole('switch')).toBeInTheDocument() | ||
}) | ||
|
||
it('applies default classes', () => { | ||
render(<Switch />) | ||
const switchElement = screen.getByRole('switch').nextSibling | ||
expect(switchElement).toHaveClass('w-[42px] h-[26px] before:h-5 before:w-5') | ||
}) | ||
|
||
it('applies small size classes when size prop is small', () => { | ||
render(<Switch size="small" />) | ||
const switchElement = screen.getByRole('switch').nextSibling | ||
expect(switchElement).toHaveClass('w-9 h-[22px] before:h-4 before:w-4 before:left-[3px]') | ||
}) | ||
|
||
it('forwards ref to input element', () => { | ||
const ref = React.createRef<HTMLInputElement>() | ||
render(<Switch ref={ref} />) | ||
expect(ref.current).toBeInstanceOf(HTMLInputElement) | ||
}) | ||
|
||
it('forwards checked prop to input element', () => { | ||
render(<Switch checked />) | ||
const inputElement = screen.getByRole('switch') | ||
expect(inputElement).toBeChecked() | ||
}) | ||
|
||
it('handles additional props', () => { | ||
render(<Switch aria-label="Custom Switch" />) | ||
const inputElement = screen.getByRole('switch') | ||
expect(inputElement).toHaveAttribute('aria-label', 'Custom Switch') | ||
}) | ||
|
||
it('applies custom class names', () => { | ||
const customClass = 'custom-class' | ||
render(<Switch className={customClass} />) | ||
const switchElement = screen.getByRole('switch').nextSibling | ||
expect(switchElement).toHaveClass(customClass) | ||
}) | ||
|
||
it('toggles switch state when clicked', () => { | ||
render(<Switch />) | ||
const inputElement = screen.getByRole('switch') | ||
expect(inputElement).not.toBeChecked() | ||
|
||
fireEvent.click(inputElement) | ||
expect(inputElement).toBeChecked() | ||
|
||
fireEvent.click(inputElement) | ||
expect(inputElement).not.toBeChecked() | ||
}) | ||
|
||
it('handles additional HTML attributes', () => { | ||
const testId = 'switch-test' | ||
render(<Switch data-testid={testId} />) | ||
const switchElement = screen.getByTestId(testId) | ||
expect(switchElement).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { type VariantProps, cva } from 'class-variance-authority' | ||
import React, { forwardRef } from 'react' | ||
|
||
import { cn } from '@/utils/cn' | ||
|
||
const inputVariants = cva( | ||
[ | ||
'w-full h-14 rounded-xl border border-2 px-4 text-base text-medium', | ||
'focus:outline-none focus:border-focus', | ||
'placeholder-disabled', | ||
], | ||
|
||
{ | ||
variants: { | ||
variant: { | ||
default: 'border-base', | ||
}, | ||
disabled: { | ||
true: 'bg-disabled border-transparent', | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: 'default', | ||
}, | ||
}, | ||
) | ||
|
||
export interface InputProps | ||
extends VariantProps<typeof inputVariants>, | ||
React.InputHTMLAttributes<HTMLInputElement> { | ||
errorMessage?: string | ||
disabled?: boolean | ||
icon?: React.ReactNode | ||
} | ||
|
||
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input( | ||
{ type = 'text', icon, errorMessage, disabled, className, ...props }, | ||
ref, | ||
) { | ||
return ( | ||
<div className="relative"> | ||
{icon && <div className="absolute left-4 top-4">{icon}</div>} | ||
<input | ||
ref={ref} | ||
type={type} | ||
className={cn(inputVariants({ disabled }), icon && 'pl-12', className)} | ||
disabled={disabled ?? false} | ||
aria-disabled={disabled ?? false} | ||
aria-invalid={!!errorMessage} | ||
aria-describedby={errorMessage} | ||
{...props} | ||
/> | ||
{errorMessage && <p className="text-error text-sm px-2">{errorMessage}</p>} | ||
</div> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type VariantProps, cva } from 'class-variance-authority' | ||
import React, { forwardRef } from 'react' | ||
|
||
import { cn } from '@/utils/cn' | ||
|
||
const labelVariants = cva('text-medium font-medium leading-6 px-2 flex items-center gap-2') | ||
|
||
export interface LabelProps | ||
extends VariantProps<typeof labelVariants>, | ||
React.LabelHTMLAttributes<HTMLLabelElement> { | ||
children: React.ReactNode | ||
} | ||
|
||
export const Label = forwardRef<HTMLLabelElement, LabelProps>(function Label( | ||
{ className, children, ...props }, | ||
ref, | ||
) { | ||
return ( | ||
<label ref={ref} className={cn(labelVariants(), className)} {...props}> | ||
{children} | ||
</label> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { type VariantProps, cva } from 'class-variance-authority' | ||
import React, { forwardRef } from 'react' | ||
|
||
import { cn } from '@/utils/cn' | ||
|
||
const switchVariants = cva( | ||
[ | ||
'rounded-full bg-disabled-strong relative cursor-pointer transition-colors duration-300 ease-in-out', | ||
'before:content-[""] before:absolute before:bg-white before:rounded-full', | ||
'before:top-1/2 before:transform before:-translate-y-1/2 before:left-[4px]', | ||
'before:transition-all before:duration-300 before:ease-in-out', | ||
'peer-checked:before:left-[18px] peer-checked:bg-switch-base', | ||
'peer-focus:outline peer-focus:outline-2 peer-focus:outline-blue-500', | ||
], | ||
|
||
{ | ||
variants: { | ||
size: { | ||
default: 'w-[42px] h-[26px] before:h-5 before:w-5', | ||
small: [ | ||
'w-9 h-[22px] before:h-4 before:w-4 before:left-[3px]', | ||
'peer-checked:before:left-4', | ||
], | ||
}, | ||
}, | ||
defaultVariants: { | ||
size: 'default', | ||
}, | ||
}, | ||
) | ||
|
||
export interface SwitchProps | ||
extends VariantProps<typeof switchVariants>, | ||
React.HTMLAttributes<HTMLInputElement> { | ||
checked?: boolean | ||
} | ||
|
||
export const Switch = forwardRef<HTMLInputElement, SwitchProps>(function Switch( | ||
{ size, className, ...props }, | ||
ref, | ||
) { | ||
return ( | ||
<label> | ||
<input | ||
role="switch" | ||
ref={ref} | ||
type="checkbox" | ||
{...props} | ||
className="peer absolute opacity-0 -translate-x-[100%] pointer-events-none" | ||
/> | ||
<div className={cn(switchVariants({ size }), className)} /> | ||
</label> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters