Skip to content

Commit

Permalink
fix: (multi-select) add idPrefix option to allow multiple multi-selec…
Browse files Browse the repository at this point in the history
…ts on the same page (#930)
  • Loading branch information
starswan authored Dec 10, 2024
1 parent fa086c2 commit 457265f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/examples/multi-select/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Multi select (example)
figma_link: https://www.figma.com/file/N2xqOFkyehXwcD9DxU1gEq/MoJ-Figma-Kit?type=design&node-id=841-220&mode=design
---

<table class="govuk-table" data-module="moj-multi-select" data-multi-select-checkbox="#select-all">
<table class="govuk-table" data-module="moj-multi-select" data-multi-select-checkbox="#select-all" data-multi-select-idprefix="mountains-">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="col" id="select-all"></th>
Expand Down
3 changes: 2 additions & 1 deletion src/moj/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ MOJFrontend.initAll = function (options) {
MOJFrontend.nodeListForEach($multiSelects, function ($multiSelect) {
new MOJFrontend.MultiSelect({
container: $multiSelect.querySelector($multiSelect.getAttribute('data-multi-select-checkbox')),
checkboxes: $multiSelect.querySelectorAll('tbody .govuk-checkboxes__input')
checkboxes: $multiSelect.querySelectorAll('tbody .govuk-checkboxes__input'),
id_prefix: $multiSelect.getAttribute('data-multi-select-idprefix'),
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/moj/components/multi-select/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Table multi-select

- [Guidance](https://design-patterns.service.justice.gov.uk/components/table-multi-select)
- [Guidance](https://design-patterns.service.justice.gov.uk/components/multi-select/)
16 changes: 11 additions & 5 deletions src/moj/components/multi-select/multi-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ MOJFrontend.MultiSelect = function(options) {

this.container.data('moj-multi-select-initialised', true);

this.toggle = $(this.getToggleHtml());
const idPrefix = options.id_prefix;
let allId = 'checkboxes-all';
if (typeof idPrefix !== 'undefined') {
allId = idPrefix + 'checkboxes-all';
}

this.toggle = $(this.getToggleHtml(allId));
this.toggleButton = this.toggle.find('input');
this.toggleButton.on('click', $.proxy(this, 'onButtonClick'));
this.container.append(this.toggle);
Expand All @@ -16,11 +22,11 @@ MOJFrontend.MultiSelect = function(options) {
this.checked = options.checked || false;
};

MOJFrontend.MultiSelect.prototype.getToggleHtml = function() {
var html = '';
MOJFrontend.MultiSelect.prototype.getToggleHtml = function (allId) {
let html = '';
html += '<div class="govuk-checkboxes__item govuk-checkboxes--small moj-multi-select__checkbox">';
html += ' <input type="checkbox" class="govuk-checkboxes__input" id="checkboxes-all">';
html += ' <label class="govuk-label govuk-checkboxes__label moj-multi-select__toggle-label" for="checkboxes-all">';
html += ` <input type="checkbox" class="govuk-checkboxes__input" id="${allId}">`;
html += ` <label class="govuk-label govuk-checkboxes__label moj-multi-select__toggle-label" for="${allId}">`;
html += ' <span class="govuk-visually-hidden">Select all</span>';
html += ' </label>';
html += '</div>';
Expand Down

0 comments on commit 457265f

Please sign in to comment.