Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type #1786

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
81 changes: 81 additions & 0 deletions src/internal/components/TextInput.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';
import { act } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { TextInput } from './TextInput';

const DELAY_MS = 100;

const RenderTest = ({
className = 'custom-class',
delayMs = DELAY_MS,
onChange = vi.fn(),
placeholder = 'Enter text',
setValue = vi.fn(),
value = 'test',
...props
}) => (
<TextInput
className={className}
delayMs={delayMs}
onChange={onChange}
placeholder={placeholder}
setValue={setValue}
value={value}
{...props}
/>
);

describe('TextInput', () => {
beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it('renders with default props', () => {
render(<RenderTest />);
expect(screen.getByTestId('ockTextInput_Input')).toBeInTheDocument();
});

it('handles value changes', () => {
const onChange = vi.fn();
const { getByTestId } = render(<RenderTest onChange={onChange} />);

const input = getByTestId('ockTextInput_Input');
act(() => {
fireEvent.change(input, { target: { value: '2' } });
});

vi.advanceTimersByTime(DELAY_MS);

expect(onChange).toHaveBeenCalledWith('2');
});

it('applies custom className', () => {
render(<RenderTest />);
expect(screen.getByTestId('ockTextInput_Input')).toHaveClass(
'custom-class',
);
});

it('handles placeholder text', () => {
render(<RenderTest />);
expect(screen.getByPlaceholderText('Enter text')).toBeInTheDocument();
});

it('handles disabled state', () => {
render(<RenderTest disabled={true} />);
expect(screen.getByTestId('ockTextInput_Input')).toBeDisabled();
});

it('handles inputMode', () => {
const { getByTestId } = render(<RenderTest inputMode="decimal" />);
expect(getByTestId('ockTextInput_Input')).toHaveAttribute(
'inputMode',
'decimal',
);
});
});
6 changes: 5 additions & 1 deletion src/internal/components/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useCallback } from 'react';
import type { ChangeEvent } from 'react';
import type { ChangeEvent, InputHTMLAttributes } from 'react';
import { useDebounce } from '../../core-react/internal/hooks/useDebounce';

type TextInputReact = {
'aria-label'?: string;
className: string;
delayMs: number;
disabled?: boolean;
// specify 'decimal' to trigger numeric keyboards on mobile devices
inputMode?: InputHTMLAttributes<HTMLInputElement>['inputMode'];
onBlur?: () => void;
onChange: (s: string) => void;
placeholder: string;
Expand All @@ -24,6 +26,7 @@ export function TextInput({
onChange,
placeholder,
setValue,
inputMode,
value,
inputValidator = () => true,
}: TextInputReact) {
Expand Down Expand Up @@ -53,6 +56,7 @@ export function TextInput({
data-testid="ockTextInput_Input"
type="text"
className={className}
inputMode={inputMode}
placeholder={placeholder}
value={value}
onBlur={onBlur}
Expand Down
Loading