import { InputFieldType, inputFieldTypeFromJson, inputFieldTypeToJson, } from './InputFieldType'; import { OptionItem, optionItemFromJson, optionItemToJson } from './OptionItem'; export class InputField { type: InputFieldType; name?: string; value?: string; label?: string; maxLength?: number; isRequired: boolean; optionsList?: OptionItem[]; optionsMap?: { [key: string]: OptionItem[] }; hint?: string; minLength?: number; readOnly: boolean; dependsOn?: string; constructor( type: InputFieldType, isRequired: boolean, readOnly: boolean, name?: string, value?: string, label?: string, maxLength?: number, optionsList?: OptionItem[], optionsMap?: { [key: string]: OptionItem[] }, hint?: string, minLength?: number, dependsOn?: string ) { this.type = type; this.name = name; this.value = value; this.label = label; this.maxLength = maxLength; this.isRequired = isRequired; this.optionsList = optionsList; this.optionsMap = optionsMap; this.hint = hint; this.minLength = minLength; this.readOnly = readOnly; this.dependsOn = dependsOn; } } export function inputFieldFromJson(json: { [key: string]: any }): InputField { return new InputField( inputFieldTypeFromJson(json.type), json.is_required, json.read_only, json.name, json.value, json.label, json.max_length, Array.isArray(json.optionsList) ? json.optionsList.map((item: any) => optionItemFromJson(item)) : undefined, json.options ? Object.entries(json.options).reduce( (acc: { [key: string]: OptionItem[] }, [key, value]) => { acc[key] = Array.isArray(value) ? value.map((item: any) => optionItemFromJson(item)) : []; return acc; }, {} ) : undefined, json.hint, json.min_length, json.depends_on ); } export function inputFieldToJson(inputField: InputField): { [key: string]: any; } { return { type: inputFieldTypeToJson(inputField.type), name: inputField.name, value: inputField.value, label: inputField.label, max_length: inputField.maxLength, is_required: inputField.isRequired, validation_regex: inputField.optionsList ? inputField.optionsList.map((item) => optionItemToJson(item)) : undefined, options: inputField.optionsMap ? Object.entries(inputField.optionsMap).reduce( (acc: any, [key, value]) => { acc[key] = value.map((item) => optionItemToJson(item)); return acc; }, {} ) : undefined, hint: inputField.hint, min_length: inputField.minLength, read_only: inputField.readOnly, depends_on: inputField.dependsOn, }; }