Skip to content

Commit

Permalink
Adding the focus() function to MuchSelect.
Browse files Browse the repository at this point in the history
Add an example that shows how to call `focus()`, which will focus the `<much-select>`.
  • Loading branch information
Jachin Rupe committed Dec 11, 2023
1 parent a28a7d2 commit 7da199d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 23 deletions.
21 changes: 13 additions & 8 deletions dist/much-select-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const buildOptionsFromSelectElement = (selectElement) => {
value = optionElement.innerText;
}
const option = { value };
if (optionElement.hasAttribute("id")) {
if (optionElement.hasAttribute("data-part")) {
option.part = optionElement.getAttribute("data-part").trim();
} else if (optionElement.hasAttribute("id")) {
option.part = optionElement.getAttribute("id").trim();
} else {
option.part = dasherize(option.value.trim());
Expand Down Expand Up @@ -610,13 +612,7 @@ class MuchSelect extends HTMLElement {
app.ports.focusInput.subscribe(() => {
// This port is here because we need to be able to call the focus method
// which is something we can not do from inside Elm.
window.requestAnimationFrame(() => {
const inputFilterElement =
this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
this.focus();
}),
);

Expand Down Expand Up @@ -1998,6 +1994,15 @@ class MuchSelect extends HTMLElement {
}
});
}

focus() {
window.requestAnimationFrame(() => {
const inputFilterElement = this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
}
}

// noinspection JSUnusedGlobalSymbols
Expand Down
21 changes: 13 additions & 8 deletions dist/much-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const buildOptionsFromSelectElement = (selectElement) => {
value = optionElement.innerText;
}
const option = { value };
if (optionElement.hasAttribute("id")) {
if (optionElement.hasAttribute("data-part")) {
option.part = optionElement.getAttribute("data-part").trim();
} else if (optionElement.hasAttribute("id")) {
option.part = optionElement.getAttribute("id").trim();
} else {
option.part = dasherize(option.value.trim());
Expand Down Expand Up @@ -610,13 +612,7 @@ class MuchSelect extends HTMLElement {
app.ports.focusInput.subscribe(() => {
// This port is here because we need to be able to call the focus method
// which is something we can not do from inside Elm.
window.requestAnimationFrame(() => {
const inputFilterElement =
this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
this.focus();
}),
);

Expand Down Expand Up @@ -1998,6 +1994,15 @@ class MuchSelect extends HTMLElement {
}
});
}

focus() {
window.requestAnimationFrame(() => {
const inputFilterElement = this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
}
}

// noinspection JSUnusedGlobalSymbols
Expand Down
23 changes: 23 additions & 0 deletions examples/focus-on-demand.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div id="focus-on-demand">
<h3>Single Select - <code>blurOrUnfocusedValueChanged</code> Event</h3>
<much-select>
<select slot="select-input">
<option value="111">Grass Pea</option>
<option value="222">Velvet Bean</option>
<option value="333">Sword Bean</option>
<option value="444">Soybean</option>
</select>
</much-select>
<br>
<button>Focus 👀</button>

<script>
window.addEventListener("load", () => {
document
.querySelector("#focus-on-demand button")
.addEventListener("click", () => {
document.querySelector("#focus-on-demand much-select").focus();
});
});
</script>
</div>
4 changes: 4 additions & 0 deletions site/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ <h3>Demo and Development Playground</h3>
<div class="example">
<div id="single-select-blur-or-unfocused-value-change"></div>
</div>

<div class="example">
<div id="focus-on-demand"></div>
</div>
</div>

<footer></footer>
Expand Down
5 changes: 5 additions & 0 deletions soupault.toml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ widget = "include"
file = "examples/events-only-mode.html"
selector = ".example #events-only-mode"

[widgets.focus-on-dedmand]
widget = "include"
file = "examples/focus-on-demand.html"
selector = ".example #focus-on-demand"

[widgets.hidden-input-json-encoding-slot]
widget = "include"
file = "examples/hidden-input-json-encoding-slot.html"
Expand Down
17 changes: 10 additions & 7 deletions src/much-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,7 @@ class MuchSelect extends HTMLElement {
app.ports.focusInput.subscribe(() => {
// This port is here because we need to be able to call the focus method
// which is something we can not do from inside Elm.
window.requestAnimationFrame(() => {
const inputFilterElement =
this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
this.focus();
}),
);

Expand Down Expand Up @@ -1998,6 +1992,15 @@ class MuchSelect extends HTMLElement {
}
});
}

focus() {
window.requestAnimationFrame(() => {
const inputFilterElement = this.shadowRoot.getElementById("input-filter");
if (inputFilterElement) {
this.shadowRoot.getElementById("input-filter").focus();
}
});
}
}

// noinspection JSUnusedGlobalSymbols
Expand Down

0 comments on commit 7da199d

Please sign in to comment.