Skip to content

Commit

Permalink
fix(value): remove usage of innerHtml and use createNode instead #169
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-capsule42 authored and blackfalcon committed Jan 26, 2024
1 parent 38f02a8 commit ddeedf8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
46 changes: 33 additions & 13 deletions src/auro-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ export class AuroSelect extends LitElement {
});
}

/**
* Adds the value string to the trigger.
* @param {string} str - The string to display in the trigger.
* @private
* @returns {void}
*/
updateDisplayedValue(str) {
const dropdown = this.shadowRoot.querySelector('auro-dropdown');
const triggerContentEl = dropdown.querySelector('#triggerFocus');

// remove all existing rendered value(s)
triggerContentEl.querySelectorAll('[valuestr]').forEach((elm) => {
elm.remove();
});

// create a new element with the new value
const valueElem = document.createElement('span');
valueElem.setAttribute('valuestr', true);
valueElem.appendChild(document.createTextNode(str));

// append the new element into the trigger content
triggerContentEl.appendChild(valueElem);
}

/**
* Binds all behavior needed to the menu after rendering.
* @private
Expand All @@ -236,12 +260,10 @@ export class AuroSelect extends LitElement {
});

this.menu.addEventListener('selectedOption', () => {
const dropdown = this.shadowRoot.querySelector('auro-dropdown');
const triggerContentEl = dropdown.querySelector('#triggerFocus');

this.optionSelected = this.menu.optionSelected;
this.value = this.optionSelected.value;
triggerContentEl.innerHTML = this.optionSelected.innerHTML;

this.updateDisplayedValue(this.optionSelected.innerHTML);

if (this.dropdown.isPopoverVisible) {
this.dropdown.hide();
Expand All @@ -259,20 +281,18 @@ export class AuroSelect extends LitElement {
this.menu.optionSelected = undefined;
this.optionSelected = this.menu.optionSelected;

const dropdown = this.shadowRoot.querySelector('auro-dropdown');
const triggerContentEl = dropdown.querySelector('#triggerFocus');

this.validity = 'badInput';

// Capitilizes the first letter of each word in this.value string
triggerContentEl.innerHTML = this.value.replace(/(^\w{1})|(\s+\w{1})/gu, (letter) => letter.toUpperCase());
const valueStr = this.value.replace(/(^\w{1})|(\s+\w{1})/gu, (letter) => letter.toUpperCase());

// Pass the new string to the trigger content
this.updateDisplayedValue(valueStr);
});

this.menu.addEventListener('auroMenu-selectValueReset', () => {
const dropdown = this.shadowRoot.querySelector('auro-dropdown');
const triggerContentEl = dropdown.querySelector('#triggerFocus');

triggerContentEl.innerHTML = this.placeholder;
// set the trigger content back to the placeholder
this.updateDisplayedValue(this.placeholder);

this.optionSelected = undefined;
this.value = undefined;
Expand Down Expand Up @@ -480,7 +500,7 @@ export class AuroSelect extends LitElement {
}

/**
* Handles reading of auro-select by screenreaders.
* Handles reading of auro-select by screen readers.
* @private
* @returns {void}
*/
Expand Down
2 changes: 1 addition & 1 deletion test/auro-select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('auro-select', () => {
const triggerContentHTML = dropdown.querySelector('#triggerFocus').innerHTML;

await expect(el.optionSelected).to.be.equal(undefined);
await expect(triggerContentHTML).to.be.equal('Flight Course');
await expect(triggerContentHTML).to.contain('Flight Course');
await expect(el.getAttribute('validity')).to.equal('badInput');
});

Expand Down

0 comments on commit ddeedf8

Please sign in to comment.