import type { BooleanField, Form, FormField, FormFieldGroup, ImageField, NumberField, ParagraphField, SelectField, StringField } from '@devvit/shared'; export type FormAction = 'CANCELED' | 'SUBMITTED'; export type FormEffectResponse = { action: 'CANCELED'; } | { action: 'SUBMITTED'; values: T; }; /** * A function that returns a form. You can use this to dynamically generate a form. * @example * ```ts * const formKey = Devvit.createForm((data) => ({ * fields: data.fields, * title: data.title, * }), callback); * * ... * * ui.showForm(formKey, { * fields: [{ type: 'string', name: 'title', label: 'Title' }] * title: 'My dynamic form' * }); * ``` * */ export type FormFunction = (data: T) => Form; export type FormToFormValues = FormFieldsToFormValues<(T extends FormFunction ? ReturnType : T)['fields']>; /** * Input is a FormField[], output is a * {fieldNameA: fieldTypeA, fieldNameB: fieldTypeB}. */ type FormFieldsToFormValues = T extends readonly [ infer Field extends FormField, ...infer Rest extends FormField[] ] ? FormFieldToFormValue & FormFieldsToFormValues : { [key: string]: any; }; /** Input is a FormField, output is a {fieldName: fieldType}. */ type FormFieldToFormValue = T extends BooleanField ? { [_ in T['name']]: boolean; } : T extends ImageField | ParagraphField | StringField ? FormFieldToRequiredFormValue : T extends NumberField ? FormFieldToRequiredFormValue : T extends SelectField ? { [_ in T['name']]: string[]; } : T extends FormFieldGroup ? FormFieldsToFormValues : never; /** * Input is a FormField, output is a {fieldName: fieldType} or * {fieldName?: fieldType}. */ type FormFieldToRequiredFormValue = T extends { required: true; } | { defaultValue: boolean | number | string; } ? { [_ in T['name']]: V; } : { [_ in T['name']]?: V; }; export {}; //# sourceMappingURL=form-types.d.ts.map