feat: login example
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export * from './input-field';
|
||||
@@ -0,0 +1,20 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { InputField } from './input-field';
|
||||
|
||||
describe('InputForm Component', () => {
|
||||
it('renders correctly with disabled prop', () => {
|
||||
render(<InputField 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(<InputField label="Test Label" disabled={false} />);
|
||||
|
||||
const input = screen.getByLabelText('Test Label');
|
||||
expect(input).not.toBeDisabled();
|
||||
expect(input).not.toHaveClass('opacity-50 cursor-not-allowed');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
import type { Meta, StoryObj } from '@storybook/react';
|
||||
import { InputField } from './input-field';
|
||||
|
||||
const meta = {
|
||||
title: 'Molecules/Input Form',
|
||||
component: InputField,
|
||||
parameters: {
|
||||
layout: 'centered',
|
||||
docs: {
|
||||
description: {
|
||||
component: `
|
||||
Komponen input form yang menggabungkan label dengan kolom input.
|
||||
|
||||
## Aksesibilitas
|
||||
Ketika prop \`htmlFor\` disediakan:
|
||||
- Atribut \`htmlFor\` pada label akan diatur ke nilai tersebut
|
||||
- Atribut \`id\` pada input akan otomatis diatur ke nilai yang sama
|
||||
- Ini menciptakan asosiasi label-input yang tepat untuk aksesibilitas
|
||||
|
||||
Cek dan inspect element pada story With HtmlFor untuk melihat hasilnya.
|
||||
|
||||
## Helper Text dan Error
|
||||
- Jika prop \`error\` disediakan, akan ditampilkan dalam warna merah di bawah input
|
||||
- Jika prop \`helperText\` disediakan dan tidak ada error, akan ditampilkan dalam warna abu-abu di bawah input
|
||||
`,
|
||||
},
|
||||
},
|
||||
},
|
||||
tags: ['autodocs'],
|
||||
} satisfies Meta<typeof InputField>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
export const Large: Story = {
|
||||
args: {
|
||||
label: 'Label',
|
||||
placeholder: 'Placeholder',
|
||||
type: 'text',
|
||||
size: 'lg',
|
||||
helperText: 'Helper Text',
|
||||
},
|
||||
};
|
||||
|
||||
export const Medium: Story = {
|
||||
args: {
|
||||
label: 'Label',
|
||||
placeholder: 'Placeholder',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
helperText: 'Helper Text',
|
||||
},
|
||||
};
|
||||
|
||||
export const Small: Story = {
|
||||
args: {
|
||||
label: 'Label',
|
||||
placeholder: 'Placeholder',
|
||||
type: 'text',
|
||||
size: 'sm',
|
||||
helperText: 'Helper Text',
|
||||
},
|
||||
};
|
||||
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
label: 'Default',
|
||||
placeholder: 'Enter your name',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
},
|
||||
};
|
||||
|
||||
export const PasswordInput: Story = {
|
||||
args: {
|
||||
label: 'Password',
|
||||
placeholder: 'Enter your password',
|
||||
type: 'password',
|
||||
size: 'md',
|
||||
},
|
||||
};
|
||||
|
||||
export const WithHelperText: Story = {
|
||||
args: {
|
||||
label: 'Email',
|
||||
placeholder: 'Enter your email',
|
||||
type: 'email',
|
||||
size: 'md',
|
||||
helperText: 'We will never share your email',
|
||||
},
|
||||
};
|
||||
|
||||
export const WithoutHelperText: Story = {
|
||||
args: {
|
||||
label: 'Username',
|
||||
placeholder: 'Enter your username',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
},
|
||||
};
|
||||
|
||||
export const WithError: Story = {
|
||||
args: {
|
||||
label: 'Username',
|
||||
placeholder: 'Enter your username',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
error: 'This field is required',
|
||||
},
|
||||
};
|
||||
|
||||
export const WithHtmlFor: Story = {
|
||||
args: {
|
||||
label: 'Full Name',
|
||||
placeholder: 'Enter your full name',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
htmlFor: 'fullname-input',
|
||||
helperText: 'Click on the label',
|
||||
},
|
||||
};
|
||||
|
||||
export const Disabled: Story = {
|
||||
args: {
|
||||
label: 'Disabled',
|
||||
placeholder: 'This field is disabled',
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
disabled: true,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
DetailedHTMLProps,
|
||||
FC,
|
||||
InputHTMLAttributes,
|
||||
ReactElement,
|
||||
} from 'react';
|
||||
import { Input } from '../../atoms';
|
||||
import { cn } from '@imphnen-frontend-service/utils';
|
||||
|
||||
export type TInputType = 'text' | 'email' | 'password' | 'file';
|
||||
export type TInputSize = 'sm' | 'md' | 'lg';
|
||||
export type TInputFieldProps = Omit<
|
||||
DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>,
|
||||
'size' | 'type'
|
||||
> & {
|
||||
label: string;
|
||||
type?: TInputType;
|
||||
size?: TInputSize;
|
||||
error?: string;
|
||||
disabled?: boolean;
|
||||
helperText?: string;
|
||||
htmlFor?: string;
|
||||
};
|
||||
|
||||
const sizeClasses: Record<TInputSize, { label: string; helperText: string }> = {
|
||||
lg: {
|
||||
label: 'text-p3 font-medium',
|
||||
helperText: 'text-label3 font-normal',
|
||||
},
|
||||
md: {
|
||||
label: 'text-label1 font-medium',
|
||||
helperText: 'text-label2 font-normal',
|
||||
},
|
||||
sm: {
|
||||
label: 'text-label2 font-medium',
|
||||
helperText: 'text-label2 font-normal',
|
||||
},
|
||||
};
|
||||
|
||||
export const InputField: FC<TInputFieldProps> = ({
|
||||
label,
|
||||
placeholder,
|
||||
type = 'text',
|
||||
size = 'md',
|
||||
error,
|
||||
helperText,
|
||||
htmlFor,
|
||||
className,
|
||||
disabled,
|
||||
...rest
|
||||
}): ReactElement => {
|
||||
return (
|
||||
<div className="flex gap-[8px] flex-col">
|
||||
<label
|
||||
htmlFor={htmlFor}
|
||||
className={cn(
|
||||
'items-start justify-item-start text-start',
|
||||
sizeClasses[size].label
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
<Input
|
||||
{...(htmlFor && { id: htmlFor })}
|
||||
placeholder={placeholder}
|
||||
type={type}
|
||||
size={size}
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
error &&
|
||||
'border-danger-500 hover:border-danger-500 focus:outline-danger-500',
|
||||
className,
|
||||
disabled && 'opacity-50 cursor-not-allowed' // Add styles for disabled state
|
||||
)}
|
||||
{...rest}
|
||||
/>
|
||||
{error ? (
|
||||
<p className="text-danger-500 text-xs mt-1">{error}</p>
|
||||
) : (
|
||||
helperText && (
|
||||
<p className={cn('text-cs mt-1', sizeClasses[size].helperText)}>
|
||||
{helperText}
|
||||
</p>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user