import * as react from 'react'; import { EventHandler, SyntheticEvent, ComponentType, ReactNode, MouseEventHandler } from 'react'; type FormatValidator = (value: any) => boolean; /** * JSON Schema draft v09 removed default `format` validations. * * But we still maintain a few built-in ones for ease-of-use. */ declare const FORMAT_VALIDATORS: Record; type ValidationStringFormat = keyof typeof FORMAT_VALIDATORS | Extra; declare class JsonPointer { static from(value: string): JsonPointer; private tokens; constructor(tokens: string[]); toString(): string; toPath(): string[]; } type JsonPointerOrString = JsonPointer | string; /** * Use this function to define schemas in a type safe way. * * ```ts * defineSchema({ typo: 'string' }); * ``` * * It will detect invalid values like `typo` instead of `type` * in the example and other excess properties. */ declare function defineSchema(schema: ExactSchemaType): InferSchemaType; type FormBaseSchema = ObjectSchema; type SubmissionState = 'not-submitted' | 'submitting' | 'submitted' | 'failed'; type SubmitHandler = (state: { values: SchemaValueType; errors: Errors; isValid: boolean; }) => Promise | Promise | undefined | boolean; interface MessageWithArgs { message: string; args: Args; } type ErrorMessage = string | MessageWithArgs; interface FormRendererProps { name: string; schema: T; errors: Errors; values: SchemaValueType; submissionState: SubmissionState; clearSubmission: () => void; submissionErrors: { [key: string]: ErrorMessage; }; steps: FormStep[]; onSubmit: EventHandler>; revealAllErrors: (value: boolean) => void; } interface FormStep { fields: { [key in keyof SchemaValueType]: FieldProps; }; isValid: () => boolean; } interface FieldProps> { schema: T; id: string; name: string; path: JsonPointerOrString; value: Value; onChange: (e: FieldChangeEvent) => void; errors: Errors; texts?: Texts; disabled?: boolean; } interface FieldChangeEvent { target: { name: string; value: Value; }; } type ValidSchemaKeys = keyof Schema | keyof ObjectSchema | keyof ArraySchema; type ExactSchemaType = { [K in keyof T]: K extends ValidSchemaKeys ? T[K] : never; } & (T extends { type: 'string'; } ? StringSchema : T extends { type: 'number'; } ? NumberSchema : T extends { type: 'integer'; } ? IntegerSchema : T extends { type: 'boolean'; } ? BooleanSchema : T extends { type: 'null'; } ? NullSchema : T extends { type: 'array'; } ? ArraySchema : T extends { type: 'object'; } | { properties: any; } ? ObjectSchema : Schema); type MergeSchemaProperties = T extends Array> ? P & (T extends Array<{ if: any; then?: infer Then extends ObjectSchema; else?: infer Else extends ObjectSchema; properties?: infer BaseProps; }> ? BaseProps & (Then extends ObjectSchema ? ThenProps : {}) & (Else extends ObjectSchema ? ElseProps : {}) : {}) : never; type InferObjectSchemaType = T extends { properties: infer P extends { [key: string]: Schema; }; allOf?: infer A; anyOf?: infer B; oneOf?: infer C; then?: infer D extends ObjectSchema; else?: infer E extends ObjectSchema; } ? ObjectSchema

