Skip to content

Commit

Permalink
clean up old code in TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
juancwu committed Dec 3, 2024
1 parent 9c131fd commit 8503b64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
37 changes: 20 additions & 17 deletions frontend/src/components/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@ interface TextInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
description?: string;
value?: string;
required?: boolean;
onChange?: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
multiline?: boolean;
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
rows?: number;
}

const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
({ label, error, description, className = '', value, required, onChange, multiline = false, rows = 4, ...props }, ref) => {
(
{
label,
error,
description,
className = '',
value,
required,
onChange,
rows = 4,
...props
},
ref
) => {
const inputProps = onChange
? { value, onChange }
: { defaultValue: value };

const sharedClassNames = `
w-full px-4 py-3
bg-white
Expand All @@ -28,8 +40,6 @@ const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
${className}
`;

const InputComponent = multiline ? 'textarea' : 'input';

return (
<div className="w-full">
<Field>
Expand All @@ -45,26 +55,19 @@ const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
)}
</div>
)}
<InputComponent

<Input
ref={ref as any}
className={sharedClassNames}
invalid={!!error}
required={required}
rows={multiline ? rows : undefined}
{...inputProps}
{...props}
/>
</Field>
{error && (
<p className="mt-1 text-sm text-red-500">
{error}
</p>
)}
{error && <p className="mt-1 text-sm text-red-500">{error}</p>}
{description && (
<p className="mt-1 text-sm text-gray-500">
{description}
</p>
<p className="mt-1 text-sm text-gray-500">{description}</p>
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ describe('TextInput', () => {
render(<TextInput className="custom-class" />);
expect(screen.getByRole('textbox')).toHaveClass('custom-class');
});
});
});

0 comments on commit 8503b64

Please sign in to comment.