import { AbstractComponentBuilder } from './AbstractComponentBuilder'; export class DropdownComponentBuilder extends AbstractComponentBuilder<'Dropdown'> { constructor(id: string) { super(id, 'Dropdown'); this.patch({ options: [], }); } addOption(id: string, displayName: string) { const state = this.state(); if (!state.options) { this.patch({ options: [{ id, displayName }], }); } else { this.patch({ options: [...state.options, { id, displayName }], }); } return this; } override isValid() { const { options } = this.state(); const ids = new Set(options!.map((option) => option.id)); const validation = this.createValidationResponse().withParentValidation( super.isValid(), ); if (options?.length) { validation.withCheck('should be at least 2 options', () => { return ids.size >= 2; }); if (options.length > 2) { validation.withCheck('options ids should be uniq', () => { return ids.size === options!.length; }); } } else { validation.withCheck('options should be defined', () => false); } return validation.validate(); } }