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-builder

Basic 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

Prop
Type
Description

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

  1. Type your forms - Always define TypeScript interfaces for form values

  2. Use Akival for validation - Consistent validation with meaningful error messages

  3. Group related fields - Use sections for better organization

  4. Use controlled mode sparingly - Only when external state sync is required

  5. Handle API errors - Display server-side validation with fieldErrors

  • Akiform - Component-based form building with React Hook Form integration

  • Akival - Validation library for form validation

Last updated

Was this helpful?