import type { IFieldBase } from './field.types'; import { FieldBase } from './field.types'; import type { ChoiceModel } from './choice.types'; import type { TextModel } from './text.types'; export interface SelectStateScaffold { waiting: any; search: any; loaded: any; failed: any; } export interface SelectStates extends SelectStateScaffold { waiting: {}; search: {}; loaded: {}; failed: {}; } // EVENTS that the machine handles export type Select_Event_Change = { type: 'CHANGE'; value: TextModel }; export type Select_Event_Load = { type: 'LOAD'; data: any }; export type Select_Event_ChoiceSelected = { type: 'SELECT_CHOICE', selected: ChoiceModel }; export type SelectEvent = | Select_Event_Change | Select_Event_Load | Select_Event_ChoiceSelected; export interface SelectEventsScaffold { 'CHANGE': Function, 'LOAD': Function, 'SELECT_CHOICE': Function } export type SelectContext = { field: SelectModel; textMachine: any; choiceMachine: any; results: string[]; selected: ChoiceModel; }; export type SelectContextUndefined = { field: undefined; textMachine: undefined; choiceMachine: undefined; results: undefined; selected: undefined; }; export interface SelectStateSchema { initial: string; context: SelectContext; states: SelectStates }; export type SelectState = { value: 'waiting'; context: SelectContext; } | { value: 'search'; context: SelectContext; } | { value: 'loaded'; context: SelectContext; } | { value: 'failed'; context: SelectContext; }; export interface ISelect extends IFieldBase { // , IChoiceModel { autocomplete?: boolean, // optional optionIdentifier: string; endpoint: string; } export class SelectModel extends FieldBase implements ISelect { type = "select"; subType = "radio" // options = [] as ChoiceOption[]; autocomplete = true; optionIdentifier = 'title'; endpoint = ''; constructor(select: Partial) { super(select); this.autocomplete = select.autocomplete ?? this.autocomplete; this.optionIdentifier = select.optionIdentifier ?? this.optionIdentifier; this.endpoint = select.endpoint ?? this.endpoint; } }