chore(client/ts): fix errors related to autocomplete

This commit is contained in:
Elian Doran 2024-12-19 20:44:21 +02:00
parent e4053de735
commit ffd609e0c5
No known key found for this signature in database
3 changed files with 28 additions and 4 deletions

View File

@ -1,11 +1,19 @@
import { AttributeType } from "../entities/fattribute.js";
import server from "./server.js"; import server from "./server.js";
interface InitOptions {
$el: JQuery<HTMLElement>;
attributeType: AttributeType | (() => AttributeType);
open: boolean;
nameCallback: () => string;
}
/** /**
* @param $el - element on which to init autocomplete * @param $el - element on which to init autocomplete
* @param attributeType - "relation" or "label" or callback providing one of those values as a type of autocompleted attributes * @param attributeType - "relation" or "label" or callback providing one of those values as a type of autocompleted attributes
* @param open - should the autocomplete be opened after init? * @param open - should the autocomplete be opened after init?
*/ */
function initAttributeNameAutocomplete({ $el, attributeType, open }) { function initAttributeNameAutocomplete({ $el, attributeType, open }: InitOptions) {
if (!$el.hasClass("aa-input")) { if (!$el.hasClass("aa-input")) {
$el.autocomplete({ $el.autocomplete({
appendTo: document.querySelector('body'), appendTo: document.querySelector('body'),
@ -39,7 +47,7 @@ function initAttributeNameAutocomplete({ $el, attributeType, open }) {
} }
} }
async function initLabelValueAutocomplete({ $el, open, nameCallback }) { async function initLabelValueAutocomplete({ $el, open, nameCallback }: InitOptions) {
if ($el.hasClass("aa-input")) { if ($el.hasClass("aa-input")) {
// we reinit every time because autocomplete seems to have a bug where it retains state from last // we reinit every time because autocomplete seems to have a bug where it retains state from last
// open even though the value was reset // open even though the value was reset

View File

@ -1,7 +1,7 @@
import server from "./server.js"; import server from "./server.js";
type OptionValue = string; type OptionValue = number | string;
class Options { class Options {
initializedPromise: Promise<void>; initializedPromise: Promise<void>;

View File

@ -47,8 +47,24 @@ declare global {
glob?: CustomGlobals; glob?: CustomGlobals;
} }
interface AutoCompleteConfig {
appendTo?: HTMLElement | null;
hint?: boolean;
openOnFocus?: boolean;
minLength?: number;
tabAutocomplete?: boolean
}
type AutoCompleteCallback = (values: AutoCompleteCallbackArgs[]) => void;
interface AutoCompleteArg {
displayKey: "name" | "value";
cache: boolean;
source: (term: string, cb: AutoCompleteCallback) => void
};
interface JQuery { interface JQuery {
// autocomplete: (action: "close") => void; autocomplete: (action: "close" | "open" | "destroy" | AutoCompleteConfig, args?: AutoCompleteArg[]) => void;
} }
var logError: (message: string) => void; var logError: (message: string) => void;