/* eslint @typescript-eslint/no-unused-vars: off */ import { FormData, FormDataValue } from "../form-types.js"; import { Context } from "koa"; import { predicates } from "@sealcode/ts-predicates"; import { FieldParseResult, FormField } from "./field.js"; import { SimpleInput } from "../controls/simple-input.js"; import { FormControl } from "../controls/controls.js"; import { CollectionItem } from "sealious"; import { FormControlContext } from "../controls/form-control.js"; export abstract class SimpleFormField< Required extends boolean, DefaultControl extends FormControl = SimpleInput, Parsed = string, > extends FormField { predicate = predicates.string; public placeholder: string; constructor( required: Required, public type: string = "text" ) { super(required); } async sealiousValueToForm( _ctx: Context, value: unknown ): Promise { if (value === null) { return ""; } // eslint-disable-next-line @typescript-eslint/no-base-to-string return String(value) as FormDataValue; } setPlaceholder(placeholder: string) { this.placeholder = placeholder; return this; } } export class TextBasedSimpleField< Required extends boolean, DefaultControl extends FormControl = SimpleInput, > extends SimpleFormField { async parse( _: Context, raw_value: FormDataValue ): Promise> { return { // eslint-disable-next-line @typescript-eslint/no-base-to-string parsed_value: (raw_value || "").toString(), parsable: true, error: null, }; } async getSealiousCreateValue( fctx: FormControlContext ): Promise { if (this.disabled) { return null; } const { parsed } = await this.getParsedValue( fctx.ctx, fctx.data.raw_values ); return parsed || ""; } async postSealiousCreate( _ctx: Context, // eslint-disable-next-line @typescript-eslint/no-explicit-any _created_item: CollectionItem, _formData: FormData ): Promise {} async postSealiousEdit( ctx: Context, // eslint-disable-next-line @typescript-eslint/no-explicit-any _edited_item: CollectionItem, _formData: FormData ): Promise {} getControl(): DefaultControl { return new SimpleInput(this, { label: this.label || this.name, type: this.type, }) as unknown as DefaultControl; } public getEmptyValue(): string { return ""; } } export class NumberBasedSimpleField< Required extends boolean, DefaultControl extends FormControl = SimpleInput, > extends SimpleFormField { async parse( _: Context, raw_value: FormDataValue ): Promise> { return { parsed_value: parseFloat(raw_value as string) || 0, parsable: true, error: null, }; } async getSealiousCreateValue( fctx: FormControlContext ): Promise { const { parsed } = await this.getParsedValue( fctx.ctx, fctx.data.raw_values ); return parsed || 0; } async postSealiousCreate(): Promise {} async postSealiousEdit(): Promise {} getControl(): DefaultControl { return new SimpleInput(this, { label: this.label || this.name, type: this.type, }) as unknown as DefaultControl; } public getEmptyValue(): number { return 0; } }