Returns a set of violations
()
| 208 | |
| 209 | /** Returns a set of violations */ |
| 210 | validate(): string[] { |
| 211 | const violations: string[] = []; |
| 212 | |
| 213 | if (!this.inputs.multi() && this.inputs.value().length > 1) { |
| 214 | violations.push( |
| 215 | `A single-select listbox should not have multiple selected options. Selected options: ${this.inputs.value().join(', ')}`, |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | const values = this.inputs.items().map(o => o.value()); |
| 220 | const duplicates = values.filter((val, idx) => values.indexOf(val) !== idx); |
| 221 | if (duplicates.length > 0) { |
| 222 | violations.push(`Duplicate option value '${duplicates[0]}' detected inside ngListbox.`); |
| 223 | } |
| 224 | |
| 225 | const ids = this.inputs.items().map(o => o.id()); |
| 226 | const duplicateIds = ids.filter((id, idx) => ids.indexOf(id) !== idx); |
| 227 | if (duplicateIds.length > 0) { |
| 228 | violations.push(`Duplicate option ID '${duplicateIds[0]}' detected inside ngListbox.`); |
| 229 | } |
| 230 | |
| 231 | return violations; |
| 232 | } |
| 233 | |
| 234 | /** Handles keydown events for the listbox. */ |
| 235 | onKeydown(event: KeyboardEvent) { |