Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select-Only Combobox Example: Add click handler to focus combobox when label is clicked #2889

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ <h2 id="ex_label">Example</h2>
</div>
<div role="separator" id="ex_start_sep" aria-labelledby="ex_start_sep ex_label" aria-label="Start of"></div>
<div id="ex1">
<label id="combo1-label" class="combo-label">Favorite Fruit</label>
<div class="combo js-select">
<div id="combo1-label" class="combo-label">Favorite Fruit</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for changing from label element to div element?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label element doesn't really provide any behavior and it may be confusing to add event handlers to the element.

<div aria-controls="listbox1" aria-expanded="false" aria-haspopup="listbox" aria-labelledby="combo1-label" id="combo1" class="combo-input" role="combobox" tabindex="0"></div>
<div class="combo-menu" role="listbox" id="listbox1" aria-labelledby="combo1-label" tabindex="-1">
<!-- options are inserted here -->
Expand Down
4 changes: 2 additions & 2 deletions content/patterns/combobox/examples/css/select-only.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pointer-events: none;
position: absolute;
right: 16px;
top: 50%;
top: 65%;
transform: translate(0, -65%) rotate(45deg);
width: 12px;
}
Expand Down Expand Up @@ -49,9 +49,9 @@

.combo-label {
display: block;
font-size: 20px;
font-weight: 100;
margin-bottom: 0.25em;
font-size: 1.2em;
}

.combo-menu {
Expand Down
6 changes: 6 additions & 0 deletions content/patterns/combobox/examples/js/select-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function maintainScrollVisibility(activeElement, scrollParent) {
const Select = function (el, options = []) {
// element refs
this.el = el;
this.labelEl = el.querySelector('.combo-label');
this.comboEl = el.querySelector('[role=combobox]');
this.listboxEl = el.querySelector('[role=listbox]');

Expand All @@ -194,6 +195,7 @@ Select.prototype.init = function () {
this.comboEl.innerHTML = this.options[0];

// add event listeners
this.labelEl.addEventListener('click', this.onLabelClick.bind(this));
this.comboEl.addEventListener('blur', this.onComboBlur.bind(this));
this.listboxEl.addEventListener('focusout', this.onComboBlur.bind(this));
this.comboEl.addEventListener('click', this.onComboClick.bind(this));
Expand Down Expand Up @@ -240,6 +242,10 @@ Select.prototype.getSearchString = function (char) {
return this.searchString;
};

Select.prototype.onLabelClick = function () {
this.comboEl.focus();
};

Select.prototype.onComboBlur = function (event) {
// do nothing if relatedTarget is contained within listboxEl
if (this.listboxEl.contains(event.relatedTarget)) {
Expand Down
Loading