import type { BaseInputorContext, BaseInputorControllerOptions, BaseInputorSchema, InputorControllerInitializeHooks, } from "./base.ts" import { BaseInputorController } from "./base.ts" export const TEXT_INPUTOR_TYPE_NAME = "text" as const export type TextInputorTypeName = typeof TEXT_INPUTOR_TYPE_NAME export type TextInputorExternalValue = string | undefined export type TextInputorInternalValue = string | undefined export interface TextInputorSchema extends BaseInputorSchema { inputorTypeName: TextInputorTypeName minLength?: number | undefined maxLength?: number | undefined } export interface TextInputorContext extends BaseInputorContext< TextInputorExternalValue, TextInputorInternalValue > {} export interface TextInputorControllerOptions extends BaseInputorControllerOptions< TextInputorExternalValue, TextInputorSchema > { minLength?: number | undefined maxLength?: number | undefined } export class TextInputorController extends BaseInputorController< TextInputorExternalValue, TextInputorInternalValue, TextInputorSchema, TextInputorContext > { declare schemaType: TextInputorSchema declare optionsType: TextInputorControllerOptions declare constructorType: typeof TextInputorController static inputorTypeName: TextInputorTypeName = TEXT_INPUTOR_TYPE_NAME inputorTypeName: TextInputorTypeName = TEXT_INPUTOR_TYPE_NAME voidExternalValue = undefined voidInternalValue = undefined private minLength!: number private maxLength!: number constructor(options: TextInputorControllerOptions) { super(options) } protected override async initialize( options: TextInputorControllerOptions, hooks?: InputorControllerInitializeHooks, ): Promise { await super.initialize(options, { ...hooks, onSchemaReady: async () => { this.minLength = options.minLength ?? 0 this.maxLength = options.maxLength ?? Infinity await hooks?.onSchemaReady?.() }, }) } override async getSchema(): Promise { return { ...(await super.getSchema()), minLength: this.minLength, maxLength: this.maxLength, } } protected override async constrainExternalValue( externalValue: TextInputorExternalValue, ): Promise { if (externalValue === undefined) { return await Promise.resolve(undefined) } return await Promise.resolve(externalValue.slice(0, this.maxLength)) } protected override async constrainInternalValue( internalValue: TextInputorInternalValue, ): Promise { if (internalValue === undefined) { return await Promise.resolve(undefined) } return await Promise.resolve(internalValue.slice(0, this.maxLength)) } }