feat: Update InputForm component
- Perbarui docs untuk component Input dan InputForm - Perbarui component InputForm agar bekerja dengan helper text, error, dan label/input
This commit is contained in:
@@ -61,11 +61,3 @@ export const Disabled: Story = {
|
|||||||
disabled: true,
|
disabled: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const WithError: Story = {
|
|
||||||
args: {
|
|
||||||
size: 'md',
|
|
||||||
type: 'text',
|
|
||||||
error: 'This field is required',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ type TInputProps = Omit<
|
|||||||
> & {
|
> & {
|
||||||
type?: TInputType;
|
type?: TInputType;
|
||||||
size?: TInputSize;
|
size?: TInputSize;
|
||||||
error?: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const sizeClasses: Record<TInputSize, string> = {
|
const sizeClasses: Record<TInputSize, string> = {
|
||||||
@@ -25,15 +24,12 @@ const sizeClasses: Record<TInputSize, string> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const disabledClass = 'opacity-50 hover:border-neutral-200 cursor-not-allowed';
|
const disabledClass = 'opacity-50 hover:border-neutral-200 cursor-not-allowed';
|
||||||
const errorClass =
|
|
||||||
'border-danger-500 hover:border-danger-500 focus:outline-danger-500';
|
|
||||||
|
|
||||||
export const Input: FC<TInputProps> = ({
|
export const Input: FC<TInputProps> = ({
|
||||||
size = 'md',
|
size = 'md',
|
||||||
type,
|
type = 'text',
|
||||||
placeholder = 'Placeholder',
|
placeholder = 'Placeholder',
|
||||||
disabled,
|
disabled,
|
||||||
error,
|
|
||||||
className,
|
className,
|
||||||
...rest
|
...rest
|
||||||
}): ReactElement => {
|
}): ReactElement => {
|
||||||
@@ -41,20 +37,16 @@ export const Input: FC<TInputProps> = ({
|
|||||||
'px-[12px] py-[8px] text-neutral-800 placeholder:text-neutral-300 border border-neutral-200 hover:border-blue-300 focus:outline-1 focus:outline-blue-500 rounded-md font-bai-jamjuree',
|
'px-[12px] py-[8px] text-neutral-800 placeholder:text-neutral-300 border border-neutral-200 hover:border-blue-300 focus:outline-1 focus:outline-blue-500 rounded-md font-bai-jamjuree',
|
||||||
sizeClasses[size],
|
sizeClasses[size],
|
||||||
disabled && disabledClass,
|
disabled && disabledClass,
|
||||||
error && errorClass,
|
|
||||||
className
|
className
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<input
|
||||||
<input
|
className={mergedClassName}
|
||||||
className={mergedClassName}
|
type={type}
|
||||||
type={type}
|
disabled={disabled}
|
||||||
disabled={disabled}
|
placeholder={placeholder}
|
||||||
placeholder={placeholder}
|
{...rest}
|
||||||
{...rest}
|
/>
|
||||||
/>
|
|
||||||
{error && <p className="text-danger-500 text-xs mt-1">{error}</p>}
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,6 +6,25 @@ const meta = {
|
|||||||
component: InputForm,
|
component: InputForm,
|
||||||
parameters: {
|
parameters: {
|
||||||
layout: 'centered',
|
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'],
|
tags: ['autodocs'],
|
||||||
} satisfies Meta<typeof InputForm>;
|
} satisfies Meta<typeof InputForm>;
|
||||||
@@ -13,6 +32,33 @@ const meta = {
|
|||||||
export default meta;
|
export default meta;
|
||||||
type Story = StoryObj<typeof meta>;
|
type Story = StoryObj<typeof meta>;
|
||||||
|
|
||||||
|
export const Large: Story = {
|
||||||
|
args: {
|
||||||
|
label: 'Description',
|
||||||
|
placeholder: 'Enter description',
|
||||||
|
type: 'text',
|
||||||
|
size: 'lg',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Medium: Story = {
|
||||||
|
args: {
|
||||||
|
label: 'Address',
|
||||||
|
placeholder: 'Enter your address',
|
||||||
|
type: 'text',
|
||||||
|
size: 'md',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Small: Story = {
|
||||||
|
args: {
|
||||||
|
label: 'Phone',
|
||||||
|
placeholder: 'Enter your phone number',
|
||||||
|
type: 'text',
|
||||||
|
size: 'sm',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export const Default: Story = {
|
export const Default: Story = {
|
||||||
args: {
|
args: {
|
||||||
label: 'Email',
|
label: 'Email',
|
||||||
@@ -22,6 +68,25 @@ export const Default: Story = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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: 'Password',
|
||||||
|
placeholder: 'Enter your password',
|
||||||
|
type: 'text',
|
||||||
|
size: 'md',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export const WithError: Story = {
|
export const WithError: Story = {
|
||||||
args: {
|
args: {
|
||||||
label: 'Username',
|
label: 'Username',
|
||||||
@@ -32,20 +97,13 @@ export const WithError: Story = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Large: Story = {
|
export const WithHtmlFor: Story = {
|
||||||
args: {
|
args: {
|
||||||
label: 'Full Name',
|
label: 'Full Name',
|
||||||
placeholder: 'Enter your full name',
|
placeholder: 'Enter your full name',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
size: 'lg',
|
size: 'md',
|
||||||
},
|
htmlFor: 'fullname-input',
|
||||||
};
|
helperText: 'Enter your legal full name',
|
||||||
|
|
||||||
export const Small: Story = {
|
|
||||||
args: {
|
|
||||||
label: 'Phone',
|
|
||||||
placeholder: 'Enter your phone number',
|
|
||||||
type: 'text',
|
|
||||||
size: 'sm',
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
ReactElement,
|
ReactElement,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { Input } from '../../atoms';
|
import { Input } from '../../atoms';
|
||||||
|
import { cn } from '@imphnen-frontend-service/utils';
|
||||||
|
|
||||||
type TInputType = 'text' | 'email';
|
type TInputType = 'text' | 'email';
|
||||||
type TInputSize = 'sm' | 'md' | 'lg';
|
type TInputSize = 'sm' | 'md' | 'lg';
|
||||||
@@ -17,6 +18,8 @@ type TInputFormProps = Omit<
|
|||||||
type?: TInputType;
|
type?: TInputType;
|
||||||
size?: TInputSize;
|
size?: TInputSize;
|
||||||
error?: string;
|
error?: string;
|
||||||
|
helperText?: string;
|
||||||
|
htmlFor?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const InputForm: FC<TInputFormProps> = ({
|
export const InputForm: FC<TInputFormProps> = ({
|
||||||
@@ -25,18 +28,35 @@ export const InputForm: FC<TInputFormProps> = ({
|
|||||||
type = 'text',
|
type = 'text',
|
||||||
size = 'md',
|
size = 'md',
|
||||||
error,
|
error,
|
||||||
|
helperText,
|
||||||
|
htmlFor,
|
||||||
|
className,
|
||||||
...rest
|
...rest
|
||||||
}): ReactElement => {
|
}): ReactElement => {
|
||||||
return (
|
return (
|
||||||
<div className="flex gap-[8px] flex-col">
|
<div className="flex gap-[8px] flex-col">
|
||||||
<div className="self-start text-p3 font-medium">{label}</div>
|
<label htmlFor={htmlFor} className="self-start text-p3 font-medium">
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
<Input
|
<Input
|
||||||
|
{...(htmlFor && { id: htmlFor })}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
type={type}
|
type={type}
|
||||||
size={size}
|
size={size}
|
||||||
error={error}
|
className={cn(
|
||||||
|
error &&
|
||||||
|
'border-danger-500 hover:border-danger-500 focus:outline-danger-500',
|
||||||
|
className
|
||||||
|
)}
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
|
{error ? (
|
||||||
|
<p className="text-danger-500 text-xs mt-1">{error}</p>
|
||||||
|
) : (
|
||||||
|
helperText && (
|
||||||
|
<p className="text-neutral-500 text-xs mt-1">{helperText}</p>
|
||||||
|
)
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user