-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* SelectAll | ||
* | ||
* A checkbox that is used to automatically select and deselect a collection of other checkboxes. | ||
* | ||
* * data-select-all-class defines the class to identify the 'select all' checkbox | ||
* * data-collection-class defines the class to identify the checkboxes in the collection | ||
* | ||
* <div data-module="govuk-select-all" data-select-all-class="select-all" data-collection-class="select-all-box" > | ||
* <input class="select-all" type="checkbox" /> | ||
* </div> | ||
* | ||
* <input class="select-all-box" type="checkbox" id="option_1" /> | ||
* <input class="select-all-box" type="checkbox" id="option_2" /> | ||
* <input class="select-all-box" type="checkbox" id="option_3" /> | ||
* <input class="select-all-box" type="checkbox" id="option_4" /> | ||
* <input type="checkbox" id="option_5 /> <!-- not part of the collection" --> | ||
*/ | ||
export class SelectAll { | ||
/** | ||
* @param {Element} $module - HTML element to use for component | ||
*/ | ||
constructor ($module) { | ||
if (($module instanceof window.HTMLElement) && document.body.classList.contains('govuk-frontend-supported')) { | ||
this.$module = $module | ||
} | ||
} | ||
|
||
/** | ||
* Initialise component | ||
*/ | ||
init () { | ||
// Check that required elements are present | ||
if (!this.$module) { return } | ||
|
||
const selector = this.$module.dataset.selectAllClass | ||
this.collection = this.$module.dataset.collectionClass | ||
|
||
this.selectAllBox = this.$module.querySelector(`.${selector}`) | ||
this.selectAllBox.addEventListener('change', () => this.toggleSelection()) | ||
} | ||
|
||
toggleSelection = () => { | ||
const checkBoxes = document.querySelectorAll(`.${this.collection}`) | ||
checkBoxes.forEach((box) => { box.checked = this.selectAllBox.checked }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { SelectAll } from './selectAll.mjs' | ||
|
||
describe('SelectAll', () => { | ||
describe('toggleSelection', () => { | ||
let selectAllBox = null | ||
let selectedBox = null | ||
let unselectedBox = null | ||
let testArea = null | ||
|
||
beforeEach(() => { | ||
document.body.classList.add('govuk-frontend-supported') | ||
|
||
testArea = document.createElement('div') | ||
document.body.appendChild(testArea) | ||
|
||
// <div data-module="govuk-select-all" data-select-all-class="selector-box" data collection-class="pick-me"> | ||
// <input class="selector-box" type="checkbox" /> | ||
// </div> | ||
const selectAllDiv = document.createElement('div') | ||
selectAllDiv.setAttribute('data-module', 'govuk-select-all') | ||
selectAllDiv.setAttribute('data-select-all-class', 'selector-box') | ||
selectAllDiv.setAttribute('data-collection-class', 'pick-me') | ||
selectAllBox = document.createElement('input') | ||
selectAllBox.type = 'checkbox' | ||
selectAllBox.classList.add('selector-box') | ||
selectAllBox.checked = false | ||
selectAllDiv.appendChild(selectAllBox) | ||
testArea.appendChild(selectAllDiv) | ||
|
||
// <input class="pick-me" type="checkbox" name="box1" checked=true> | ||
selectedBox = createCheckbox('pick-me', true, 'box1') | ||
testArea.appendChild(selectedBox) | ||
|
||
// <input class="pick-me" type="checkbox" name="box2"> | ||
unselectedBox = createCheckbox('pick-me', true, 'box1') | ||
testArea.appendChild(unselectedBox) | ||
|
||
const selectAll = new SelectAll(selectAllDiv) | ||
selectAll.init() | ||
}) | ||
|
||
afterEach(() => { | ||
document.body.classList.remove('govuk-frontend-supported') | ||
testArea.remove() | ||
}) | ||
|
||
const createCheckbox = (className, checked = false, name = '') => { | ||
const checkbox = document.createElement('input') | ||
checkbox.type = 'checkbox' | ||
checkbox.classList.add(className) | ||
checkbox.name = name | ||
checkbox.checked = checked | ||
return checkbox | ||
} | ||
|
||
const toggleSelectAll = () => { | ||
const event = new Event('change') | ||
selectAllBox.checked = !selectAllBox.checked | ||
selectAllBox.dispatchEvent(event) | ||
} | ||
|
||
it('should mark all checkboxes as checked', () => { | ||
toggleSelectAll() | ||
|
||
expect(selectedBox.checked).toBeTrue() | ||
expect(unselectedBox.checked).toBeTrue() | ||
}) | ||
|
||
it('should mark all checkboxes as unchecked', () => { | ||
toggleSelectAll() | ||
toggleSelectAll() | ||
|
||
expect(selectedBox.checked).toBeFalse() | ||
expect(unselectedBox.checked).toBeFalse() | ||
}) | ||
|
||
it('should select newly created boxes', () => { | ||
const newBox = createCheckbox('pick-me', false, 'new-box') | ||
document.body.appendChild(newBox) | ||
|
||
toggleSelectAll() | ||
|
||
expect(newBox.checked).toBeTrue() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters