feat: Update InputForm to handle different sizes

- Tambah spec test pada komponen InputForm
- Tambah autodocs pada komponen Input
This commit is contained in:
Hafid Nur
2025-03-27 06:00:03 +07:00
parent 54f4572533
commit 631684cb05
3 changed files with 47 additions and 4 deletions
+2 -1
View File
@@ -14,7 +14,8 @@ const meta: Meta<typeof Input> = {
options: ['sm', 'md', 'lg'], options: ['sm', 'md', 'lg'],
}, },
}, },
}; tags: ['autodocs'],
} satisfies Meta<typeof Input>;
export default meta; export default meta;
type Story = StoryObj<typeof Input>; type Story = StoryObj<typeof Input>;
@@ -0,0 +1,20 @@
import { render, screen } from '@testing-library/react';
import InputForm from './input-form';
describe('InputForm Component', () => {
it('renders correctly with disabled prop', () => {
render(<InputForm label="Test Label" disabled={true} />);
const input = screen.getByLabelText('Test Label');
expect(input).toBeDisabled();
expect(input).toHaveClass('opacity-50 cursor-not-allowed');
});
it('renders correctly without disabled prop', () => {
render(<InputForm label="Test Label" disabled={false} />);
const input = screen.getByLabelText('Test Label');
expect(input).not.toBeDisabled();
expect(input).not.toHaveClass('opacity-50 cursor-not-allowed');
});
});
@@ -18,10 +18,27 @@ type TInputFormProps = Omit<
type?: TInputType; type?: TInputType;
size?: TInputSize; size?: TInputSize;
error?: string; error?: string;
disabled?: boolean;
helperText?: string; helperText?: string;
htmlFor?: string; htmlFor?: string;
}; };
const sizeClasses: Record<TInputSize, { label: string; helperText: string }> = {
lg: {
label: 'text-p3 font-medium',
helperText: 'text-label2 font-normal',
},
md: {
label: 'text-label1 font-medium',
helperText: 'text-label2 font-normal',
},
sm: {
label: 'text-label2 font-medium',
helperText: 'text-label3 font-normal',
},
};
export const InputForm: FC<TInputFormProps> = ({ export const InputForm: FC<TInputFormProps> = ({
label, label,
placeholder, placeholder,
@@ -31,11 +48,12 @@ export const InputForm: FC<TInputFormProps> = ({
helperText, helperText,
htmlFor, htmlFor,
className, className,
disabled,
...rest ...rest
}): ReactElement => { }): ReactElement => {
return ( return (
<div className="flex gap-[8px] flex-col"> <div className="flex gap-[8px] flex-col">
<label htmlFor={htmlFor} className="self-start text-p3 font-medium"> <label htmlFor={htmlFor} className={sizeClasses[size].label}>
{label} {label}
</label> </label>
<Input <Input
@@ -43,10 +61,12 @@ export const InputForm: FC<TInputFormProps> = ({
placeholder={placeholder} placeholder={placeholder}
type={type} type={type}
size={size} size={size}
disabled={disabled}
className={cn( className={cn(
error && error &&
'border-danger-500 hover:border-danger-500 focus:outline-danger-500', 'border-danger-500 hover:border-danger-500 focus:outline-danger-500',
className className,
disabled && 'opacity-50 cursor-not-allowed' // Add styles for disabled state
)} )}
{...rest} {...rest}
/> />
@@ -54,7 +74,9 @@ export const InputForm: FC<TInputFormProps> = ({
<p className="text-danger-500 text-xs mt-1">{error}</p> <p className="text-danger-500 text-xs mt-1">{error}</p>
) : ( ) : (
helperText && ( helperText && (
<p className="text-neutral-500 text-xs mt-1">{helperText}</p> <p className={`${sizeClasses[size].helperText} text-xs mt-1`}>
{helperText}
</p>
) )
)} )}
</div> </div>