Skip to content

Commit cbb704a

Browse files
committed
IBX-10879: Added possibility to disable/enable drop-down options
1 parent 7d6fc40 commit cbb704a

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

src/bundle/Resources/public/js/scripts/core/dropdown.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@
179179
const stringifiedValue = JSON.stringify(String(value));
180180
const optionToSelect = this.itemsListContainer.querySelector(`.ibexa-dropdown__item[data-value=${stringifiedValue}]`);
181181

182+
if (optionToSelect?.classList.contains('ibexa-dropdown__item--disabled')) {
183+
return;
184+
}
185+
182186
return this.onSelect(optionToSelect, true);
183187
}
184188

@@ -243,6 +247,10 @@
243247
}
244248

245249
onSelect(element, selected) {
250+
if (!element || element.classList.contains('ibexa-dropdown__item--disabled')) {
251+
return;
252+
}
253+
246254
if (this.canSelectOnlyOne && selected) {
247255
this.hideOptions();
248256
this.clearCurrentSelection(false);
@@ -294,6 +302,11 @@
294302

295303
onOptionClick({ target }) {
296304
const option = target.closest('.ibexa-dropdown__item');
305+
306+
if (!option || option.classList.contains('ibexa-dropdown__item--disabled')) {
307+
return;
308+
}
309+
297310
const isSelected = this.canSelectOnlyOne || !option.classList.contains('ibexa-dropdown__item--selected');
298311

299312
return this.onSelect(option, isSelected);
@@ -455,7 +468,7 @@
455468
});
456469

457470
optionsToRecreate.forEach((option) => {
458-
this.createOption(option.value, option.innerHTML);
471+
this.createOption(option.value, option.innerHTML, option.disabled);
459472
});
460473

461474
const selectedItems = this.getSelectedItems();
@@ -479,14 +492,19 @@
479492
optionNode.remove();
480493
}
481494

482-
createOption(value, label) {
495+
createOption(value, label, isDisabled = false) {
483496
const container = doc.createElement('div');
484497
const itemRendered = this.itemTemplate.replaceAll('{{ value }}', escapeHTMLAttribute(value)).replaceAll('{{ label }}', label);
485498

486499
container.insertAdjacentHTML('beforeend', itemRendered);
487500

488501
const optionNode = container.firstElementChild;
489502

503+
if (isDisabled) {
504+
optionNode.classList.add('ibexa-dropdown__item--disabled');
505+
optionNode.setAttribute('aria-disabled', 'true');
506+
}
507+
490508
optionNode.addEventListener('click', this.onOptionClick, false);
491509
this.itemsListContainer.append(optionNode);
492510
}
@@ -578,7 +596,7 @@
578596
this.itemsPopover._element.addEventListener('shown.bs.popover', this.onPopoverShow);
579597
this.itemsPopover._element.addEventListener('hidden.bs.popover', this.onPopoverHide);
580598
this.itemsListContainer
581-
.querySelectorAll('.ibexa-dropdown__item:not([disabled])')
599+
.querySelectorAll('.ibexa-dropdown__item:not(.ibexa-dropdown__item--disabled)')
582600
.forEach((option) => option.addEventListener('click', this.onOptionClick, false));
583601

584602
if (this.itemsFilterInput) {
@@ -626,6 +644,43 @@
626644

627645
selectedItems.forEach((selectedItem) => this.attachSelectedItemEvents(selectedItem));
628646
}
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+
}
629684
}
630685

631686
ibexa.addConfig('core.Dropdown', Dropdown);

src/bundle/Resources/public/scss/_dropdown.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,12 @@
557557
margin: calculateRem(5px) calculateRem(8px);
558558
}
559559
}
560+
561+
.ibexa-dropdown__item--disabled {
562+
opacity: 0.5;
563+
cursor: not-allowed;
564+
}
565+
566+
.ibexa-dropdown__item--disabled * {
567+
pointer-events: none;
568+
}

0 commit comments

Comments
 (0)