dynamic-forms / Function

createField

Generic types:T

Creates a typed field configuration with helpful error messages for common mistakes.

This helper function provides early validation and clear error messages for common configuration pitfalls that would otherwise cause runtime errors.

Presentation

function createField(
  type: T,
  config: Omit<ExtractField<T>, "type">,
): ExtractField<T>;

Returns

ExtractField<T> -

A properly typed field configuration

Parameters

NameTypeDescription
type
T

The field type (e.g., 'input', 'select', 'group')

config
Omit<ExtractField<T>, "type">

The field configuration (excluding the 'type' property)

Example usage

// Basic usage
const nameField = createField('input', {
  key: 'name',
  label: 'Name',
  value: ''
});

// With validation
const emailField = createField('input', {
  key: 'email',
  label: 'Email',
  required: true,
  email: true,
  props: { type: 'email' }
});

// Select with options (at field level)
const countryField = createField('select', {
  key: 'country',
  label: 'Country',
  options: [{ label: 'USA', value: 'us' }]
});

// Slider with minValue/maxValue (at field level)
const ratingField = createField('slider', {
  key: 'rating',
  label: 'Rating',
  minValue: 1,
  maxValue: 10,
  step: 1,
  value: 5
});