@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
1 lines • 13.9 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../../../../src/components/forms/index.ts"],"sourcesContent":["/**\n * @frank-auth/react - Forms Components Index\n *\n * Main entry point for all form components. Exports all form fields,\n * validation utilities, and form management components.\n */\n\n// ============================================================================\n// Form Components\n// ============================================================================\n\n// Form wrapper and context\nimport {FormWrapper, type FormWrapperProps, useFormContext, useFormField} from './form-wrapper';\n// Input field components\nimport {PasswordConfirmationField, PasswordField, type PasswordFieldProps,} from './password-field';\nimport {EmailField, type EmailFieldProps,} from './email-field';\nimport {PhoneField, type PhoneFieldProps,} from './phone-field';\nimport {VerificationCode, type VerificationCodeProps} from './verification-code';\nimport FieldError, {type FieldErrorProps, useFieldError} from './field-error';\n\nexport {\n FormWrapper,\n useFormContext,\n useFormField,\n FormContext,\n type FormWrapperProps\n} from './form-wrapper';\n\n// Field error handling\nexport {\n FieldError,\n useFieldError,\n type FieldErrorProps\n} from './field-error';\n\nexport {\n PasswordField,\n PasswordConfirmationField,\n type PasswordFieldProps,\n type PasswordRules,\n type PasswordStrength,\n type PasswordRequirement\n} from './password-field';\n\nexport {\n EmailField,\n type EmailFieldProps,\n type EmailValidation\n} from './email-field';\n\nexport {\n PhoneField,\n type PhoneFieldProps,\n type Country\n} from './phone-field';\n\nexport {\n VerificationCode,\n type VerificationCodeProps\n} from './verification-code';\n\n// ============================================================================\n// Form Validation Utilities\n// ============================================================================\n\n/**\n * Common validation rules for forms\n */\nexport const ValidationRules = {\n email: /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/,\n phone: /^\\+?[\\d\\s\\-\\(\\)]+$/,\n password: {\n minLength: 8,\n maxLength: 128,\n requireUppercase: /[A-Z]/,\n requireLowercase: /[a-z]/,\n requireNumbers: /\\d/,\n requireSpecialChars: /[!@#$%^&*(),.?\":{}|<>_\\-+=\\[\\]\\\\;'\\/~`]/,\n },\n verificationCode: {\n numeric: /^\\d+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/,\n },\n};\n\n/**\n * Common validation functions\n */\nexport const ValidationHelpers = {\n isValidEmail: (email: string): boolean => ValidationRules.email.test(email),\n\n isValidPhone: (phone: string): boolean => ValidationRules.phone.test(phone),\n\n isStrongPassword: (password: string): boolean => {\n const rules = ValidationRules.password;\n return password.length >= rules.minLength &&\n password.length <= rules.maxLength &&\n rules.requireUppercase.test(password) &&\n rules.requireLowercase.test(password) &&\n rules.requireNumbers.test(password) &&\n rules.requireSpecialChars.test(password);\n },\n\n isValidVerificationCode: (code: string, length: number, type: 'numeric' | 'alphanumeric' = 'numeric'): boolean => {\n if (code.length !== length) return false;\n return type === 'numeric'\n ? ValidationRules.verificationCode.numeric.test(code)\n : ValidationRules.verificationCode.alphanumeric.test(code);\n },\n};\n\n// ============================================================================\n// Form Field Collections\n// ============================================================================\n\n/**\n * Collection of all form field components\n */\nexport const FormFields = {\n PasswordField,\n PasswordConfirmationField,\n EmailField,\n PhoneField,\n VerificationCode,\n} as const;\n\n/**\n * Collection of form utility components\n */\nexport const FormUtilities = {\n FormWrapper,\n FieldError,\n} as const;\n\n// ============================================================================\n// Form Hook Collections\n// ============================================================================\n\n/**\n * Collection of form hooks\n */\nexport const FormHooks = {\n useFormContext,\n useFormField,\n useFieldError,\n} as const;\n\n// ============================================================================\n// Form Type Collections\n// ============================================================================\n\n/**\n * Collection of form field prop types\n */\nexport type FormFieldProps = {\n PasswordField: PasswordFieldProps;\n EmailField: EmailFieldProps;\n PhoneField: PhoneFieldProps;\n VerificationCode: VerificationCodeProps;\n};\n\n/**\n * Collection of form utility prop types\n */\nexport type FormUtilityProps = {\n FormWrapper: FormWrapperProps;\n FieldError: FieldErrorProps;\n};\n\n// ============================================================================\n// Form Presets\n// ============================================================================\n\n/**\n * Common form configurations for different auth flows\n */\nexport const FormPresets = {\n signIn: {\n fields: ['email', 'password'],\n validation: {\n email: { required: true, validateFormat: true },\n password: { required: true, minLength: 1 },\n },\n },\n\n signUp: {\n fields: ['email', 'password', 'passwordConfirmation'],\n validation: {\n email: { required: true, validateFormat: true },\n password: { required: true, showStrength: true },\n passwordConfirmation: { required: true, mustMatch: 'password' },\n },\n },\n\n resetPassword: {\n fields: ['email'],\n validation: {\n email: { required: true, validateFormat: true },\n },\n },\n\n changePassword: {\n fields: ['currentPassword', 'newPassword', 'confirmPassword'],\n validation: {\n currentPassword: { required: true },\n newPassword: { required: true, showStrength: true },\n confirmPassword: { required: true, mustMatch: 'newPassword' },\n },\n },\n\n verifyEmail: {\n fields: ['verificationCode'],\n validation: {\n verificationCode: { required: true, length: 6, type: 'numeric' },\n },\n },\n\n verifyPhone: {\n fields: ['phone', 'verificationCode'],\n validation: {\n phone: { required: true, validateFormat: true },\n verificationCode: { required: true, length: 6, type: 'numeric' },\n },\n },\n\n setupMFA: {\n fields: ['verificationCode'],\n validation: {\n verificationCode: { required: true, length: 6, type: 'numeric' },\n },\n },\n\n verifyMFA: {\n fields: ['verificationCode'],\n validation: {\n verificationCode: { required: true, length: 6, type: 'numeric' },\n },\n },\n\n organizationInvite: {\n fields: ['email'],\n validation: {\n email: { required: true, validateFormat: true },\n },\n },\n} as const;\n\n// ============================================================================\n// Form Builder Utility\n// ============================================================================\n\n/**\n * Utility function to build forms from presets\n */\nexport function buildForm(preset: keyof typeof FormPresets, overrides: any = {}) {\n const presetConfig = FormPresets[preset];\n\n return {\n ...presetConfig,\n ...overrides,\n validation: {\n ...presetConfig.validation,\n ...overrides.validation,\n },\n };\n}\n\n// ============================================================================\n// Form State Management\n// ============================================================================\n\n/**\n * Generic form state interface\n */\nexport interface FormState {\n values: Record<string, any>;\n errors: Record<string, string | string[]>;\n touched: Record<string, boolean>;\n isSubmitting: boolean;\n isValid: boolean;\n isDirty: boolean;\n}\n\n/**\n * Form action types\n */\nexport type FormAction =\n | { type: 'SET_VALUE'; field: string; value: any }\n | { type: 'SET_ERROR'; field: string; error: string | string[] | null }\n | { type: 'SET_TOUCHED'; field: string; touched: boolean }\n | { type: 'SET_SUBMITTING'; isSubmitting: boolean }\n | { type: 'RESET_FORM'; initialValues?: Record<string, any> }\n | { type: 'CLEAR_ERRORS' };\n\n/**\n * Form reducer function\n */\nexport function formReducer(state: FormState, action: FormAction): FormState {\n switch (action.type) {\n case 'SET_VALUE':\n return {\n ...state,\n values: { ...state.values, [action.field]: action.value },\n isDirty: true,\n // Clear error when value changes\n errors: { ...state.errors, [action.field]: '' },\n };\n\n case 'SET_ERROR':\n return {\n ...state,\n errors: {\n ...state.errors,\n [action.field]: action.error || '',\n },\n };\n\n case 'SET_TOUCHED':\n return {\n ...state,\n touched: { ...state.touched, [action.field]: action.touched },\n };\n\n case 'SET_SUBMITTING':\n return {\n ...state,\n isSubmitting: action.isSubmitting,\n };\n\n case 'RESET_FORM':\n return {\n values: action.initialValues || {},\n errors: {},\n touched: {},\n isSubmitting: false,\n isValid: true,\n isDirty: false,\n };\n\n case 'CLEAR_ERRORS':\n return {\n ...state,\n errors: {},\n };\n\n default:\n return state;\n }\n}\n\n// ============================================================================\n// Export Default\n// ============================================================================\n\n// Export the most commonly used components as default\nexport default {\n FormWrapper,\n FieldError,\n PasswordField,\n EmailField,\n PhoneField,\n VerificationCode,\n ValidationHelpers,\n FormPresets,\n};"],"names":["ValidationRules","ValidationHelpers","email","phone","password","rules","code","length","type","FormFields","PasswordField","PasswordConfirmationField","EmailField","PhoneField","VerificationCode","FormUtilities","FormWrapper","FieldError","FormHooks","useFormContext","useFormField","useFieldError","FormPresets","buildForm","preset","overrides","presetConfig","formReducer","state","action"],"mappings":"0RAoEaA,EAAkB,CAC3B,MAAO,6BACP,MAAO,qBACP,SAAU,CACN,UAAW,EACX,UAAW,IACX,iBAAkB,QAClB,iBAAkB,QAClB,eAAgB,KAChB,oBAAqB,yCACzB,EACA,iBAAkB,CACd,QAAS,QACT,aAAc,gBAAA,CAEtB,EAKaC,EAAoB,CAC7B,aAAeC,GAA2BF,EAAgB,MAAM,KAAKE,CAAK,EAE1E,aAAeC,GAA2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