Skip to content

Field Types

Anhanga provides factory functions for each field type. All fields use the builder pattern — chain methods to configure behavior.

Field Factories

FactoryData TypeComponentDescription
text()stringtextText input with optional kind specialization
number()numbernumberNumeric input
currency()numbercurrencyCurrency input with prefix and precision
date()stringdateDate picker
datetime()stringdatetimeDate and time picker
toggle()booleantoggleToggle switch
checkbox()booleancheckboxCheckbox
select()V (generic)selectDropdown selection
file()FilefileFile upload
image()FileimageImage upload

Common Methods

Every field type inherits these methods from FieldDefinition<T>:

MethodDescription
.width(n)Form width (0–100)
.height(n)Form height
.hidden()Hide the field
.disabled()Disable the field
.required()Mark as required (adds validation)
.order(n)Sort order in the form
.default(value)Default value
.group(name)Assign to a group
.scopes(...scopes)Whitelist scopes
.excludeScopes(...scopes)Blacklist scopes
.states(...states)Allowed visual states
.column(config?)Show as table column
.filterable()Enable table filtering
.sortable(sortable?)Enable table sorting

Type-Specific Methods

text()

typescript
import { text, Text } from '@anhanga/core'

text()
  .kind(Text.Email)   // specialize the text input
  .minLength(3)        // minimum character length
  .maxLength(100)      // maximum character length
  .pattern(/^\d{5}$/, 'Must be 5 digits')  // regex validation

Text Kinds

Specialize text fields with .kind():

KindDescription
Text.EmailEmail input
Text.PhonePhone mask
Text.UrlURL input
Text.CpfCPF mask (Brazilian ID)
Text.CnpjCNPJ mask (Brazilian company ID)
Text.CepCEP mask (Brazilian postal code)
Text.StreetStreet input
Text.CityCity input

number()

typescript
import { number } from '@anhanga/core'

number()
  .min(0)          // minimum value
  .max(999)        // maximum value
  .precision(2)    // decimal places

currency()

typescript
import { currency } from '@anhanga/core'

currency()
  .min(0)
  .max(999999)
  .precision(2)     // decimal places
  .prefix('$')      // currency symbol prefix

date() / datetime()

typescript
import { date, datetime } from '@anhanga/core'

date()
  .min('2020-01-01')   // minimum date
  .max('2030-12-31')   // maximum date

datetime()
  .min('2020-01-01T00:00')
  .max('2030-12-31T23:59')

file() / image()

typescript
import { file, image } from '@anhanga/core'

file()
  .accept('.pdf,.doc,.docx')   // accepted file types
  .maxSize(5 * 1024 * 1024)    // max file size in bytes

image()
  .accept('.png,.jpg,.webp')
  .maxSize(2 * 1024 * 1024)

select()

typescript
import { select } from '@anhanga/core'

// Generic over the option value type
select<string>()
select<number>()

toggle() / checkbox()

No type-specific methods. Use common methods only:

typescript
import { toggle, checkbox } from '@anhanga/core'

toggle().default(true).width(20)
checkbox().default(false)

Column Configuration

Mark fields as table columns with .column():

typescript
text().column()              // default column
text().column({ width: 200, align: 'center' })  // with options
text().column().sortable()   // sortable column
text().column().filterable() // filterable column

Released under the MIT License.