|
179 | 179 | const stringifiedValue = JSON.stringify(String(value)); |
180 | 180 | const optionToSelect = this.itemsListContainer.querySelector(`.ibexa-dropdown__item[data-value=${stringifiedValue}]`); |
181 | 181 |
|
| 182 | + if (optionToSelect?.classList.contains('ibexa-dropdown__item--disabled')) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
182 | 186 | return this.onSelect(optionToSelect, true); |
183 | 187 | } |
184 | 188 |
|
|
243 | 247 | } |
244 | 248 |
|
245 | 249 | onSelect(element, selected) { |
| 250 | + if (!element || element.classList.contains('ibexa-dropdown__item--disabled')) { |
| 251 | + return; |
| 252 | + } |
| 253 | + |
246 | 254 | if (this.canSelectOnlyOne && selected) { |
247 | 255 | this.hideOptions(); |
248 | 256 | this.clearCurrentSelection(false); |
|
294 | 302 |
|
295 | 303 | onOptionClick({ target }) { |
296 | 304 | const option = target.closest('.ibexa-dropdown__item'); |
| 305 | + |
| 306 | + if (!option || option.classList.contains('ibexa-dropdown__item--disabled')) { |
| 307 | + return; |
| 308 | + } |
| 309 | + |
297 | 310 | const isSelected = this.canSelectOnlyOne || !option.classList.contains('ibexa-dropdown__item--selected'); |
298 | 311 |
|
299 | 312 | return this.onSelect(option, isSelected); |
|
455 | 468 | }); |
456 | 469 |
|
457 | 470 | optionsToRecreate.forEach((option) => { |
458 | | - this.createOption(option.value, option.innerHTML); |
| 471 | + this.createOption(option.value, option.innerHTML, option.disabled); |
459 | 472 | }); |
460 | 473 |
|
461 | 474 | const selectedItems = this.getSelectedItems(); |
|
479 | 492 | optionNode.remove(); |
480 | 493 | } |
481 | 494 |
|
482 | | - createOption(value, label) { |
| 495 | + createOption(value, label, isDisabled = false) { |
483 | 496 | const container = doc.createElement('div'); |
484 | 497 | const itemRendered = this.itemTemplate.replaceAll('{{ value }}', escapeHTMLAttribute(value)).replaceAll('{{ label }}', label); |
485 | 498 |
|
486 | 499 | container.insertAdjacentHTML('beforeend', itemRendered); |
487 | 500 |
|
488 | 501 | const optionNode = container.firstElementChild; |
489 | 502 |
|
| 503 | + if (isDisabled) { |
| 504 | + optionNode.classList.add('ibexa-dropdown__item--disabled'); |
| 505 | + optionNode.setAttribute('aria-disabled', 'true'); |
| 506 | + } |
| 507 | + |
490 | 508 | optionNode.addEventListener('click', this.onOptionClick, false); |
491 | 509 | this.itemsListContainer.append(optionNode); |
492 | 510 | } |
|
578 | 596 | this.itemsPopover._element.addEventListener('shown.bs.popover', this.onPopoverShow); |
579 | 597 | this.itemsPopover._element.addEventListener('hidden.bs.popover', this.onPopoverHide); |
580 | 598 | this.itemsListContainer |
581 | | - .querySelectorAll('.ibexa-dropdown__item:not([disabled])') |
| 599 | + .querySelectorAll('.ibexa-dropdown__item:not(.ibexa-dropdown__item--disabled)') |
582 | 600 | .forEach((option) => option.addEventListener('click', this.onOptionClick, false)); |
583 | 601 |
|
584 | 602 | if (this.itemsFilterInput) { |
|
626 | 644 |
|
627 | 645 | selectedItems.forEach((selectedItem) => this.attachSelectedItemEvents(selectedItem)); |
628 | 646 | } |
| 647 | + |
| 648 | + disableOption(value) { |
| 649 | + const stringifiedValue = JSON.stringify(String(value)); |
| 650 | + const item = this.itemsListContainer.querySelector(`[data-value=${stringifiedValue}]`); |
| 651 | + const option = this.sourceInput.querySelector(`[value=${stringifiedValue}]`); |
| 652 | + |
| 653 | + if (item) { |
| 654 | + item.classList.add('ibexa-dropdown__item--disabled'); |
| 655 | + item.setAttribute('aria-disabled', 'true'); |
| 656 | + } |
| 657 | + |
| 658 | + if (option) { |
| 659 | + option.disabled = true; |
| 660 | + } |
| 661 | + } |
| 662 | + |
| 663 | + enableOption(value) { |
| 664 | + const stringifiedValue = JSON.stringify(String(value)); |
| 665 | + const item = this.itemsListContainer.querySelector(`[data-value=${stringifiedValue}]`); |
| 666 | + const option = this.sourceInput.querySelector(`[value=${stringifiedValue}]`); |
| 667 | + |
| 668 | + if (item) { |
| 669 | + item.classList.remove('ibexa-dropdown__item--disabled'); |
| 670 | + item.removeAttribute('aria-disabled'); |
| 671 | + } |
| 672 | + |
| 673 | + if (option) { |
| 674 | + option.disabled = false; |
| 675 | + } |
| 676 | + } |
| 677 | + |
| 678 | + isOptionDisabled(value) { |
| 679 | + const stringifiedValue = JSON.stringify(String(value)); |
| 680 | + const option = this.sourceInput.querySelector(`[value=${stringifiedValue}]`); |
| 681 | + |
| 682 | + return option.disabled; |
| 683 | + } |
629 | 684 | } |
630 | 685 |
|
631 | 686 | ibexa.addConfig('core.Dropdown', Dropdown); |
|
0 commit comments