Skip to content

Commit 48b5541

Browse files
feat(app): add input-otp demo (#96)
Co-authored-by: Brandy Smith <[email protected]>
1 parent 6ec4e18 commit 48b5541

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

src/components.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export namespace Components {
5050
}
5151
interface ComponentInput {
5252
}
53+
interface ComponentInputOtp {
54+
}
5355
interface ComponentItem {
5456
}
5557
interface ComponentItemGroup {
@@ -240,6 +242,12 @@ declare global {
240242
prototype: HTMLComponentInputElement;
241243
new (): HTMLComponentInputElement;
242244
};
245+
interface HTMLComponentInputOtpElement extends Components.ComponentInputOtp, HTMLStencilElement {
246+
}
247+
var HTMLComponentInputOtpElement: {
248+
prototype: HTMLComponentInputOtpElement;
249+
new (): HTMLComponentInputOtpElement;
250+
};
243251
interface HTMLComponentItemElement extends Components.ComponentItem, HTMLStencilElement {
244252
}
245253
var HTMLComponentItemElement: {
@@ -448,6 +456,7 @@ declare global {
448456
"component-icons": HTMLComponentIconsElement;
449457
"component-infinite-scroll": HTMLComponentInfiniteScrollElement;
450458
"component-input": HTMLComponentInputElement;
459+
"component-input-otp": HTMLComponentInputOtpElement;
451460
"component-item": HTMLComponentItemElement;
452461
"component-item-group": HTMLComponentItemGroupElement;
453462
"component-list": HTMLComponentListElement;
@@ -526,6 +535,8 @@ declare namespace LocalJSX {
526535
}
527536
interface ComponentInput {
528537
}
538+
interface ComponentInputOtp {
539+
}
529540
interface ComponentItem {
530541
}
531542
interface ComponentItemGroup {
@@ -610,6 +621,7 @@ declare namespace LocalJSX {
610621
"component-icons": ComponentIcons;
611622
"component-infinite-scroll": ComponentInfiniteScroll;
612623
"component-input": ComponentInput;
624+
"component-input-otp": ComponentInputOtp;
613625
"component-item": ComponentItem;
614626
"component-item-group": ComponentItemGroup;
615627
"component-list": ComponentList;
@@ -668,6 +680,7 @@ declare module "@stencil/core" {
668680
"component-icons": LocalJSX.ComponentIcons & JSXBase.HTMLAttributes<HTMLComponentIconsElement>;
669681
"component-infinite-scroll": LocalJSX.ComponentInfiniteScroll & JSXBase.HTMLAttributes<HTMLComponentInfiniteScrollElement>;
670682
"component-input": LocalJSX.ComponentInput & JSXBase.HTMLAttributes<HTMLComponentInputElement>;
683+
"component-input-otp": LocalJSX.ComponentInputOtp & JSXBase.HTMLAttributes<HTMLComponentInputOtpElement>;
671684
"component-item": LocalJSX.ComponentItem & JSXBase.HTMLAttributes<HTMLComponentItemElement>;
672685
"component-item-group": LocalJSX.ComponentItemGroup & JSXBase.HTMLAttributes<HTMLComponentItemGroupElement>;
673686
"component-list": LocalJSX.ComponentList & JSXBase.HTMLAttributes<HTMLComponentListElement>;

src/components/input-otp/input-otp.css

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Component, h, State } from '@stencil/core';
2+
import { toastController } from '@ionic/core';
3+
4+
@Component({
5+
tag: 'component-input-otp',
6+
styleUrl: 'input-otp.css'
7+
})
8+
export class InputOtp {
9+
otpRef: any;
10+
11+
@State() otpValue: string = '';
12+
13+
setFocus = () => {
14+
this.otpRef?.setFocus();
15+
}
16+
17+
componentDidLoad() {
18+
this.setFocus();
19+
}
20+
21+
handleOtpComplete = async (event: CustomEvent) => {
22+
const otp = event.detail.value;
23+
24+
const toast = await toastController.create({
25+
message: `Entered OTP: ${otp}`,
26+
duration: 1000,
27+
position: 'bottom'
28+
});
29+
30+
await toast.present();
31+
await toast.onDidDismiss();
32+
this.reset();
33+
}
34+
35+
reset = () => {
36+
if (this.otpRef) {
37+
this.otpRef.value = '';
38+
}
39+
if (document.activeElement instanceof HTMLElement) {
40+
document.activeElement.blur();
41+
}
42+
}
43+
44+
render() {
45+
return [
46+
<ion-header translucent={true}>
47+
<ion-toolbar>
48+
<ion-buttons slot="start">
49+
<ion-back-button defaultHref="/"></ion-back-button>
50+
</ion-buttons>
51+
<ion-title>Input OTP</ion-title>
52+
</ion-toolbar>
53+
</ion-header>,
54+
55+
<ion-content>
56+
<div class="ion-padding ion-text-center">
57+
<h2>Enter Verification Code</h2>
58+
<p>Please enter the 4-digit code sent to your device</p>
59+
60+
<ion-input-otp
61+
ref={el => (this.otpRef = el)}
62+
onIonComplete={this.handleOtpComplete}
63+
separators="2"
64+
class="ion-margin-top"
65+
>
66+
Didn't get the code? <a onClick={() => this.setFocus()}>Resend Code</a>
67+
</ion-input-otp>
68+
</div>
69+
</ion-content>,
70+
];
71+
}
72+
}

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<meta name="format-detection" content="telephone=no">
1313
<meta name="msapplication-tap-highlight" content="no">
1414

15-
<meta name="apple-mobile-web-app-capable" content="yes">
15+
<meta name="mobile-web-app-capable" content="yes">
1616
<meta name="apple-mobile-web-app-title" content="Ionic PWA">
1717
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
1818

src/utils/component-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const getComponents = () => {
1717
{ name: 'Icons', icon: '/assets/component-icon.svg', id: 'icons' },
1818
{ name: 'Infinite Scroll', icon: '/assets/component-icon.svg', id: 'infinite-scroll' },
1919
{ name: 'Input', icon: '/assets/component-icon.svg', id: 'input' },
20+
{ name: 'Input OTP', icon: '/assets/component-icon.svg', id: 'input-otp' },
2021
{ name: 'Item', icon: '/assets/component-icon.svg', id: 'item' },
2122
{ name: 'Item Group', icon: '/assets/component-icon.svg', id: 'item-group' },
2223
{ name: 'List', icon: '/assets/component-icon.svg', id: 'list' },

0 commit comments

Comments
 (0)