import { Context } from "koa"; import { FormControl, FormControlContext } from "../controls/form-control.js"; import { SimpleInput } from "../controls/simple-input.js"; import { FormDataValue } from "../form-types.js"; import { FieldParseResult } from "./field.js"; import { SimpleFormField } from "./simple-form-field.js"; export class Coordinates< Required extends boolean, DefaultControl extends FormControl = SimpleInput, > extends SimpleFormField { async parse( _: Context, raw_value: FormDataValue ): Promise> { if (typeof raw_value == "string") { return { parsed_value: raw_value ?.split(",") .map((e) => parseFloat(e)) as [number, number], parsable: true, error: null, }; } if (Array.isArray(raw_value)) { return { parsed_value: raw_value.map((e) => parseFloat(e)) as [ number, number, ], parsable: true, error: null, }; } return { parsed_value: null, parsable: false, error: "should be an array or string", }; } async getSealiousCreateValue( fctx: FormControlContext ): Promise<[number, number] | undefined> { const { parsed } = await this.getParsedValue( fctx.ctx, fctx.data.raw_values ); return parsed || undefined; } 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, number] { return [0, 0]; } }