BASuite

BAFormElement

A dynamic form builder that renders various input types based on a configurable schema. Supports validation, masking, custom body content, loading states, disabled mode, save button alignment, and full two-way model binding.

Example:


"use client";

import { BAFormElement, BAPera, formElement } from "basuite";
import { useState } from "react";

export default function Playground() {
  const [model, setModel] = useState({
    inputField: "Sample Text",
    firstName: "Muhammad",
    lastName: "Ahmed",
    cnic: "4210119738169",
    password: "123456",
    email: "",
    datePickerField: "2025-01-01",
    selectField: 1,
    radioField: "female",
    checkboxField: false,
    booleanField: false,
    textareaField: "Sample Text",
    buttonField: undefined,
    customBodyField: undefined,
    headingField: "Sample Text",
    textField: "Sample Text",
    dragFileField: null,
    imageUploadField: null,
  });

  const elem: formElement[] = [
    { elementType: "input", col: 4, key: "firstName", label: "First Name" },
    { elementType: "input", col: 4, key: "lastName", label: "Last Name" },
    {
      elementType: "input",
      col: 4,
      key: "cnic",
      label: "CNIC",
      inputType: "maskinput",
      mask: "#####-#######-#"
    },
    {
      elementType: "input",
      col: 4,
      key: "email",
      label: "Email",
      inputType: "emailinput"
    },
    {
      elementType: "input",
      col: 4,
      key: "password",
      label: "Password",
      inputType: "passwordinput"
    },
    {
      elementType: "datepicker",
      col: 4,
      key: "datePickerField",
      label: "Date Picker"
    },
    {
      elementType: "select",
      col: 4,
      key: "selectField",
      label: "Select Field",
      showSearch: true,
      options: [
        { label: "Opt 1", value: 1 },
        { label: "Opt 2", value: 2 },
        { label: "Opt 3", value: 3 }
      ]
    },
    {
      elementType: "radio",
      col: 4,
      key: "radioField",
      label: "Radio Field",
      options: [
        { label: "Female", value: "female" },
        { label: "Male", value: "male" }
      ]
    },
    { elementType: "checkbox", col: 4, key: "checkboxField", label: "Checkbox Field" },
    { elementType: "boolean", col: 4, key: "booleanField", label: "Boolean Field" },
    { elementType: "textarea", col: 4, key: "textareaField", label: "Textarea Field" },
    { elementType: "button", col: 4, key: "buttonField", label: "Button Field" },
    {
      elementType: "custombody",
      col: 4,
      key: "customBodyField",
      label: "Custom Body",
      body: <BAPera>Custom Body</BAPera>
    },
    { elementType: "heading", col: 4, key: "headingField", label: "Heading" },
    { elementType: "text", col: 4, key: "textField", label: "Text Block" }
  ];

  return (
    <BAFormElement
      model={model}
      setModel={setModel}
      formElement={elem}
      saveButtonLabel="Register"
      saveButtonAlignment="start"
    />
  );
}

Props:

NameTypeDescriptionDefault
modelanyObject containing current values of all form fields. Each key must match a form element key.
setModel(value: any) => voidFunction used to update form field values. BAFormElement calls this when inputs change.
formElementformElement[]Array of field configurations defining how each form control is displayed.
onSaveClick() => voidCallback fired when the save button is clicked and validation passes.
saveLoaderbooleanShows loading spinner on the save button.false
disabledFormbooleanDisables all inputs and interactions inside the form.false
loadingbooleanShows skeleton loaders for all form fields.false
customButtonReact.ReactNodeAllows replacing the default save button with a custom button.
hideButtonbooleanHides the save button entirely.false
disabledSaveButtonbooleanDisables the save button manually.false
apiFunctionsanyOptional API function mapper passed down to fields like lookup.
saveButtonLabelstringButton label for the form submit action."Save"
saveButtonIconanyOptional icon for the save button.
saveButtonAlignment"start" | "center" | "end"Alignment of the save button inside the footer"end"
saveButtonType"primary" | "default" | "dashed" | "link" | "text"Ant Design button type for the save button."primary"

formElement:

NameTypeDescriptionOptional
col1 | 1.5 | 2 | ... | 12Column span size (1–12) used for grid layout.
elementType"input" | "datepicker" | "select" | "radio" | "checkbox" | "boolean" | "textarea" | "button" | "custombody" | "heading" | "text"Determines which component will be rendered.
keystringUnique model key used to bind the field value.
labelstringLabel displayed above the form element.
placeholderstringPlaceholder text for inputs and textareas.
textAlign"left" | "right" | "center"Text alignment inside the field.
inputType"numericinput" | "maskinput" | "passwordinput" | "otpinput" | "emailinput"Type of input component to render.
maskstringMask pattern for mask inputs.
lenghtnumberLength limit for OTP or masked fields.
otpMarkstringOptional prefix/suffix marker used in OTP input.
fillObjNamestringParent object key to fill from an API result.
apistringAPI endpoint for lookup/select fields.
fillObjanyObject returned from API responses used to fill data.
valueFieldstringField name to use as value in lookup or select components.
options{ value: any, label: string, disabled?: boolean }[]Options for select, radio, and checkbox groups.
ChangeEvanyCustom onChange event handler.
onCancelanyCancel event for modal-based fields.
blurEvanyBlur event handler.
focusEvanyFocus event handler.
requiredbooleanMarks the field as required for validation.
removeDecimalsbooleanRemoves decimals for numeric inputs.
isHidebooleanHides the field from UI.
multiplebooleanAllows multiple selection for select input.
maxlengthnumberMaximum character length.
disabledboolean | anyDisables the field.
loadingbooleanShows a loading state for selects/lookups.
showSearchbooleanEnables search inside dropdown selects.
onClickanyClick event handler for button or custom fields.
bodyanyReact Node used for custombody component.
controlleranyExternal controller for lookup / async components.
typeanyCustom field type used for dynamic rendering.
apiParamsanyParameters passed to API calls.
configanyExtra configuration for component behavior.
displayFieldanyField name used to display item label.
singleValueanyUsed for single-value selects with custom objects.
classNamestringCustom CSS class applied to the grid column.
multiSelectbooleanEnables multi-select dropdown behavior.
arrKeystringNested object key for multi-value structures.
validatePeriodbooleanValidates date range or month/year periods.
dataanyExtra data for select, lookup, or custom components.
showTimebooleanEnables time selection in datepicker.
validateCodeKeystringField key for matching verification codes.
validateControllerstringController used for validating dependent fields.
fieldAliasstringAlias used for renaming fields in payloads.
reqFieldsstring[]Conditionally required fields list.
useLookupbooleanEnables lookup component instead of regular select.
aliasModel{ actualKey: string; modelKey: string }[]Maps multiple model fields to a single API field.
paramsanyAdditional params for lookup API or computation.