? MergeSchemaProperties : {}) & (B extends Array ? MergeSchemaProperties : {}) & (C extends Array ? MergeSchemaProperties : {}) & (D extends ObjectSchema ? D['properties'] : {}) & (E extends ObjectSchema ? E['properties'] : {})> : never; type InferSchemaType = T extends { type: 'string'; } ? StringSchema : T extends { type: 'number'; } ? NumberSchema : T extends { type: 'integer'; } ? IntegerSchema : T extends { type: 'boolean'; } ? BooleanSchema : T extends { type: 'null'; } ? NullSchema : T extends { type: 'array'; items: infer Items extends Schema; } ? ArraySchema : T extends { type: 'object'; } ? InferObjectSchemaType : T extends { properties: any; } ? InferObjectSchemaType : T extends { allOf: any; } ? ObjectSchema : T extends { if: any; } ? ObjectSchema : T extends { required: any; } ? ObjectSchema : T extends { const: string; } ? StringSchema : T extends { const: number; } ? NumberSchema : T extends { const: boolean; } ? BooleanSchema : T extends { const: null; } ? NullSchema : Schema; interface BitiformsExtras { propertyOrder?: string[]; widget?: string; widgetProps?: Record; placeholder?: string; autoComplete?: boolean | string; autoFocus?: boolean; errorMessages?: { [key: string]: string; }; } interface SchemaExtensions { $bitiforms?: BitiformsExtras; } interface StringSchema extends SchemaExtensions { type?: 'string'; title?: string; const?: string; description?: string; enum?: (string | null)[]; $enumNames?: string[]; format?: ValidationStringFormat; oneOf?: StringSchema[]; anyOf?: StringSchema[]; minLength?: number; maxLength?: number; default?: string | null; } interface NumberSchema extends SchemaExtensions { type?: 'number'; title?: string; description?: string; minimum?: number; maximum?: number; default?: number | null; const?: number | null; } interface IntegerSchema extends SchemaExtensions { type?: 'integer'; title?: string; description?: string; minimum?: number; maximum?: number; default?: number | null; const?: number | null; } interface ObjectSchema extends SchemaExtensions { type?: 'object'; title?: string; description?: string; properties?: Properties; allOf?: ObjectSchema[]; anyOf?: ObjectSchema[]; oneOf?: ObjectSchema[]; if?: ObjectSchema; then?: ObjectSchema; else?: ObjectSchema | null; required?: string[]; default?: any; } interface ArraySchema extends SchemaExtensions { type?: 'array'; title?: string; description?: string; items: Items; additionalItems?: boolean; minItems?: number; maxItems?: number; contains?: Schema; default?: any[]; } interface BooleanSchema extends SchemaExtensions { type?: 'boolean'; title?: string; description?: string; enum?: (boolean | null)[]; $enumNames?: string[]; default?: boolean | null; const?: boolean | null; } interface NullSchema extends SchemaExtensions { type?: 'null'; title?: string; description?: string; default?: null; const?: null; } interface ObjectSchemaI extends ObjectSchema<{ [key: string]: Schema; }> { } interface ArraySchemaI extends ArraySchema { } type Schema = StringSchema | NumberSchema | IntegerSchema | ObjectSchemaI | ArraySchemaI | BooleanSchema | NullSchema; type PrimitiveSchema = StringSchema | NumberSchema | IntegerSchema | BooleanSchema | NullSchema; type SchemaType = Schema['type']; type SchemaValueType = SchemaT extends StringSchema ? string | null : SchemaT extends NumberSchema ? number | null : SchemaT extends IntegerSchema ? number | null : SchemaT extends BooleanSchema ? boolean | null : SchemaT extends ArraySchema ? ArraySchemaValue : SchemaT extends ObjectSchema ? ObjectSchemaValue : never; interface ArraySchemaValue extends Array ? ObjectSchemaValue : SchemaValueType> { } type ObjectSchemaValue = { [K in keyof T]: SchemaValueType; }; interface ArraySchemaErrors extends Array> { } type ObjectSchemaErrors = { [K in keyof T]: Errors; } & { _all: ErrorMessage[]; }; type Errors = SchemaT extends PrimitiveSchema ? ErrorMessage[] : SchemaT extends ArraySchema ? { all: ErrorMessage[]; items: ArraySchemaErrors; } : SchemaT extends ObjectSchema ? ObjectSchemaErrors : never; declare function isObjectSchema(schema: Schema): schema is ObjectSchema; declare function assertIsObjectSchema(schema?: Schema, message?: string): asserts schema is ObjectSchema; declare function isStringSchema(schema: Schema): schema is StringSchema; declare function isNumberSchema(schema: Schema): schema is NumberSchema; declare function isIntegerSchema(schema: Schema): schema is IntegerSchema; declare function isBooleanSchema(schema: Schema): schema is BooleanSchema; declare function isArraySchema(schema: Schema): schema is ArraySchema; declare function isNullSchema(schema: Schema): schema is NullSchema; interface FormConfig { /** * Key-value pairs mapping a text keys to human readable * localized strings. */ texts: Texts; /** * Map of available field widgets. */ widgets: Record>; /** * JSON Schema format validators. */ formats: Record; /** * Resolver function used to decide which widget to render * based on the given schema. * * Note: The function *must* be ref-stable to avoid severe performance * regressions. */ resolveWidget: (schema: Schema, widgets: Record>) => ComponentType; } declare const defaultTexts: { 'wizard.next': string; 'wizard.prev': string; 'wizard.fixErrorsBeforeContinue': string; 'errors.required': string; 'errors.radiogroup.required': string; 'errors.checkbox.required': string; 'errors.minItems': string; 'errors.maxLength': string; 'errors.minLength': string; 'errors.minimum': string; 'errors.invalidFormat.email': string; 'arrayfield.remove': string; 'arrayfield.add': string; }; type DefaultTexts = typeof defaultTexts; interface Texts extends Partial { } declare function useFormConfig(): FormConfig; interface FormConfigProviderProps extends Partial { children: ReactNode; } /** * `FormConfigProvider` sets up configuration for * its child `Form`s. * * They can be nested, such that an inner form has different * config from a Form higher up in the tree. But in most use * cases you should stick to one high up in your component tree. */ declare function FormConfigProvider({ widgets, texts, formats, resolveWidget, children, }: FormConfigProviderProps): react.JSX.Element; declare const FormValuesContext: react.Context<{ [key: string]: any; }>; /** * This is rather expensive. Make sure you really need it before using it. * * It should mostly be used in custom widgets when you need to compute * some value based on other values in the form. See the example folders * for how this could be used to your advantage. */ declare function useFormValues(): SchemaValueType; /** * Used to communicate to all widgets that they should reveal their * errors. * * Widgets should use `useRevealErrors` to manage error reveal state * locally, but synchronize with the global state when it is changed * to `true´. * * Triggered on submit and when going to the next step in a wizard. */ declare const RevealErrorsContext: react.Context; /** * Hook for use in Widget components * * By default error messages should no be shown until a field * has been touched. In the `onBlur` handler of the input components, * widget implementations should call `revealErrors.enable()` returned * from this hook. * * Widgets using this hook will automatically make sure that the value * is in sync with the global one. */ declare function useRevealErrors(): { value: boolean; enable: () => void; }; type UpdateFn = (previousValues: T) => T; type ValueUpdater = (jsonPointer: JsonPointerOrString, updater: UpdateFn) => void; declare const UpdateValuesContext: react.Context | null>; declare function useUpdateValues(jsonPointer: JsonPointerOrString): (updater: UpdateFn) => void; declare const FormRendererContext: react.Context | null>; declare function useFormRenderer>(): FormRendererProps; type RenderCallback = (props: T) => ReactNode; interface RenderItemProps { id: number | string; path: JsonPointerOrString; index: number; fields: { [key: string]: FieldProps; }; onRemove: () => void; texts?: Texts; } interface BaseArrayFieldProps extends Omit>, 'value'> { value: Array<{ _id: number; } & SchemaValueType>; renderItem: RenderCallback; renderAddButton: RenderCallback; renderContainer: RenderCallback; } interface ArrayFieldProps extends BaseArrayFieldProps { } interface AddButtonProps { onClick: MouseEventHandler; itemCount: number; disabled?: boolean; texts?: Texts; } interface ArrayFieldContainerProps { children: ReactNode; name: string; id: string; errors: string | null; schema: BaseArrayFieldProps['schema']; texts?: Texts; itemCount: number; } declare function ArrayField({ id, name, path, value, schema, errors, disabled, texts, renderAddButton, renderItem, renderContainer, }: BaseArrayFieldProps): react.JSX.Element; interface ArrayFieldItemProps { path: string; parentId: number | string; parentName: string; onRemove: (itemId: number) => void; render: RenderCallback; index: number; texts?: Texts; id: number; item: ArrayFieldProps['value'][0]; schema: ArrayFieldProps['schema']['items']; errors: ArrayFieldProps['errors']; onFieldChange: (id: number, e: FieldChangeEvent) => void; } interface ObjectFieldProps extends FieldProps { } declare function ObjectField({ id, name, path, value, schema, errors, disabled, texts, }: ObjectFieldProps): react.JSX.Element; export { isNullSchema as $, RevealErrorsContext as G, UpdateValuesContext as L, assertIsObjectSchema as W, defineSchema as X, isArraySchema as Y, isBooleanSchema as Z, isIntegerSchema as _, isNumberSchema as a0, isObjectSchema as a1, isStringSchema as a2, useFormConfig as a3, useFormRenderer as a4, useFormValues as a5, useRevealErrors as a6, useUpdateValues as a7, ArrayField as g, FormConfigProvider as r, FormRendererContext as t, FormValuesContext as v, ObjectField as y }; export type { AddButtonProps as A, BitiformsExtras as B, ObjectSchemaErrors as C, ObjectSchemaValue as D, ErrorMessage as E, FieldProps as F, SchemaExtensions as H, InferSchemaType as I, SchemaType as J, SubmissionState as K, MessageWithArgs as M, NullSchema as N, ObjectSchema as O, PrimitiveSchema as P, ValidSchemaKeys as Q, RenderItemProps as R, Schema as S, Texts as T, UpdateFn as U, ValueUpdater as V, FormBaseSchema as a, SchemaValueType as b, SubmitHandler as c, FormRendererProps as d, StringSchema as e, FieldChangeEvent as f, ArrayFieldContainerProps as h, ArrayFieldItemProps as i, ArrayFieldProps as j, ArraySchema as k, ArraySchemaErrors as l, ArraySchemaValue as m, BooleanSchema as n, Errors as o, ExactSchemaType as p, FormConfig as q, FormConfigProviderProps as s, FormStep as u, IntegerSchema as w, NumberSchema as x, ObjectFieldProps as z };