Skip to content
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

feat: Implement OTP Field #148

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/major-groups-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@formwerk/core': minor
---

feat: add OTP Field
2 changes: 2 additions & 0 deletions packages/core/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const FieldTypePrefixes = {
DateTimeField: 'dtf',
DateTimeSegment: 'dts',
Calendar: 'cal',
OTPField: 'otp',
OTPSlot: 'otps',
} as const;

export const NOOP = () => {};
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './useSelect';
export * from './useComboBox';
export * from './useHiddenField';
export * from './useCustomField';
export * from './useOtpField';
export * from './useDateTimeField';
export * from './useCalendar';
export * from './usePicker';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/useCalendar/useCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { fromDateToCalendarZonedDateTime, useTemporalStore } from '../useDateTimeField/useTemporalStore';
import { PickerContextKey } from '../usePicker';
import { registerField } from '@formwerk/devtools';
import { useConstraintsValidator } from '../validation/useContraintsValidator';
import { useConstraintsValidator } from '../validation/useConstraintsValidator';

export interface CalendarProps {
/**
Expand Down Expand Up @@ -112,12 +112,12 @@
/**
* The form field to use for the calendar.
*/
field?: FormField<any>;

Check warning on line 115 in packages/core/src/useCalendar/useCalendar.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

/**
* The schema to use for the calendar.
*/
schema?: StandardSchema<any>;

Check warning on line 120 in packages/core/src/useCalendar/useCalendar.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}

export function useCalendar(_props: Reactivify<CalendarProps, 'field' | 'schema'>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/useDateTimeField/useDateTimeField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ZonedDateTime, Calendar } from '@internationalized/date';
import { useInputValidity } from '../validation';
import { createDisabledContext } from '../helpers/createDisabledContext';
import { registerField } from '@formwerk/devtools';
import { useConstraintsValidator } from '../validation/useContraintsValidator';
import { useConstraintsValidator } from '../validation/useConstraintsValidator';

export interface DateTimeFieldProps {
/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/useDateTimeField/useDateTimeSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface DateTimeSegmentDomProps {
inputmode?: string;
autocorrect?: string;
enterkeyhint?: string;
autocomplete?: string;
'aria-valuemin'?: number;
'aria-valuemax'?: number;
'aria-valuenow'?: number;
Expand Down Expand Up @@ -207,6 +208,7 @@ export function useDateTimeSegment(_props: Reactivify<DateTimeSegmentProps>) {
'aria-label': isNonEditable() ? undefined : toValue(props.type),
'aria-readonly': toValue(props.readonly) ? true : undefined,
autocorrect: isNonEditable() ? undefined : 'off',
autocomplete: isNonEditable() ? undefined : 'off',
spellcheck: isNonEditable() ? undefined : false,
enterkeyhint: isNonEditable() ? undefined : isLast() ? 'done' : 'next',
inputmode: 'none',
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/useOtpField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './useOtpField';
export * from './useOtpSlot';
export * from './types';
19 changes: 19 additions & 0 deletions packages/core/src/useOtpField/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { InjectionKey } from 'vue';

export type OtpSlotAcceptType = 'all' | 'numeric' | 'alphanumeric';

export interface OtpSlotRegistration {
id: string;
focusNext(): void;
focusPrevious(): void;
setValue(value: string): void;
handlePaste(event: ClipboardEvent): void;
isLast(): boolean;
}

export interface OtpContext {
useSlotRegistration(): OtpSlotRegistration;
getMaskCharacter(): string;
}

export const OtpContextKey: InjectionKey<OtpContext> = Symbol('otp-context');
Loading
Loading