Components

Input OTP

A one-time code in cells, typed, pasted or filled from a text message.

Previews are drawn in the theme you made.

Edit it
scheme
direction

Basic #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { OtpField, OtpFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ code: Schema.String })
const Message = defineMessageUnion({ Typed: { code: Schema.String } })

// One real input over the cells, which only show its characters: paste, the
// caret and the code a phone offers all stay the browser's. `groups` splits the
// cells into runs with a separator between them.
export default example({
  title: 'Basic',
  slots: OtpFieldSlots,
  recipe: Recipes.OtpField,
  state: {
    Model,
    Message,
    init: () => ({ model: { code: '' } }),
    update: (_, { code }) => ({ model: { code } }),
  },
  view: ({ style, model, onMessage, h }) =>
    OtpField.view(
      {
        label: 'Sign-in code',
        groups: [3, 3],
        value: model.code,
        onChange: code => onMessage(Message.Typed({ code })),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

A hidden code #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { OtpField, OtpFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ code: Schema.String })
const Message = defineMessageUnion({ Typed: { code: Schema.String } })

// `mask` hides the code: the input is a password field and each filled cell
// shows a dot, or the mark you give. The application still holds the real code.
export default example({
  title: 'A hidden code',
  slots: OtpFieldSlots,
  recipe: Recipes.OtpField,
  state: {
    Model,
    Message,
    init: () => ({ model: { code: '482' } }),
    update: (_, { code }) => ({ model: { code } }),
  },
  view: ({ style, model, onMessage, h }) =>
    OtpField.view(
      {
        label: 'Card PIN',
        length: 4,
        mask: true,
        value: model.code,
        onChange: code => onMessage(Message.Typed({ code })),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Letters and digits #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { OtpField, OtpFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ code: Schema.String })
const Message = defineMessageUnion({ Typed: { code: Schema.String } })

// `pattern: 'alphanumeric'` takes either, and offers a text keyboard; what is
// typed is kept to the pattern and the length before it is sent.
export default example({
  title: 'Letters and digits',
  slots: OtpFieldSlots,
  recipe: Recipes.OtpField,
  state: {
    Model,
    Message,
    init: () => ({ model: { code: '' } }),
    update: (_, { code }) => ({ model: { code } }),
  },
  view: ({ style, model, onMessage, h }) =>
    OtpField.view(
      {
        label: 'Invite code',
        length: 8,
        groups: [4, 4],
        pattern: 'alphanumeric',
        value: model.code,
        onChange: code => onMessage(Message.Typed({ code })),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Refused, disabled and read only #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { OtpField, OtpFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Model = Schema.Struct({ code: Schema.String })
const Message = defineMessageUnion({ Typed: { code: Schema.String } })

// Empty and filling are the same field at different values. `invalid` marks a
// refused code, `disabled` takes no input, and `readOnly` shows a code that
// cannot be changed. The one that takes input holds a code of its own.
export default example({
  title: 'Refused, disabled and read only',
  slots: OtpFieldSlots,
  recipe: Recipes.OtpField,
  state: {
    Model,
    Message,
    init: () => ({ model: { code: '482910' } }),
    update: (_, { code }) => ({ model: { code } }),
  },
  view: ({ style, model, onMessage, h }) =>
    stack(
      h,
      [
        { id: 'refused', label: 'Refused code', invalid: true },
        { id: 'off', label: 'Disabled', disabled: true },
        { id: 'fixed', label: 'Read only', readOnly: true },
      ].map(options =>
        OtpField.view(
          {
            ...options,
            value: model.code,
            onChange: code => onMessage(Message.Typed({ code })),
            style,
          },
          h,
        ),
      ),
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