AkiForm Builder
AkiformBuilder provides a schema-driven approach to form creation, automatically rendering fields based on a declarative configuration.
It builds on top of Akiform and React Hook Form.
Installation
pnpm add @akinon/akiform-builderBasic Usage
import { AkiformBuilder, field, type FormField } from '@akinon/akiform-builder';
import { akival } from '@akinon/akival';
interface UserForm {
name: string;
email: string;
role: string;
}
const formFields: FormField<UserForm>[] = [
field<UserForm>()
.key('name')
.type('text')
.label('Name')
.placeholder('Enter name')
.validation(akival.string().required())
.build(),
field<UserForm>()
.key('email')
.type('text')
.label('Email')
.placeholder('Enter email')
.validation(akival.string().email().required())
.build(),
field<UserForm>()
.key('role')
.type('select')
.label('Role')
.placeholder('Select role')
.options([
{ label: 'Admin', value: 'admin' },
{ label: 'User', value: 'user' },
{ label: 'Guest', value: 'guest' }
])
.build()
];
export const UserFormPage = () => {
const handleSubmit = (values: UserForm) => {
console.log('Form values:', values);
};
return (
<AkiformBuilder
fields={formFields}
layout="vertical"
onSubmit={handleSubmit}
/>
);
};Props
fields
FormField<T>[]
Array of field definitions
onSubmit
(values: T) => void
Form submission handler
layout
'vertical' | 'horizontal' | 'inline'
Form layout
layoutOptions
LayoutOptions
Label/wrapper column configuration
showResetButton
boolean
Show reset button
onReset
FormEventHandler
Reset handler
controlled
boolean
Enable controlled mode
values
T
Controlled form values
onValueChange
(values: T) => void
Value change callback
submitButtonProps
ButtonProps
Submit button customization
resetButtonProps
ButtonProps
Reset button customization
fieldErrors
ApiFieldError[]
API field errors to display
onFieldErrorsClear
() => void
Clear field errors callback
Field Builder API
Use the field() builder for type-safe field definitions:
Field Types
Text
Number
Select
Date
Date with Time
Checkbox
Textarea
Custom
Layout Fields
Section
Group fields into collapsible sections:
Row and Column
Create multi-column layouts:
Field Array
Dynamic repeatable fields:
Conditional Fields
Use .config() for dynamic visibility and disabled states:
Controlled Mode
For external state management:
API Field Errors
Display server-side validation errors:
Complete Example
Best Practices
Type your forms - Always define TypeScript interfaces for form values
Use Akival for validation - Consistent validation with meaningful error messages
Group related fields - Use sections for better organization
Use controlled mode sparingly - Only when external state sync is required
Handle API errors - Display server-side validation with
fieldErrors
Related
Last updated
Was this helpful?

