import { Collection, FieldTypes as SealiousFieldTypes } from "sealious"; import { CollectionField } from "./collection-field.js"; import { SingleReferenceDropdown } from "../controls/single-reference.js"; import { FormControlContext } from "../controls/form-control.js"; import { Context } from "koa"; import { ExtractedFieldInfo } from "../../utils/extract-fields-from-collection.js"; import { toPascalCase } from "js-convert-case"; export class SingleReference< TargetCollection extends Collection, Required extends boolean, F extends SealiousFieldTypes.SingleReference, > extends CollectionField { getControl(): SingleReferenceDropdown { return new SingleReferenceDropdown(this, { label: this.label || this.name, getLabel: (item) => item.id, }); } async getSealiousCreateValue( fctx: FormControlContext ): Promise { const { parsed } = await this.getParsedValue( fctx.ctx, fctx.data.raw_values ); return parsed || undefined; } async sealiousValueToForm(_ctx: Context, value: string | null) { return value || undefined; } generateImportsForFieldList(field_info: ExtractedFieldInfo) { if (!field_info.target) { throw new Error("Expected 'target' attribute in field info"); } return [ { what: toPascalCase(field_info.target), from: "src/back/collections/collections.js", }, ]; } generateFieldDeclaration( field_info: ExtractedFieldInfo, vars: { form_field_types: string; sealious_field: string } ): string { if (!field_info.target) { throw new Error("Expected 'target' attribute in field info"); } return `new ${vars.form_field_types}.${ this.constructor.name }(${String(field_info.is_required)}, ${ vars.sealious_field })`; } }