import { generateSchemaFromJoi } from '../schema-generator'; import { BaseComponent, IComponentSchema } from './base-component'; interface IMinMaxListRules { minNumber?: number; maxNumber?: number; } export class ListComponent extends BaseComponent { public override schema() { // no key allowed for list return super.schema().map(({ key, ...rest }) => rest); } protected override getComponentCustomProperties() { const rules = this.getMinMaxFromRules(); const props = (rules.maxNumber ?? 0) > 1 ? { addButtonLabel: 'Add', } : {}; return { components: this.getListComponents(), outputPathList: this.name, props, ...rules, }; } private getListComponents(): IComponentSchema[] { return ( this.properties.items?.flatMap((item: any) => { // only supports 'object' in array if (item.type === 'object') { return generateSchemaFromJoi({ joiDescribe: item, parent: { key: this.name, properties: this.properties }, }); } return []; }) ?? [] ); } protected override getOutputPath(key: string) { // not valid field for this component return {}; } private getMinMaxFromRules(): IMinMaxListRules { return ( this.properties.rules?.reduce((result: any, rule: any) => { const argValue = rule.args?.limit ?? rule.arg; if (rule.name === 'min') { result.minNumber = argValue; } else if (rule.name === 'max') { result.maxNumber = argValue; } return result; }, {}) ?? {} ); } }