adwaveui
Version:
Interactive Web Components inspired by the Gtk Adwaita theme.
645 lines (643 loc) • 31.4 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/components/input/input.tsx
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "jsxte/jsx-runtime";
import { Input, Suggestions } from "adwavecss";
import { Attribute, CustomElement, ElementLifecycleEvent, State } from "jsxte-wc";
import { BaseElement } from "../../base-elements.mjs";
import "../../index.style.mjs";
import { cls } from "../../utils/cls.mjs";
import { CustomKeyboardEvent, CustomMouseEvent } from "../../utils/events.mjs";
import { forceClassName } from "../../utils/force-class-name.mjs";
import { fuzzyCmp } from "../../utils/fuzzy-search.mjs";
import { getUid } from "../../utils/get-uid.mjs";
import { preventDefault, stopEvent } from "../../utils/prevent-default.mjs";
import "./input.style.mjs";
var __esDecorate = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) {
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
return f;
}
__name(accept, "accept");
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function(f) {
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
extraInitializers.push(accept(f || null));
};
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
} else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __runInitializers = function(thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var InputChangeEvent = class extends CustomEvent {
static {
__name(this, "InputChangeEvent");
}
constructor(type, value) {
super("change", {
detail: {
value,
type
}
});
}
};
var ADWaveInputElement = (() => {
let _classDecorators = [CustomElement("adw-input")];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = BaseElement;
let _value_decorators;
let _value_initializers = [];
let _value_extraInitializers = [];
let _disabled_decorators;
let _disabled_initializers = [];
let _disabled_extraInitializers = [];
let _name_decorators;
let _name_initializers = [];
let _name_extraInitializers = [];
let _form_decorators;
let _form_initializers = [];
let _form_extraInitializers = [];
let _type_decorators;
let _type_initializers = [];
let _type_extraInitializers = [];
let _placeholder_decorators;
let _placeholder_initializers = [];
let _placeholder_extraInitializers = [];
let _minLength_decorators;
let _minLength_initializers = [];
let _minLength_extraInitializers = [];
let _maxLength_decorators;
let _maxLength_initializers = [];
let _maxLength_extraInitializers = [];
let _errorLabel_decorators;
let _errorLabel_initializers = [];
let _errorLabel_extraInitializers = [];
let _alertLabel_decorators;
let _alertLabel_initializers = [];
let _alertLabel_extraInitializers = [];
let _suggestions_decorators;
let _suggestions_initializers = [];
let _suggestions_extraInitializers = [];
let _suggestionsShowAll_decorators;
let _suggestionsShowAll_initializers = [];
let _suggestionsShowAll_extraInitializers = [];
let _suggestionsOrientation_decorators;
let _suggestionsOrientation_initializers = [];
let _suggestionsOrientation_extraInitializers = [];
let _fuzzy_decorators;
let _fuzzy_initializers = [];
let _fuzzy_extraInitializers = [];
let _availableOptions_decorators;
let _availableOptions_initializers = [];
let _availableOptions_extraInitializers = [];
let _selectedOption_decorators;
let _selectedOption_initializers = [];
let _selectedOption_extraInitializers = [];
let _isSuggestionsOpen_decorators;
let _isSuggestionsOpen_initializers = [];
let _isSuggestionsOpen_extraInitializers = [];
var ADWaveInputElement2 = class extends _classSuper {
static {
__name(this, "ADWaveInputElement");
}
static {
_classThis = this;
}
static {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_value_decorators = [Attribute({ nullable: false })];
_disabled_decorators = [Attribute({ type: "boolean", nullable: false })];
_name_decorators = [Attribute({ nullable: true })];
_form_decorators = [Attribute({ nullable: true })];
_type_decorators = [Attribute({ nullable: true, default: "text" })];
_placeholder_decorators = [Attribute({ nullable: true })];
_minLength_decorators = [Attribute({ type: "number", nullable: true })];
_maxLength_decorators = [Attribute({ type: "number", nullable: true })];
_errorLabel_decorators = [Attribute({ nullable: true })];
_alertLabel_decorators = [Attribute({ nullable: true })];
_suggestions_decorators = [Attribute({ nullable: true })];
_suggestionsShowAll_decorators = [Attribute({ type: "boolean", nullable: false })];
_suggestionsOrientation_decorators = [Attribute({ nullable: true, default: "down" })];
_fuzzy_decorators = [Attribute({ type: "boolean", nullable: false })];
_availableOptions_decorators = [State()];
_selectedOption_decorators = [State()];
_isSuggestionsOpen_decorators = [State()];
__esDecorate(this, null, _value_decorators, { kind: "accessor", name: "value", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "value" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.value, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.value = value;
}, "set") }, metadata: _metadata }, _value_initializers, _value_extraInitializers);
__esDecorate(this, null, _disabled_decorators, { kind: "accessor", name: "disabled", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "disabled" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.disabled, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.disabled = value;
}, "set") }, metadata: _metadata }, _disabled_initializers, _disabled_extraInitializers);
__esDecorate(this, null, _name_decorators, { kind: "accessor", name: "name", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "name" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.name, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.name = value;
}, "set") }, metadata: _metadata }, _name_initializers, _name_extraInitializers);
__esDecorate(this, null, _form_decorators, { kind: "accessor", name: "form", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "form" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.form, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.form = value;
}, "set") }, metadata: _metadata }, _form_initializers, _form_extraInitializers);
__esDecorate(this, null, _type_decorators, { kind: "accessor", name: "type", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "type" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.type, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.type = value;
}, "set") }, metadata: _metadata }, _type_initializers, _type_extraInitializers);
__esDecorate(this, null, _placeholder_decorators, { kind: "accessor", name: "placeholder", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "placeholder" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.placeholder, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.placeholder = value;
}, "set") }, metadata: _metadata }, _placeholder_initializers, _placeholder_extraInitializers);
__esDecorate(this, null, _minLength_decorators, { kind: "accessor", name: "minLength", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "minLength" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.minLength, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.minLength = value;
}, "set") }, metadata: _metadata }, _minLength_initializers, _minLength_extraInitializers);
__esDecorate(this, null, _maxLength_decorators, { kind: "accessor", name: "maxLength", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "maxLength" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.maxLength, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.maxLength = value;
}, "set") }, metadata: _metadata }, _maxLength_initializers, _maxLength_extraInitializers);
__esDecorate(this, null, _errorLabel_decorators, { kind: "accessor", name: "errorLabel", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "errorLabel" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.errorLabel, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.errorLabel = value;
}, "set") }, metadata: _metadata }, _errorLabel_initializers, _errorLabel_extraInitializers);
__esDecorate(this, null, _alertLabel_decorators, { kind: "accessor", name: "alertLabel", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "alertLabel" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.alertLabel, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.alertLabel = value;
}, "set") }, metadata: _metadata }, _alertLabel_initializers, _alertLabel_extraInitializers);
__esDecorate(this, null, _suggestions_decorators, { kind: "accessor", name: "suggestions", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "suggestions" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.suggestions, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.suggestions = value;
}, "set") }, metadata: _metadata }, _suggestions_initializers, _suggestions_extraInitializers);
__esDecorate(this, null, _suggestionsShowAll_decorators, { kind: "accessor", name: "suggestionsShowAll", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "suggestionsShowAll" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.suggestionsShowAll, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.suggestionsShowAll = value;
}, "set") }, metadata: _metadata }, _suggestionsShowAll_initializers, _suggestionsShowAll_extraInitializers);
__esDecorate(this, null, _suggestionsOrientation_decorators, { kind: "accessor", name: "suggestionsOrientation", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "suggestionsOrientation" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.suggestionsOrientation, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.suggestionsOrientation = value;
}, "set") }, metadata: _metadata }, _suggestionsOrientation_initializers, _suggestionsOrientation_extraInitializers);
__esDecorate(this, null, _fuzzy_decorators, { kind: "accessor", name: "fuzzy", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "fuzzy" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.fuzzy, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.fuzzy = value;
}, "set") }, metadata: _metadata }, _fuzzy_initializers, _fuzzy_extraInitializers);
__esDecorate(this, null, _availableOptions_decorators, { kind: "accessor", name: "availableOptions", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "availableOptions" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.availableOptions, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.availableOptions = value;
}, "set") }, metadata: _metadata }, _availableOptions_initializers, _availableOptions_extraInitializers);
__esDecorate(this, null, _selectedOption_decorators, { kind: "accessor", name: "selectedOption", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "selectedOption" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.selectedOption, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.selectedOption = value;
}, "set") }, metadata: _metadata }, _selectedOption_initializers, _selectedOption_extraInitializers);
__esDecorate(this, null, _isSuggestionsOpen_decorators, { kind: "accessor", name: "isSuggestionsOpen", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "isSuggestionsOpen" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.isSuggestionsOpen, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.isSuggestionsOpen = value;
}, "set") }, metadata: _metadata }, _isSuggestionsOpen_initializers, _isSuggestionsOpen_extraInitializers);
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
ADWaveInputElement2 = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
}
#value_accessor_storage;
get value() {
return this.#value_accessor_storage;
}
set value(value) {
this.#value_accessor_storage = value;
}
#disabled_accessor_storage;
get disabled() {
return this.#disabled_accessor_storage;
}
set disabled(value) {
this.#disabled_accessor_storage = value;
}
#name_accessor_storage;
get name() {
return this.#name_accessor_storage;
}
set name(value) {
this.#name_accessor_storage = value;
}
#form_accessor_storage;
get form() {
return this.#form_accessor_storage;
}
set form(value) {
this.#form_accessor_storage = value;
}
#type_accessor_storage;
get type() {
return this.#type_accessor_storage;
}
set type(value) {
this.#type_accessor_storage = value;
}
#placeholder_accessor_storage;
get placeholder() {
return this.#placeholder_accessor_storage;
}
set placeholder(value) {
this.#placeholder_accessor_storage = value;
}
#minLength_accessor_storage;
get minLength() {
return this.#minLength_accessor_storage;
}
set minLength(value) {
this.#minLength_accessor_storage = value;
}
#maxLength_accessor_storage;
get maxLength() {
return this.#maxLength_accessor_storage;
}
set maxLength(value) {
this.#maxLength_accessor_storage = value;
}
#errorLabel_accessor_storage;
get errorLabel() {
return this.#errorLabel_accessor_storage;
}
set errorLabel(value) {
this.#errorLabel_accessor_storage = value;
}
#alertLabel_accessor_storage;
get alertLabel() {
return this.#alertLabel_accessor_storage;
}
set alertLabel(value) {
this.#alertLabel_accessor_storage = value;
}
#suggestions_accessor_storage;
get suggestions() {
return this.#suggestions_accessor_storage;
}
set suggestions(value) {
this.#suggestions_accessor_storage = value;
}
#suggestionsShowAll_accessor_storage;
get suggestionsShowAll() {
return this.#suggestionsShowAll_accessor_storage;
}
set suggestionsShowAll(value) {
this.#suggestionsShowAll_accessor_storage = value;
}
#suggestionsOrientation_accessor_storage;
get suggestionsOrientation() {
return this.#suggestionsOrientation_accessor_storage;
}
set suggestionsOrientation(value) {
this.#suggestionsOrientation_accessor_storage = value;
}
#fuzzy_accessor_storage;
get fuzzy() {
return this.#fuzzy_accessor_storage;
}
set fuzzy(value) {
this.#fuzzy_accessor_storage = value;
}
#availableOptions_accessor_storage;
get availableOptions() {
return this.#availableOptions_accessor_storage;
}
set availableOptions(value) {
this.#availableOptions_accessor_storage = value;
}
#selectedOption_accessor_storage;
get selectedOption() {
return this.#selectedOption_accessor_storage;
}
set selectedOption(value) {
this.#selectedOption_accessor_storage = value;
}
#isSuggestionsOpen_accessor_storage;
get isSuggestionsOpen() {
return this.#isSuggestionsOpen_accessor_storage;
}
set isSuggestionsOpen(value) {
this.#isSuggestionsOpen_accessor_storage = value;
}
constructor() {
super();
this.#value_accessor_storage = __runInitializers(this, _value_initializers, "");
this.#disabled_accessor_storage = (__runInitializers(this, _value_extraInitializers), __runInitializers(this, _disabled_initializers, false));
this.#name_accessor_storage = (__runInitializers(this, _disabled_extraInitializers), __runInitializers(this, _name_initializers, null));
this.#form_accessor_storage = (__runInitializers(this, _name_extraInitializers), __runInitializers(this, _form_initializers, null));
this.#type_accessor_storage = (__runInitializers(this, _form_extraInitializers), __runInitializers(this, _type_initializers, "text"));
this.#placeholder_accessor_storage = (__runInitializers(this, _type_extraInitializers), __runInitializers(this, _placeholder_initializers, null));
this.#minLength_accessor_storage = (__runInitializers(this, _placeholder_extraInitializers), __runInitializers(this, _minLength_initializers, null));
this.#maxLength_accessor_storage = (__runInitializers(this, _minLength_extraInitializers), __runInitializers(this, _maxLength_initializers, null));
this.#errorLabel_accessor_storage = (__runInitializers(this, _maxLength_extraInitializers), __runInitializers(this, _errorLabel_initializers, null));
this.#alertLabel_accessor_storage = (__runInitializers(this, _errorLabel_extraInitializers), __runInitializers(this, _alertLabel_initializers, null));
this.#suggestions_accessor_storage = (__runInitializers(this, _alertLabel_extraInitializers), __runInitializers(this, _suggestions_initializers, null));
this.#suggestionsShowAll_accessor_storage = (__runInitializers(this, _suggestions_extraInitializers), __runInitializers(this, _suggestionsShowAll_initializers, false));
this.#suggestionsOrientation_accessor_storage = (__runInitializers(this, _suggestionsShowAll_extraInitializers), __runInitializers(this, _suggestionsOrientation_initializers, "down"));
this.#fuzzy_accessor_storage = (__runInitializers(this, _suggestionsOrientation_extraInitializers), __runInitializers(this, _fuzzy_initializers, false));
this.#availableOptions_accessor_storage = (__runInitializers(this, _fuzzy_extraInitializers), __runInitializers(this, _availableOptions_initializers, []));
this.#selectedOption_accessor_storage = (__runInitializers(this, _availableOptions_extraInitializers), __runInitializers(this, _selectedOption_initializers, -1));
this.#isSuggestionsOpen_accessor_storage = (__runInitializers(this, _selectedOption_extraInitializers), __runInitializers(this, _isSuggestionsOpen_initializers, false));
this.uid = (__runInitializers(this, _isSuggestionsOpen_extraInitializers), getUid());
this.isInFocus = false;
this.hasChanged = false;
this.lastScrollIntoView = 0;
this.handleOptionClick = (ev) => {
ev.preventDefault();
ev.stopPropagation();
const target = ev.target;
const idx = target.dataset.opt ? Number(target.dataset.opt) : void 0;
const shouldContinue = this.dispatchEvent(new CustomMouseEvent("optionclick", {
option: idx ? this.availableOptions[idx] : ""
}, ev));
if (idx != null && shouldContinue) {
this.value = this.availableOptions[idx] ?? "";
this.isSuggestionsOpen = false;
}
};
this.handleInputChange = (ev) => {
const inputElem = ev.target;
this.value = inputElem.value;
};
this.handleKeyDown = (ev) => {
switch (ev.key) {
case "ArrowUp":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightPreviousOption();
} else {
this.highlightNextOption();
}
}
});
break;
case "ArrowDown":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightNextOption();
} else {
this.highlightPreviousOption();
}
}
});
break;
case "PageUp":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightPreviousOption(8);
} else {
this.highlightNextOption(8);
}
}
});
break;
case "PageDown":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightNextOption(8);
} else {
this.highlightPreviousOption(8);
}
}
});
break;
case "Home":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightPreviousOption(this.availableOptions.length - 1);
} else {
this.highlightNextOption(this.availableOptions.length - 1);
}
}
});
break;
case "End":
ev.preventDefault();
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
if (this.suggestionsOrientation == "up") {
this.highlightNextOption(this.availableOptions.length - 1);
} else {
this.highlightPreviousOption(this.availableOptions.length - 1);
}
}
});
break;
case "Enter":
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen && this.selectedOption >= 0) {
ev.preventDefault();
const opt = this.availableOptions[this.selectedOption];
if (opt) {
this.value = opt;
this.isSuggestionsOpen = false;
this.dispatchEvent(new InputChangeEvent("select", this.value));
}
} else if (this.hasChanged) {
this.hasChanged = false;
this.dispatchEvent(new InputChangeEvent("submit", this.value));
}
}, () => {
ev.preventDefault();
});
break;
case "Backspace":
this.withCustomKeyEvent(ev, () => {
this.isSuggestionsOpen = true;
}, () => {
ev.preventDefault();
});
break;
case "Escape":
this.withCustomKeyEvent(ev, () => {
if (this.isSuggestionsOpen) {
this.isSuggestionsOpen = false;
}
}, () => {
ev.preventDefault();
});
break;
}
};
this.handleFocus = (ev) => {
this.isInFocus = true;
if (this.availableOptions.length) {
this.isSuggestionsOpen = true;
}
};
this.handleBlur = (ev) => {
this.isInFocus = false;
this.isSuggestionsOpen = false;
if (this.hasChanged) {
this.hasChanged = false;
this.dispatchEvent(new InputChangeEvent("submit", this.value));
}
};
this.Suggestions = () => {
const isHidden = this.suggestions == null || this.availableOptions.length === 0 || this.isSuggestionsOpen === false;
const reversed = this.suggestionsOrientation == "up";
const options = this.availableOptions.map((option, idx) => {
const isActive = idx === this.selectedOption;
return _jsx("div", { "data-opt": idx, class: cls({
[Suggestions.option]: true,
[Suggestions.active]: isActive
}), onclick: this.handleOptionClick, onmousedown: preventDefault, role: "option", children: _jsx("span", { "data-opt": idx, class: "text", children: option }) });
});
if (reversed) {
options.reverse();
}
return _jsx("div", { id: this.uid, class: cls([
{
[Suggestions.suggestions]: true,
"suggestions-options": true,
_adw_hidden: isHidden
},
this.suggestionsOrientation === "up" ? ["orientation-up", "top"] : "orientation-down"
]), role: "listbox", children: options });
};
this.immediateEffect(() => {
this.availableOptions = this.getMatchingOptions();
this.selectedOption = -1;
}, (s) => [s.value, s.suggestions, s.suggestionsShowAll]);
this.immediateEffect(() => {
if (this.isSuggestionsOpen) {
this.selectedOption = -1;
}
}, (s) => [s.isSuggestionsOpen]);
this.effect(() => {
if (this.isSuggestionsOpen) {
this.scrollActiveToView();
}
}, (s) => [s.selectedOption]);
this.effect(() => {
if (this.isSuggestionsOpen) {
this.scrollActiveToView(true);
}
}, (s) => [s.isSuggestionsOpen]);
this.effect(({ isFirstMount }) => {
if (isFirstMount)
return;
if (this.isInFocus) {
this.hasChanged = true;
} else {
this.dispatchEvent(new InputChangeEvent("submit", this.value));
}
}, (s) => [s.value]);
this.lifecycle.once(ElementLifecycleEvent.WillMount, () => {
forceClassName(this, Input.wrapper);
});
}
/**
* Performs a simple search through options with the provided query,
* if the option starts with the exact same string as the query, it
* is considered a match.
*/
search(options, query) {
const results = [];
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (option.toLowerCase().startsWith(query)) {
results.push(option);
}
}
return results;
}
/**
* Performs a fuzzy search through options with the provided query,
* if the option is similar enough to the query, it is considered a
* match.
*/
fuzzySearch(options, query) {
const results = [];
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (fuzzyCmp(query, option.toLowerCase())) {
results.push(option);
}
}
return results;
}
getMatchingOptions() {
if (this.suggestions == null) {
return [];
}
let options = this.suggestions.split(";");
if (this.suggestionsShowAll) {
return options;
}
const value = this.value?.toLowerCase();
if (value == null) {
return options;
}
if (this.fuzzy) {
return this.fuzzySearch(options, value);
} else {
return this.search(options, value);
}
}
/** Scrolls into view the currently highlighted suggestion. */
scrollActiveToView(forceInstant = false) {
const suggestions = this.querySelector(`.${Suggestions.suggestions}`);
if (suggestions == null) {
return;
}
const activeOption = suggestions?.querySelector(`.${Suggestions.active}`);
if (activeOption == null) {
return;
}
const now = Date.now();
if (forceInstant || now - this.lastScrollIntoView <= 100) {
activeOption.scrollIntoView({
behavior: "instant",
block: "nearest"
});
} else {
activeOption.scrollIntoView({
behavior: "smooth",
block: "nearest"
});
}
this.lastScrollIntoView = now;
}
highlightNextOption(offset = 1) {
this.selectedOption = Math.max(-1, this.selectedOption - offset);
}
highlightPreviousOption(offset = 1) {
this.selectedOption = Math.min(this.availableOptions.length - 1, this.selectedOption + offset);
}
withCustomKeyEvent(ev, cb, onCancel) {
ev.stopPropagation();
const shouldContinue = this.dispatchEvent(new CustomKeyboardEvent("keydown", {}, ev));
if (shouldContinue) {
cb();
} else if (onCancel) {
onCancel();
}
}
render() {
return _jsxs(_Fragment, { children: [_jsx("input", { class: cls({
[Input.input]: true,
[Input.disabled]: this.disabled
}), oninput: this.handleInputChange, onkeydown: this.handleKeyDown, onfocus: this.handleFocus, onblur: this.handleBlur, onchange: stopEvent, type: this.type, value: this.value ?? void 0, disabled: this.disabled, name: this.name ?? void 0, form: this.form ?? void 0, placeholder: this.placeholder ?? void 0, minlength: this.minLength ?? void 0, maxlength: this.maxLength ?? void 0, "aria-placeholder": this.placeholder, "aria-label": this.placeholder, "aria-invalid": this.errorLabel != null, "aria-haspopup": "listbox", "aria-expanded": this.isSuggestionsOpen, "aria-controls": this.uid }), _jsx(this.Suggestions, {})] });
}
};
return ADWaveInputElement2 = _classThis;
})();
export {
ADWaveInputElement
};