Keel v0.5.1 中文 GitHub

OtpInput

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/otp_input.go

Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/theme"
)

func init() {
	registerSection("otp_input", "inputs", func() core.Widget {
		msg := demoText("Enter a 6-digit code; pasting is supported", "输入 6 位验证码,可以直接粘贴")
		code := kit.OtpInput(demoText("Verification code", "验证码"), 6)
		code.OnComplete(func(s string) {
			if s == "123456" {
				msg = demoText("Verified", "验证通过")
			} else {
				code.SetError(demoText("Incorrect code; try 123456", "验证码错误,试试 123456"))
			}
		})
		pin := kit.OtpInput(demoText("PIN · 4 digits", "密码 PIN · 4 位"), 4).Masked(true).Groups(1).Size(40)
		pin.SetValue("1234")
		grouped := kit.OtpInput(demoText("Groups · 3–2–2", "分组 Groups · 3–2–2"), 7).Groups(3).Size(56)
		grouped.SetValue("1234567")
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(12).Items(el.Start).Child(code.Render(cx), el.Text(msg).TextColor(theme.Muted), pin.Render(cx), grouped.Render(cx))
		}))
	})
}

Enter the fixed-digit verification code in separate boxes.

code := kit.OtpInput("Verification code", 6).OnComplete(func(s string) { verify(s) })
  • Only numbers are accepted. After inputting, it will automatically jump to the next box. Backspace to return to the previous box. Pasting the entire verification code will fill all the boxes at once.
  • OnComplete is called when the last digit is entered; OnChange is called after each edit.
  • Implementation method: An invisible text box is covered on the grid, and focus, editing, and pasting are all handled by it. The grid is only responsible for display. Therefore the screen reader and Agent only see a text box.
  • Value() / SetValue (discard non-digits and extra digits), SetDisabled, SetError.

Masked(true) masks the visible number and semantic value, Masked(false) restores the display; the original value is still handed to the application through Value and the callback. Groups(n) sets the number of groups, limited to 1-digit number. The default is two groups (single digits are one group). When it is not divisible, the previous group has one more digit. For example, 7 digits divided into 3 groups are 3–2–2. The spacing between groups is twice the spacing within the grid.

Size(dp) sets the grid height, the default is 48dp; the grid width, spacing and font size change proportionally, ignoring non-positive numbers and non-finite numbers. Modifying the layout or mask does not change the value and does not trigger the edit callback.

Agent: role textbox, the name is the label; value in normal mode is the entered number, and the mask mode is dots of equal length.

Verify: go run ./examples/components -section otp_input, add -theme dark to check the dark theme.

Each bit in the narrow container shrinks to the same width, and the actual editing area is consistent with the width of the visible grid. The default six digits are still fully displayed under the 224dp content width; there are regression tests for the 1× / 2× number boundary and the entire paragraph pasting.