UNPKG

@better-auth-ui/core

Version:

Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.

151 lines (150 loc) 7.43 kB
/** Data type of the additional field. */ export type AdditionalFieldType = "string" | "number" | "boolean" | "date"; /** Runtime value held by an `AdditionalField` (matches `AdditionalFieldType`). */ export type AdditionalFieldValue = string | number | boolean | Date; /** Value stored for an additional field while it is owned by a form. */ export type AdditionalFieldFormValue = AdditionalFieldValue | null; /** Default delay for custom additional-field validation after a value changes. */ export declare const DEFAULT_ADDITIONAL_FIELD_VALIDATION_DEBOUNCE_MS = 300; /** Runtime additional-field values keyed by their configured model names. */ export type AdditionalFieldFormValues = Record<string, AdditionalFieldFormValue>; /** UI rendering choice. Default is inferred from `AdditionalField.type`. */ export type AdditionalFieldInputType = "input" | "textarea" | "number" | "slider" | "switch" | "checkbox" | "select" | "combobox" | "date" | "datetime" | "hidden"; /** * Augmentation target for widening `AdditionalField` slot types * (`label`, `renderProps`, `renderResult`) in UI packages. * * @example * declare module "@better-auth-ui/core" { * interface AdditionalFieldRegister { label: ReactNode } * } */ export interface AdditionalFieldRegister { } /** Resolved label type. Defaults to `string`. */ export type AdditionalFieldLabel = AdditionalFieldRegister extends { label: infer L; } ? L : string; /** Resolved argument type for `AdditionalField.render`. */ export type AdditionalFieldRenderProps = AdditionalFieldRegister extends { renderProps: infer P; } ? P : { name: string; field: AdditionalField; value: AdditionalFieldFormValue; onBlur: () => void; onChange: (value: AdditionalFieldFormValue) => void; isInvalid?: boolean; errors?: unknown[]; isPending?: boolean; }; /** Resolved return type for `AdditionalField.render`. */ export type AdditionalFieldRenderResult = AdditionalFieldRegister extends { renderResult: infer R; } ? R : unknown; /** Option for a `select` input. */ export interface AdditionalFieldOption { label: AdditionalFieldLabel; value: string; } /** Configuration for a single additional user field. */ export interface AdditionalField { /** Field name. Used as the user object key and form input `name`. */ name: string; /** Data type of the field. */ type: AdditionalFieldType; /** Visible label rendered next to the input. */ label: AdditionalFieldLabel; /** Override the default UI rendering. @default inferred from `type` */ inputType?: AdditionalFieldInputType; /** Placeholder text. */ placeholder?: string; /** Content rendered as a prefix addon inside the input group. */ prefix?: AdditionalFieldLabel; /** Content rendered as a suffix addon inside the input group. */ suffix?: AdditionalFieldLabel; /** * `Intl.NumberFormat` options for number fields. Use `maximumFractionDigits` * (and optionally `minimumFractionDigits`) to allow decimals, or `style: "currency"` * / `style: "percent"` for richer formatting. */ formatOptions?: Intl.NumberFormatOptions; /** Minimum value. Applies to `number` and `slider` input types. */ min?: number; /** Maximum value. Applies to `number` and `slider` input types. */ max?: number; /** Step value. Applies to `number` and `slider` input types. */ step?: number; /** @default false */ required?: boolean; /** * Default value used to seed the input on the sign-up form. On the user * profile, the value is always re-seeded from the persisted session. */ defaultValue?: AdditionalFieldValue | null; /** * Render the field but exclude it from submission payloads. * @default false */ readOnly?: boolean; /** * Show a copy-to-clipboard button as a suffix. Input variant only. * @default false */ copyable?: boolean; /** Options for the select input type. */ options?: AdditionalFieldOption[]; /** * Custom client-side validation. Throw an `Error` (the `message` is shown * to the user) when invalid; return / resolve normally when valid. * * Receives the current typed form value. */ validate?: (value: AdditionalFieldValue | null | undefined) => void | Promise<void>; /** * Delay custom validation after a value changes. Set to `0` to validate * immediately. * @default 300 */ validateDebounceMs?: number; /** * Render on the sign-up form. Pass `"above"` to render between the `email` * and `password` fields; otherwise the field renders below the password * block. `true` is an alias for `"below"`. * @default false */ signUp?: boolean | "above" | "below"; /** Render on the user profile. @default true */ profile?: boolean; /** * Custom renderer. Replaces the host UI package's built-in input. Use the * form bindings supplied by the host package to read and update its value. */ render?: (props: AdditionalFieldRenderProps) => AdditionalFieldRenderResult; } /** Ordered list of `AdditionalField` configurations. */ export type AdditionalFields = AdditionalField[]; /** Resolve the initial form value for a configured additional field. */ export declare function getAdditionalFieldDefaultValue(field: AdditionalField): AdditionalFieldFormValue; /** Build collision-safe form defaults for a runtime list of fields. */ export declare function getAdditionalFieldDefaultValues(fields: readonly AdditionalField[]): AdditionalFieldFormValues; /** Keep only writable configured values when building an API payload. */ export declare function getAdditionalFieldSubmitValues(fields: readonly AdditionalField[], values: AdditionalFieldFormValues): AdditionalFieldFormValues; /** Validate required semantics without relying on native constraint state. */ export declare function validateAdditionalFieldRequired(field: AdditionalField, value: AdditionalFieldFormValue, requiredMessage: string): string | undefined; /** Run a configured custom validator and normalize thrown values as errors. */ export declare function validateAdditionalFieldValue(field: AdditionalField, value: AdditionalFieldFormValue): Promise<string | undefined>; /** * Convert a raw form value into the JS value Better Auth expects. * Returns `null` for blank input (explicit clear), `undefined` when omitted * or unparseable. Booleans always return `true`/`false`. */ export declare function parseAdditionalFieldValue(field: AdditionalField, raw: string | null | undefined): AdditionalFieldValue | null | undefined; /** Parse and validate a model's configured fields from submitted form data. */ export declare function parseAdditionalFieldValues(fields: AdditionalFields, formData: FormData): Promise<Record<string, AdditionalFieldValue | null>>; /** Seed field defaults from a Better Auth model returned by a query. */ export declare function fieldsWithModelValues(fields: AdditionalFields, model: Record<string, unknown>): AdditionalFields; /** Format a persisted additional-field value for compact read-only display. */ export declare function formatAdditionalFieldValue(value: unknown, languageTag?: string): string | undefined; /** Resolve the effective `inputType`, defaulting based on `field.type`. */ export declare function resolveInputType(field: AdditionalField): AdditionalFieldInputType;