-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add OTP field component #802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohanchkrabrty
wants to merge
3
commits into
main
Choose a base branch
from
worktree-otp-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
apps/www/src/components/playground/otp-field-examples.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| 'use client'; | ||
|
|
||
| import { Field, Flex, OTPField, Text } from '@raystack/apsara'; | ||
| import { useState } from 'react'; | ||
| import PlaygroundLayout from './playground-layout'; | ||
|
|
||
| const renderSlots = (length: number, offset = 0) => | ||
| Array.from({ length }, (_, i) => ( | ||
| <OTPField.Input | ||
| key={i + offset} | ||
| aria-label={`Character ${i + 1 + offset} of ${length + offset}`} | ||
| /> | ||
| )); | ||
|
|
||
| function ControlledOTP() { | ||
| const [value, setValue] = useState(''); | ||
| return ( | ||
| <Flex direction='column' gap={3} align='start'> | ||
| <OTPField length={6} value={value} onValueChange={setValue}> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| <Text size='small'> | ||
| Current value: <code>{value || '(empty)'}</code> | ||
| </Text> | ||
| </Flex> | ||
| ); | ||
| } | ||
|
|
||
| function CompleteOTP() { | ||
| const [submitted, setSubmitted] = useState(''); | ||
| return ( | ||
| <Flex direction='column' gap={3} align='start'> | ||
| <OTPField length={6} onValueComplete={setSubmitted}> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| <Text size='small'> | ||
| {submitted ? `Submitted: ${submitted}` : 'Type all 6 digits to submit'} | ||
| </Text> | ||
| </Flex> | ||
| ); | ||
| } | ||
|
|
||
| export function OTPFieldExamples() { | ||
| return ( | ||
| <PlaygroundLayout title='OTPField'> | ||
| <Flex direction='column' gap={9}> | ||
| <Text>Default (6 digits):</Text> | ||
| <OTPField length={6}>{renderSlots(6)}</OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>4 digits:</Text> | ||
| <OTPField length={4}>{renderSlots(4)}</OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>With separator:</Text> | ||
| <OTPField length={6}> | ||
| {renderSlots(3)} | ||
| <OTPField.Separator /> | ||
| {Array.from({ length: 3 }, (_, i) => ( | ||
| <OTPField.Input | ||
| key={`b-${i}`} | ||
| aria-label={`Character ${i + 4} of 6`} | ||
| /> | ||
| ))} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Masked:</Text> | ||
| <OTPField length={6} mask> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Alphanumeric:</Text> | ||
| <OTPField length={6} validationType='alphanumeric'> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Default value:</Text> | ||
| <OTPField length={6} defaultValue='123456'> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Controlled (value / onValueChange):</Text> | ||
| <ControlledOTP /> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>onValueComplete:</Text> | ||
| <CompleteOTP /> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Disabled:</Text> | ||
| <OTPField length={6} disabled defaultValue='123'> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>Read-only:</Text> | ||
| <OTPField length={6} readOnly defaultValue='934821'> | ||
| {renderSlots(6)} | ||
| </OTPField> | ||
| </Flex> | ||
|
|
||
| <Flex direction='column' gap={9}> | ||
| <Text>With Field:</Text> | ||
| <Field | ||
| label='Verification code' | ||
| description='Enter the 6-digit code we sent to your device.' | ||
| > | ||
| <OTPField length={6}>{renderSlots(6)}</OTPField> | ||
| </Field> | ||
| </Flex> | ||
| </PlaygroundLayout> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| 'use client'; | ||
|
|
||
| import type { ComponentPropsType } from '@/components/demo/types'; | ||
| import { getPropsString } from '@/lib/utils'; | ||
|
|
||
| const renderInputs = ( | ||
| length: number | ||
| ) => `Array.from({ length: ${length} }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of ${length}\`} /> | ||
| ))`; | ||
|
|
||
| export const preview = { | ||
| type: 'code', | ||
| code: `<OTPField length={6}> | ||
| {${renderInputs(6)}} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| const getCode = (props: ComponentPropsType) => { | ||
| const { length = 6, ...rest } = props; | ||
| const slotCount = Number(length) || 6; | ||
| return `<OTPField length={${slotCount}}${getPropsString(rest)}> | ||
| {${renderInputs(slotCount)}} | ||
| </OTPField>`; | ||
| }; | ||
|
|
||
| export const playground = { | ||
| type: 'playground', | ||
| controls: { | ||
| length: { | ||
| type: 'select', | ||
| options: ['4', '6', '8'], | ||
| defaultValue: '6' | ||
| }, | ||
| validationType: { | ||
| type: 'select', | ||
| options: ['numeric', 'alpha', 'alphanumeric', 'none'], | ||
| defaultValue: 'numeric' | ||
| }, | ||
| mask: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| }, | ||
| disabled: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| }, | ||
| readOnly: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| }, | ||
| autoSubmit: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| } | ||
| }, | ||
| getCode | ||
| }; | ||
|
|
||
| export const separatorDemo = { | ||
| type: 'code', | ||
| code: `<OTPField length={6}> | ||
| {Array.from({ length: 3 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| <OTPField.Separator /> | ||
| {Array.from({ length: 3 }, (_, i) => ( | ||
| <OTPField.Input key={i + 3} aria-label={\`Character \${i + 4} of 6\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const maskedDemo = { | ||
| type: 'code', | ||
| code: `<OTPField length={6} mask> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const alphanumericDemo = { | ||
| type: 'code', | ||
| code: `<OTPField length={6} validationType="alphanumeric"> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const disabledDemo = { | ||
| type: 'code', | ||
| code: `<OTPField length={6} disabled defaultValue="123"> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const readOnlyDemo = { | ||
| type: 'code', | ||
| code: `<OTPField length={6} readOnly defaultValue="934821"> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const controlledDemo = { | ||
| type: 'code', | ||
| code: `function ControlledOTP() { | ||
| const [value, setValue] = React.useState(''); | ||
|
|
||
| return ( | ||
| <Flex direction="column" gap={4} align="start"> | ||
| <OTPField length={6} value={value} onValueChange={setValue}> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField> | ||
| <Text size="small">Current value: <code>{value || '(empty)'}</code></Text> | ||
| </Flex> | ||
| ); | ||
| }` | ||
| }; | ||
|
|
||
| export const onCompleteDemo = { | ||
| type: 'code', | ||
| code: `function CompleteOTP() { | ||
| const [submitted, setSubmitted] = React.useState(''); | ||
|
|
||
| return ( | ||
| <Flex direction="column" gap={4} align="start"> | ||
| <OTPField | ||
| length={6} | ||
| onValueComplete={(value) => setSubmitted(value)} | ||
| > | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField> | ||
| <Text size="small"> | ||
| {submitted ? \`Submitted: \${submitted}\` : 'Type all 6 digits to submit'} | ||
| </Text> | ||
| </Flex> | ||
| ); | ||
| }` | ||
| }; | ||
|
|
||
| export const customSanitizeDemo = { | ||
| type: 'code', | ||
| code: `<OTPField | ||
| length={4} | ||
| validationType="none" | ||
| inputMode="numeric" | ||
| sanitizeValue={(val) => val.replace(/[^0-3]/g, '')} | ||
| > | ||
| {Array.from({ length: 4 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 4\`} /> | ||
| ))} | ||
| </OTPField>` | ||
| }; | ||
|
|
||
| export const withFieldDemo = { | ||
| type: 'code', | ||
| code: `<Flex justify="center"> | ||
| <Field label="Verification code" description="Enter the 6-digit code we sent to your device."> | ||
| <OTPField length={6}> | ||
| {Array.from({ length: 6 }, (_, i) => ( | ||
| <OTPField.Input key={i} aria-label={\`Character \${i + 1} of 6\`} /> | ||
| ))} | ||
| </OTPField> | ||
| </Field> | ||
| </Flex>` | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix incorrect slot aria-label totals in separator example.
Line 11 generates labels like “Character 1 of 3” for the first group in a 6-character field, which gives incorrect context to screen readers. Pass the total field length separately.
Suggested fix
Also applies to: 58-66
🤖 Prompt for AI Agents