ion-input-otp
Input OTP コンポーネントは、ワンタイムパスワード(OTP)入力用に設計された入力コンポーネントです。複数の入力ボックスと自動フォーカス管理をサポートし、 検証コードを入力するためのユーザーフレンドリーなインターフェイスを提供します。
基本的な使用法
このコンポーネントはデフォルトで4つの入力ボックスを提供します。入力ボックスの数は length
プロパティを使用してカスタマイズすることができます。
Type
type
プロパティは入力形式を決定し、数字または英数字の検証コードをサポートします。プロパティには2つの値を指定できる: number
と text
です。デフォルトでは type="number"
を使用し、数字認証コードを入力を受け付けます。 type="text"
を指定すると、英数字の入力を受け付ける。この柔軟性により、数字のみのコード(SMS 認証コードのような)や英数字のコード(バックアップコードやリカバリキーのような)など、さまざまな OTP フォーマットを扱うことができます
type
プロパティは inputmode
属性と pattern
属性の両方を自動的に設定します:
type="number"
の場合:inputmode="numeric"
を設定し、モバイルデバイスに数字キーボードを表示します。pattern="[\p{N}]"
を設定し、数字入力のみを許可します。
type="text"
の場合- 標準のキーボードを表示するには
inputmode="text"
を設定します。 pattern="[\p{L}\p{N}]"
を設定し、英数字の入力を許可する。
- 標準のキーボードを表示するには
パターンの検証やカスタマイズの詳細については、パターンのセクションを参照してください。
Shape
shape
プロパティは、入力ボックスのボーダー半径をコントロールし、丸みを帯びたコーナーやシャープなコーナーを作成します。
Fill
fill
プロパティは入力ボックスの背景のスタイルをコントロールし、縁取りまたは塗りつぶしの背景を提供します。
Size
size
プロパティは入力ボックスに異なるサイズのオプションを提供します。
Separators
separators
プロパティは1つ以上の入力ボックスの間に視覚的な仕切りを追加します。セパレータは3つの方法で定義できます:
- カンマで区切られた数値の文字列 (例:
"1,3"
) - 数字の配列 (例:
[1, 3]
) - 文字列
"all"
(すべての入力ボックスの間にセパレータを表示する)
数字はセパレータが表示されるべきインデックスを表します。例えば、"1,3"
は1番目と3番目の入力ボックスの後にセパレータを表示します。これは、入力ボックスを視覚的に区別してグループ化するために使用できますが、それでも値は1つです。
States
このコンポーネントは、入力ボックスの自動スタイリングのためのさまざまな状態をサポートしています:
- それぞれのプロパティによる
disabled
とreadonly
状態 - フォームバリデーション状態: CSSクラスによって視覚的に示される
valid
とinvalid
の状態 - Angularの場合:バリデーション状態は、フレームワークの値アクセサとフォームバリデーションによって自動的に管理されます
- 他のフレームワークの場合: 開発者は
ion-valid
、ion-invalid
、ion-touched
クラスを手動で追加する必要があります onvalid
スタイルはタッチされたときのみ表示されます (ion-touched
)ion-valid
スタイルはフォーカスされたときのみ表示されます (has-focus
)
Pattern
pattern
プロパティは正規表現を使ったカスタムバリデーションを可能にします。 文字列の正規表現またはunicodeの正規表現を使用して、許可される文字を検証することができます。 pattern
はサブセットだけでなく、値全体にマッチする必要があります。デフォルトのパターン
type="number"
:"[\p{N}]"
スクリプト内のあらゆる種類の数値文字にマッチします。type="text"
:"[\p{L}\p{N}]"
あらゆるスクリプトのあらゆる種類の数字と、あらゆる言語のあらゆる種類の文字にマッチします。
このコンポーネントは、指定されたパターンにマッチしない文字の入力を防ぎます。開発者は、特定の入力要件に一致する独自のパターン文字列を提供することで、これらのデフォルトをオーバーライドできます。
カスタム pattern
を使用する場合、type
プロパティがモバイルデバイスに表示されるキーボードを制御することを覚えておいてください:
- 数字のみのパターンには
type="number"
を使い、数字キーボードを表示します。 - 英数字キーボードを表示するには、文字を含むパターンには
type="text"
を使用します。
Theming
Colors
color
プロパティは入力ボックスのカラーパレットを変更します。アウトライン塗りつぶしの場合、このプロパティはキャレットカラー、ハイライトカラー、ボーダーカラーを変更します。 solid
塗りつぶしの場合、このプロパティはキャレットカラーとハイライトカラーを変更します。
The color
property does not change the text color of the input OTP. For that, use the --color
CSS property.
CSS Custom Properties
Input OTPはスコープされたカプセル化を使用しています。これは、実行時に各スタイルに追加のクラスを追加することで、CSSを自動的にスコープすることを意味します。CSSでスコープされたセレクタをオーバーライドするには、より高い特異性 セレクタが必要です。そのため、クラスを追加してカスタマイズすることをお勧めします。特定のスタイルが fill
に基づいて適用されるため、fill
のプロパティを個別にオーバーライドする必要があるかもしれません。
Accessibility
Keyboard Interactions
Input OTPのキーボードナビゲーションは、ARIA Authoring Practices Guide の複合ウィジェットに関する推奨に従っています。これは、1つのコントロールとして機能する複数のフォーカス可能な要素(入力ボックス)を含むため、複合ウィジェットとして扱われます。
コンポーネントが無効になっていない場合、これらのキーボード操作はすべての ion-input-otp
要素に適用されます。
Key | Description |
---|---|
Tab | When first tabbing into the component, focus moves to the first empty box. If all boxes are filled, focus moves to the last box. Once inside the component, tabbing moves to the next focusable element on the page. |
Shift + Tab | When tabbing backwards into the component, focus moves to the first empty box. If all boxes are filled, focus moves to the last box. Once inside the component, shift tabbing focus moves to the previous focusable element on the page. |
ArrowRight | Moves focus to the next input box, stopping at the first empty box. In RTL mode, moves focus back to any previous box that contains a value. |
ArrowLeft | Moves focus back to any previous box that contains a value. In RTL mode, moves focus to the next input box, stopping at the first empty box. |
Any character matching the pattern property | Fills the current box and automatically moves focus to the next empty box. If all boxes are filled, focus remains on the last box. If the current box has a value, override the value with the entered character. In RTL mode, input fills boxes from right to left. |
Backspace | In an empty box: moves focus back one box and clears its value. In a box with a value: clears that value. With values in boxes to the right: shifts them all one position to the left. In RTL mode, with values in boxes to the left: shifts them all one position to the right. |
Ctrl + V Cmd + V | Pastes content starting from the first box, regardless of which box is currently focused. All existing values are cleared before pasting. For example, if you have "1234" in all boxes and paste "56", the result will be "56" in the first two boxes with the remaining boxes empty. If the pasted content is longer than the available boxes, the extra characters are ignored. |
Properties
autocapitalize
Description | テキスト値がユーザーによって入力/編集される際に、自動的に大文字にするかどうか、またどのようにする かについて示します。利用可能なオプション: "off" , "none" , "on" , "sentences" , "words" , "characters" . |
Attribute | autocapitalize |
Type | string |
Default | 'off' |
color
Description | アプリケーションのカラーパレットから使用する色を指定します。デフォルトのオプションは以下の通りです。 "primary" , "secondary" , "tertiary" , "success" , "warning" , "danger" , "light" , "medium" , と "dark" です.色に関する詳しい情報は theming を参照してください。 |
Attribute | color |
Type | "danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined |
Default | undefined |
disabled
Description | true の場合、ユーザはInputと対話することができません。 |
Attribute | disabled |
Type | boolean |
Default | false |
fill
Description | 入力ボックスの塗りつぶし。"solid" の場合は背景色が付き、"outline" の場合は枠線付きで透過表示になります。 |
Attribute | fill |
Type | "outline" | "solid" | undefined |
Default | 'outline' |
inputmode
Description | どのキーボードを表示するかのブラウザへのヒント。指定可能な値は "none" , "text" , "tel" , "url" , "email" , "numeric" , "decimal" , "search" 。数値入力(type="number")には "numeric"、テキスト(type="text")には "text" を使用します。 |
Attribute | inputmode |
Type | "decimal" | "email" | "none" | "numeric" | "search" | "tel" | "text" | "url" | undefined |
Default | undefined |
length
Description | 表示する入力ボックスの数。 |
Attribute | length |
Type | number |
Default | 4 |
pattern
Description | 許可される文字の正規表現パターン文字列。型に応じたデフォルトがあります。数値(type="number")は "[\p{N}]" 、テキスト(type="text")は "[\p{L}\p{N}]" 。 |
Attribute | pattern |
Type | string | undefined |
Default | undefined |
readonly
Description | true の場合、ユーザーは値を変更することができません。 |
Attribute | readonly |
Type | boolean |
Default | false |
separators
Description | 入力ボックス間に区切り線を表示する場所。カンマ区切り文字列または数値配列で指定できます。例: "3" は 3 番目の入力ボックスの後に区切りを表示、[1,4] は 1 番目と 4 番目の後に表示、"all" はすべての入力ボックス間に表示します。 |
Attribute | separators |
Type | number[] | string | undefined |
Default | undefined |
shape
Description | 入力ボックスの形状。"round" は角の丸みを大きくし、"rectangular" は角丸なし、"soft" はやわらかい角丸になります。 |
Attribute | shape |
Type | "rectangular" | "round" | "soft" |
Default | 'round' |
size
Description | 入力ボックスのサイズ。 |
Attribute | size |
Type | "large" | "medium" | "small" |
Default | 'medium' |
type
Description | 入力ボックスに許可される入力種別。 |
Attribute | type |
Type | "number" | "text" |
Default | 'number' |
value
Description | 入力グループの値。 |
Attribute | value |
Type | null | number | string | undefined |
Default | '' |
Events
Name | Description | Bubbles |
---|---|---|
ionBlur | 入力グループのフォーカスが外れたときに発行されます。 | true |
ionChange | ユーザーが入力値を変更したときに ionChange イベントが発生します。ionInput と異なり、ionChange は入力途中ではなく変更が確定したときにのみ発生します。<ion-input-otp> の値が変更された後にフォーカスを失ったときにも発生します。プログラムで value を設定した場合には発生しません。 | true |
ionComplete | すべての入力ボックスが有効な値で埋められたときに発行されます。 | true |
ionFocus | 入力グループにフォーカスが当たったときに発行されます。 | true |
ionInput | ionInput イベントは、ユーザが入力値を変更するたびに発生する。ionChange イベントとは異なり、 ionInput イベントは入力値が変更されるたびに発生する。これは通常、ユーザが入力を行うたびに発生します。 テキスト入力を受け付ける要素(type=text , type=tel など)の場合、インターフェイスはInputEvent となります。その他の要素の場合、インターフェイスはEvent となります。編集時に入力がクリアされる場合、型は null となる。 | true |
Methods
setFocus
Description | 任意の入力ボックスにフォーカスを設定します。 |
Signature | setFocus(index?: number) => Promise<void> |
Parameters | index: - The index of the input box to focus (0-based). If provided and the input box has a value, the input box at that index will be focused. Otherwise, the first empty input box or the last input if all are filled will be focused. |
CSS Shadow Parts
No CSS shadow parts available for this component.
CSS Custom Properties
- iOS
- MD
Name | Description |
---|---|
--background | 入力ボックスの背景色 |
--border-color | 入力ボックスのボーダーカラー |
--border-radius | 入力ボックスのボーダー半径 |
--border-width | 入力ボックスのボーダー幅 |
--color | 入力テキストの色 |
--height | 入力ボックスの高さ |
--highlight-color-focused | フォーカスされたときの入力のハイライトの色 |
--highlight-color-invalid | 入力が無効な場合のハイライトの色 |
--highlight-color-valid | 有効時の入力のハイライトの色 |
--margin-bottom | 入力グループの下マージン |
--margin-end | 入力グループの左右方向に応じた右/左マージン |
--margin-start | 入力グループの左右方向に応じた左/右マージン |
--margin-top | 入力グループの上マージン |
--min-width | 入力ボックスの最小幅 |
--padding-bottom | 入力グループの下パディング |
--padding-end | 入力グループの左右方向に応じた右/左パディング |
--padding-start | 入力グループの左右方向に応じた左/右パディング |
--padding-top | 入力グループの上パディング |
--separator-border-radius | ボックス間セパレーターのボーダー半径 |
--separator-color | ボックス間セパレーターの色 |
--separator-height | ボックス間セパレーターの高さ |
--separator-width | ボックス間セパレーターの幅 |
--width | 入力ボックスの幅 |
Name | Description |
---|---|
--background | 入力ボックスの背景色 |
--border-color | 入力ボックスのボーダーカラー |
--border-radius | 入力ボックスのボーダー半径 |
--border-width | 入力ボックスのボーダー幅 |
--color | 入力テキストの色 |
--height | 入力ボックスの高さ |
--highlight-color-focused | フォーカスされたときの入力のハイライトの色 |
--highlight-color-invalid | 入力が無効な場合のハイライトの色 |
--highlight-color-valid | 有効時の入力のハイライトの色 |
--margin-bottom | 入力グループの下マージン |
--margin-end | 入力グループの左右方向に応じた右/左マージン |
--margin-start | 入力グループの左右方向に応じた左/右マージン |
--margin-top | 入力グループの上マージン |
--min-width | 入力ボックスの最小幅 |
--padding-bottom | 入力グループの下パディング |
--padding-end | 入力グループの左右方向に応じた右/左パディング |
--padding-start | 入力グループの左右方向に応じた左/右パディング |
--padding-top | 入力グループの上パディング |
--separator-border-radius |