import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; type $RestProps = SvelteHTMLElements["div"]; type $Props = { /** * Specify the number of input segments to render * @default 4 */ count?: number; /** * The concatenated code value. * * Derived from `code` (i.e. `code.join("")`); bind to read the assembled * value. When `complete` is `true`, its length equals `count`. Each * character matches `pattern` when set, otherwise the active `type`: * `0-9` for `"numeric"`, `a-zA-Z0-9` for `"alphanumeric"`. Original casing * is preserved regardless of `uppercase`, which only affects the visual * rendering. * @default "" */ value?: string; /** * The individual segment characters. * * `code` is the source of truth; its length tracks `count`. Each element is * either an empty string (unfilled segment) or a single character matching * `pattern` when set, otherwise the active `type`: `0-9` for `"numeric"`, * `a-zA-Z0-9` for `"alphanumeric"`. * Follows the segments when the owning form resets. * @default [] */ code?: string[]; /** * Specify the type of allowed characters. * * `"numeric"` allows `0-9`; `"alphanumeric"` allows `a-z`, `A-Z`, `0-9`. * When `pattern` is set, `pattern` decides which characters are accepted, * and `type` still sets each segment's `inputmode` (`"numeric"` shows a * numeric keypad on touch devices). * @default "numeric" */ type?: "numeric" | "alphanumeric"; /** * Override the per-character validation used for typing and paste. * * Accepts a `RegExp` or a string compiled with `new RegExp(...)`. The * pattern is tested against each individual character, not the full * assembled value. When unset, `type` selects the preset (`"numeric"` or * `"alphanumeric"`). Set `type` to `"alphanumeric"` when the pattern * accepts letters, so touch keyboards offer them. */ pattern?: RegExp | string | undefined; /** * Set to `true` to visually display the characters in uppercase * while retaining the original casing of `value` and `code`. * * Only affects letters, so it is most useful with `type="alphanumeric"`. * @default false */ uppercase?: boolean; /** * Set to `true` to mask the segments. * * Each segment is masked when it is not focused, revealing its character * only while focused. * @default false */ mask?: boolean; /** * `true` when every segment is filled. * @default false */ complete?: boolean; /** * Set to `true` to disable the input * @default false */ disabled?: boolean; /** * Set the size of the input. * Inherits the parent `Form` size when unset, otherwise `"default"`. * Also accepts Carbon v11's `"md"` (`"default"`) and `"lg"` (`"xl"`). * @default "default" */ size?: "default" | "xs" | "sm" | "xl" | "md" | "lg" | undefined; /** * Specify the label text * @default "" */ labelText?: string; /** * Set to `true` to visually hide the label text * @default false */ hideLabel?: boolean; /** * Specify the helper text * @default "" */ helperText?: string; /** * Placeholder shown in empty segments. * * Defaults to a placeholder in the fluid variant; non-fluid segments have no * placeholder unless this prop is set. */ placeholder?: string | undefined; /** * Set to `true` to indicate an invalid state * @default false */ invalid?: boolean; /** * Specify the invalid state text * @default "" */ invalidText?: string; /** * Set to `true` to indicate a warning state * @default false */ warn?: boolean; /** * Specify the warning state text * @default "" */ warnText?: string; /** * Set to `true` to mark the field as required * @default false */ required?: boolean; /** * Specify a name attribute for native form participation. * * When set, a hidden input mirrors the assembled `value` so the code is * included in FormData / form submissions. `required` stays on each * segment, since hidden inputs take no part in constraint validation. */ name?: string | undefined; /** * Specify the id of a form element outside the component to associate the * segments and the hidden `name` input with. */ form?: string | undefined; /** * Set to `true` to use the read-only variant * @default false */ readonly?: boolean; /** * Set to `true` to use the fluid variant. * Inherited from the parent `FluidForm` context, * so it does not need to be set when used inside `FluidForm`. * @default false */ fluid?: boolean; /** * Set to `true` to select a segment's value when it receives focus, * including on click. Without this prop, browsers typically select the * value only on keyboard focus (for example, via Tab). * @default false */ selectTextOnFocus?: boolean; /** * Override the accessible label of each segment. * * `position` is 1-based. The default returns, for example, * `"Verification code digit 1 of 4"`, using `"Pin code"` when `labelText` * is empty and `"character"` instead of `"digit"` when `type` is * `"alphanumeric"`. * @default (position, count, labelText, type) => `${labelText || "Pin code"} ${type === "numeric" ? "digit" : "character"} ${position} of ${count}` */ segmentLabelText?: (position: number, count: number, labelText: string, type: "numeric" | "alphanumeric") => string; /** * Set an id for the input group * @default uniqueId() */ id?: string; /** * Obtain a reference to the outer element. * @default null */ ref?: null | HTMLDivElement; labelChildren?: (this: void) => void; [key: `data-${string}`]: unknown; }; export type PinCodeInputProps = Omit<$RestProps, keyof $Props> & $Props; export default class PinCodeInput extends SvelteComponentTyped< PinCodeInputProps, { change: CustomEvent<{ value: string; code: string[] }>; clear: CustomEvent; complete: CustomEvent<{ value: string; code: string[] }>; paste: CustomEvent<{ value: string }>; }, { labelChildren: Record } > { /** * Focus the first input segment. */ focusFirstInput: (options?: { selectTextOnFocus?: boolean }) => void; /** * Focus the last input segment. */ focusLastInput: (options?: { selectTextOnFocus?: boolean }) => void; /** * Focus the first empty input segment, or the last if all are filled. */ focusNextEmptyInput: (options?: { selectTextOnFocus?: boolean }) => void; /** * Focus the next input segment from the currently focused segment. * Does not wrap when the last segment is focused. */ focusNext: (options?: { selectTextOnFocus?: boolean }) => void; /** * Clear all segments programmatically. * By default, focus is not moved. Set `options.focus` to `true` to focus the first segment after clearing. */ clear: (options?: { focus?: boolean }) => void; }