Skip to content

Commit

Permalink
Merge pull request #25 from usame-algan/master
Browse files Browse the repository at this point in the history
Adds option for event bubbling
  • Loading branch information
zoltantothcom committed Apr 3, 2020
2 parents 6a9c193 + afc8dd9 commit 39220b6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ JavaScript disabled? No problem! Nicely degrades to original `<select>`.
| Option | Required | Description |
| ------ | -------- | ------------------------------------------------------------------ |
| elem | yes | _id_ of the select you want to replace **or** a direct DOM element |
| bubbles | no | Boolean indicating whether the change event of the select bubbles |

## Methods

Expand Down
2 changes: 1 addition & 1 deletion dist/vanilla-js-dropdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/javascript/vanilla-js-dropdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/javascript/vanilla-js-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @param {(string|Object)} options.elem - HTML id of the select or the DOM element.
*/
var CustomSelect = function(options) {
var elem =
typeof options.elem === 'string' ? document.getElementById(options.elem) : options.elem,
var elem = typeof options.elem === 'string' ? document.getElementById(options.elem) : options.elem;
var bubbles = typeof options.bubbles === 'boolean' ? true : false,
mainClass = 'js-Dropdown',
titleClass = 'js-Dropdown-title',
listClass = 'js-Dropdown-list',
Expand Down Expand Up @@ -117,7 +117,7 @@ var CustomSelect = function(options) {
elem.options.selectedIndex = t.getAttribute('data-index');

//trigger 'change' event
var evt = new CustomEvent('change');
var evt = bubbles ? new CustomEvent('change', {bubbles: true}) : new CustomEvent('change');
elem.dispatchEvent(evt);

// highlight the selected
Expand Down
26 changes: 26 additions & 0 deletions test/spec/dropdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ describe('SELECT - contains OPTGROUP', function() {
sharedTests();
});

describe('SELECT - supports event bubbling', function() {
beforeEach(function() {
jasmine.getFixtures().fixturesPath = fixturePath;
loadFixtures(selectFixtureWithId);

this.original = document.getElementById('select');

this.select = new CustomSelect({
elem: this.original,
bubbles: true,
});
});

it("should create an event with the bubbles attribute if the option is passed", function() {
this.original.addEventListener('change', function(event) {
expect(event.bubbles).toBe(true);
});

var spyEvent = spyOnEvent('.js-Dropdown-list li:eq(3)', 'click');
$('.js-Dropdown-list li:eq(3)').click();

expect('click').toHaveBeenTriggeredOn('.js-Dropdown-list li:eq(3)');
expect(spyEvent).toHaveBeenTriggered();
});
});

function sharedTests() {
describe('original select', function() {
it('markup should be present', function() {
Expand Down

0 comments on commit 39220b6

Please sign in to comment.