Products · Components

CodeInput

A field for a one-time code or PIN, with a box for each digit.

Check the code in onComplete, not onChange.

Children: Nothing

Example

The CodeInput component
view.dml
<View gap="small">
  <CodeInput label="Enter the code we sent you" length="6" value="{{ code }}" onChange="setCode" onComplete="verify" />
  <If condition="{{ checking }}">
    <Text tone="muted">Checking your code…</Text>
  </If>
</View>
controller.ts
export default definePage({
  initialState: { code: '', checking: false },
  mutations: {
    setCode: (_state, event: ValueEvent) => ({ code: event.detail.value }),
    // Runs once all 6 digits are in.
    verify: (_state, event: ValueEvent) => ({ code: event.detail.value, checking: true }),
  },
});

Props

PropTypeWhat it is
valuestring
lengthnumberNumber of digits. Default: 6.
labelstring
hintstringHelp shown under the field, which error replaces.
errorstringShown under the field in red, in place of hint.
securebooleanShows dots in place of the digits, as for a PIN.
disabledbooleanDims it, and the person can't use it.

Events

EventSendsNotes
onChange{ value } in detail
onComplete{ value } in detailWhen every box is filled.

Keep reading

On this page