Cleaned up chip list further and simplified it.

This commit is contained in:
2025-09-24 17:24:22 -07:00
parent 449a5eb896
commit 061bd5a3b6

View File

@@ -1,6 +1,6 @@
import { IgniteHtml } from '../ignite-html/ignite-html.js';
import { IgniteElement } from "../ignite-html/ignite-element.js";
import { IgniteTemplate, list, div, input, button, h4, span } from "../ignite-html/ignite-template.js";
import { IgniteTemplate, list, div, input, button, h4, span, i, converter, text } from "../ignite-html/ignite-template.js";
import { Chip } from "./chip.js";
import { Popper } from "./popper.js";
import { LinearProgress } from "./linear-progress.js";
@@ -47,38 +47,35 @@ class ChipList extends IgniteElement {
items: [],
itemsMax: Number.MAX_SAFE_INTEGER,
placeholder: null,
stopEditingOnBlur: true,
editing: false,
input: null,
searchBox: null,
search: true,
searching: false,
showSearchResults: true,
searchLoading: false,
searchResults: null,
searchPlaceholder: "No results found.",
searchFooter: null,
searchResultConverter: (result) => new text(result),
onSearch: null,
onSearchDelay: 200,
onSearchCallback: null,
blurTimeout: null,
documentListener: null,
freeForm: true,
chipBackground: null,
chipColor: null,
changed: false,
border: false,
readOnly: false,
showClearButton: true
};
}
render() {
return this.template
.class("form-control form-control-lg position-relative d-flex flex-row flex-wrap gap-2 align-items-center")
.class("form-control form-control-lg position-relative d-flex flex-row gap-2 align-items-center justify-content-between")
.class(this.editing, value => value ? "editing" : null)
.attribute("tabindex", "0")
.onFocus(e => this.onFocus())
.child(
new div().class("d-flex flex-row flex-wrap gap-2 align-items-center").child(
//Placeholder
new span().class("text-muted").child(this.placeholder).hide([this.editing, this.items], (editing, items) => editing || (items != null && items.length > 0)),
@@ -99,11 +96,11 @@ class ChipList extends IgniteElement {
//Text input
new div()
.class("input-container")
.show(this.editing)
.child(
new div()
.class("input")
.attribute("contenteditable", "true")
.hide(this.editing, value => { return !value; })
.ref(this.input)
.onEnter((e) => {
e.preventDefault();
@@ -122,6 +119,7 @@ class ChipList extends IgniteElement {
//Add a new item to the chip list.
this.items.push({ content: this.input.textContent.trim() });
this.input.innerHTML = "";
this.input.focus(); //Refocus to ensure we still have it.
this.searching = false; //Reset searching since we just added a item.
this.changed = true; //Make sure changed flag was set.
}
@@ -162,8 +160,6 @@ class ChipList extends IgniteElement {
//will focus the next avaiable element.
if (e.key == "Tab") {
this.searching = false;
if (this.stopEditingOnBlur) {
this.editing = false;
//Fire a change event if there was a change.
@@ -171,7 +167,6 @@ class ChipList extends IgniteElement {
this.changed = false;
this.dispatchEvent(new Event("change"));
}
}
this.input.innerHTML = "";
return;
@@ -194,25 +189,49 @@ class ChipList extends IgniteElement {
this.onSearchCallback = setTimeout(() => this.onSearch(this, this.input.textContent.trim()), this.onSearchDelay);
}
})
)
),
//Clear button
new button()
.class("btn btn-link text-muted fs-4")
.title("Clear")
.show([this.showClearButton, this.items], (show, items) => show && items && items.length > 0)
.child(new i().class('fa fa-times'))
.onClick(() => {
this.items = null;
this.editing = false;
this.searching = false;
this.input.blur();
this.input.innerHTML = "";
}),
//Search results popper
new Popper()
.property("show", this.searching)
.child(
new div()
.class("d-flex flex-column justify-content-center p-2 shadow bg-white")
.style("border-radius", "0.4em")
.class("d-flex flex-column justify-content-center p-2 shadow bg-white border rounded-3")
.child(
new LinearProgress().class("my-2").property("loading", this.searchLoading),
new span(this.searchPlaceholder).class("mt-2").hide([this.searchResults, this.showSearchResults, this.searchLoading], (searchResults, showSearchResults, searchLoading) => {
//Dont show the placeholder if we have search results, or if we are not showing search results.
return (searchResults != null && searchResults.length > 0 && !searchLoading) || (!showSearchResults || searchLoading);
}),
new list(this.searchResults, item => {
return new div(item.content).class("search-result p-2").onClick(() => this.searchResultClick(item));
}).hide([this.showSearchResults, this.searchLoading], (showSearchResults, searchLoading) => !showSearchResults || searchLoading),
this.searchFooter
//Progress bar
new LinearProgress()
.class("my-2")
.property("loading", this.searchLoading)
.property("color", "var(--bs-primary)"),
//Showing results
new span().class("text-muted").hide(this.searchLoading).innerText(this.searchResults, results => results && results.length > 0 ? `Showing ${results.length} ${results.length == 1 ? "result" : "results"}` : "No results"),
//Results
new div().class("d-flex flex-column gap-2").hide([this.showSearchResults, this.searchLoading], (showSearchResults, searchLoading) => !showSearchResults || searchLoading).child(
new list(
this.searchResults,
result => new div()
.class("search-result")
.onClick(() => this.searchResultClick(result))
.child(new converter(result, this.searchResultConverter))
)
)
)
),
)
@@ -245,16 +264,15 @@ class ChipList extends IgniteElement {
//Only blur if we are editing and the target is not ourself or any of our children.
if (this.editing) {
if (e.target != this && !this.contains(e.target)) {
if (this.stopEditingOnBlur) {
this.editing = false;
this.searching = false;
//Fire a change event if there was a change.
if (this.changed) {
this.changed = false;
this.dispatchEvent(new Event("change"));
}
}
this.searching = false;
this.input.blur();
this.input.innerHTML = "";
}