Rokkit 1.0.0-next.128

Field Mapping

Map any data shape to components with the fields prop

The fields prop is Rokkit's core concept. Instead of reshaping your data to match component contracts, you tell components which fields in your data map to which roles.

The Problem

Most UI libraries force you to transform your API data into their expected shape. If your API returns { name, id } but the component expects { label, key }, you write adapter code everywhere.

javascript
Highlighting code...

The Rokkit Way

Pass a fields object that maps component roles to your data's property names:

svelte
Highlighting code...

Standard Field Roles

RolePurposeComponents
textDisplay labelAll
valueUnique identifier for selectionAll
iconIcon class nameList, Menu, Select, Tabs
childrenNested itemsList, Tree, Menu
descriptionSecondary textList, Card
disabledItem disabled stateList, Menu, Select
componentPer-item Svelte componentList
snippetNamed snippet for per-item renderingList, Select

Default Mapping

When no fields prop is provided, Rokkit uses the field names directly. So if your data already has text and value properties, no mapping is needed.

svelte
Highlighting code...

Primitive Arrays

For arrays of strings or numbers, each item is used as both text and value automatically:

svelte
Highlighting code...

Nested Data

Map the children field for hierarchical data. Groups with children render as collapsible sections in List and Tree, or as option groups in Select and Menu.

svelte
Highlighting code...

Related

  • Snippets — Customize rendering with access to mapped fields
  • List — See field mapping in action
  • Select — Dropdown with mapped fields