toField
toField turns a V0 question into a Field: the control to render and its constraints.
const field = toField(question, options?)Reference
Section titled “Reference”toField(question, options?)
Section titled “toField(question, options?)”import { toField } from '@partner-sdk/core/view-model';
const field = toField(state.questions[name]);Parameters
Section titled “Parameters”question: aQuestionfromstate.questionsorq.questions.options.now(optional): fixes relative date bounds. DefaultDate.now().
Returns
Section titled “Returns”A Field. Every field has:
name: key forsession.editandfieldState.field: the submit key.label,hint?: HTML.required.invalidMessage?: the question’s own error copy.commit:"blur" | "change" | "none": when to callsession.flush().
Plus, by kind:
kind |
Extra fields |
|---|---|
text |
format (text, email, tel, zip), inputType, autoComplete?, inputMode?, minLength?, maxLength?, pattern?, placeholder? |
number |
prefix? ("$"), min?, max?, integer |
date |
min?, max? (YYYY-MM-DD) |
choice |
options ({ value, label }[]), presentation (radio for 4 or fewer, else select) |
multi |
options, presentation (group or checkbox), description? |
search |
search: the trade search template |
info |
Caveats
Section titled “Caveats”- Cached per question object, so the result is referentially stable.
- Unknown question types fall back to
text.
encode(question, value)
Section titled “encode(question, value)”Converts a UI value to a wire value: strips , $ and spaces from currency, phones to E.164, trims zips. The session calls this for you; use it only with the raw client.
Map kinds to your components
Section titled “Map kinds to your components”const renderers: Record<Field['kind'], (f: any) => HTMLElement> = { text: TextInput, number: NumberInput, date: DateInput, choice: (f) => (f.presentation === 'radio' ? RadioGroup(f) : Select(f)), multi: CheckboxGroup, search: TradeSearch, info: InfoBlock,};const el = renderers[field.kind](field);Troubleshooting
Section titled “Troubleshooting”Labels show raw tags
Section titled “Labels show raw tags”Labels and hints are HTML. Render them as HTML (sanitised by your framework or a Trusted Types policy), not as text.