UNPKG

react-signal-forms

Version:

A forms library focusing on performance and modular extensibility.

63 lines 3.39 kB
import { IFormContextLike } from "./formContext"; import { FormValues } from "./types"; import { KeyOf } from "./utils"; export interface FieldBase<TValue = unknown> { type?: string; name: string; label: string | null; defaultValue?: TValue; } export type TextField = FieldBase<string>; export type NumberField = FieldBase<number>; export type BooleanField = FieldBase<boolean>; export interface SelectField extends FieldBase<string> { options: SelectItem[]; } export type SelectItem = { value: string; label: string; }; export type Field<TForm = any, TKey extends KeyOf<TForm> = KeyOf<TForm>, TFieldBase extends FieldBase<TForm[TKey]> = FieldBase<TForm[TKey]>, TParentForm extends IFormContextLike | never = never> = TFieldBase & { rules?: Array<FieldRule<TForm, TKey, TParentForm>>; }; export interface FieldRule<TForm = FormValues, _Key extends KeyOf<TForm> = KeyOf<TForm>, _ParentForm extends IFormContextLike | never = never> { pluginName: string; } export type FieldCollection<TForm = any> = { [Key in KeyOf<TForm>]: Field<TForm, Key, FieldBase<TForm[Key]>>; }; export declare const signalForm: <TForm>() => { withFields<TFields extends FieldCollection<TForm>>(build: (field: FieldBuilder<TForm, never>) => TFields): { [Key in KeyOf<TForm>]: TFields[Key]; }; }; type FieldBuilder<TForm, TParentForm extends IFormContextLike | never = never> = { <TKey extends KeyOf<TForm>>(name: TKey, properties?: Omit<Field<TForm, TKey, FieldBase<TForm[TKey]>, TParentForm>, "name">): FieldDescriptor<TForm, TKey, "name">; <TKey extends KeyOf<TForm>>(name: TKey, label: string, properties?: Omit<Field<TForm, TKey, FieldBase<TForm[TKey]>, TParentForm>, "name" | "label">): FieldDescriptor<TForm, TKey, "name" | "label">; }; type FieldDescriptor<TForm, TKey extends KeyOf<TForm>, TExcept extends string | never> = { [name in TKey]: Field<TForm, TKey, FieldBase<TForm[TKey]>>; } & { /** * Extends the field with an extended field type. */ as: <TFieldBase extends FieldBase<TForm[TKey]>>(properties: Omit<Field<TForm, TKey, TFieldBase>, TExcept>) => FieldItem<TForm, TKey, TFieldBase>; asArray: (properties: Omit<Field<TForm, TKey, ArrayFieldBase<AsArrayValueType<TForm[TKey]>>>, "type" | "name" | "label" | "fields"> & { fields: ArrayFieldBuilder<TForm, TKey>; }) => FieldItem<TForm, TKey, ArrayFieldBase<AsArrayValueType<TForm[TKey]>>>; /** * Configures the field as hidden. */ asHidden: () => FieldItem<TForm, TKey, FieldBase<TForm[TKey]>>; }; type FieldItem<TForm, TKey extends KeyOf<TForm>, TFieldBase extends FieldBase<TForm[TKey]>> = { [name in TKey]: Field<TForm, TKey, TFieldBase>; }; type AsArrayValueType<TValue> = TValue extends FormValues[] ? TValue : never; export interface ArrayFieldBase<TArray extends FormValues[] = FormValues[]> extends FieldBase<TArray> { type: "array"; fields: FieldCollection<ArrayItemType<TArray>>; } export type ArrayItemType<TArray> = TArray extends Array<infer TItem> ? TItem : never; type ArrayFieldBuilder<TForm, TKey extends KeyOf<TForm>> = (field: FieldBuilder<ArrayItemType<TForm[TKey]>, IFormContextLike<TForm, never>>) => FieldCollection<ArrayItemType<TForm[TKey]>>; export declare function isArrayField<TValue>(field: FieldBase<TValue>): field is ArrayFieldBase<TValue & FormValues[]>; export {}; //# sourceMappingURL=fields.d.ts.map