Skip to content

Commit 89532cc

Browse files
committed
refactor: address gemini code review
1 parent 0957421 commit 89532cc

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

apps/client/src/services/attribute_autocomplete.ts

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function initAttributeNameAutocomplete({ $el, attributeType, open, onValueChange
193193
panelEl.style.display = "none";
194194
stopPositioning();
195195
onValueChange?.(inputEl.value);
196-
}, 200);
196+
}, 50);
197197
};
198198
const onKeyDown = (e: KeyboardEvent) => {
199199
if (e.key === "Enter" && isPanelOpen && hasActiveItem) {
@@ -281,6 +281,27 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
281281
let cachedAttributeName = "";
282282
let cachedAttributeValues: NameItem[] = [];
283283

284+
const handleSelect = (item: NameItem) => {
285+
isSelecting = true;
286+
inputEl.value = item.name;
287+
inputEl.dispatchEvent(new Event("input", { bubbles: true }));
288+
autocomplete.setQuery(item.name);
289+
autocomplete.setIsOpen(false);
290+
onValueChange?.(item.name);
291+
isSelecting = false;
292+
293+
setTimeout(() => {
294+
inputEl.dispatchEvent(new KeyboardEvent("keydown", {
295+
key: "Enter",
296+
code: "Enter",
297+
keyCode: 13,
298+
which: 13,
299+
bubbles: true,
300+
cancelable: true
301+
}));
302+
}, 0);
303+
};
304+
284305
const autocomplete = createAutocomplete<NameItem>({
285306
openOnFocus: true,
286307
defaultActiveItemId: null,
@@ -311,24 +332,7 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
311332
return item.name;
312333
},
313334
onSelect({ item }) {
314-
isSelecting = true;
315-
inputEl.value = item.name;
316-
inputEl.dispatchEvent(new Event("input", { bubbles: true }));
317-
autocomplete.setQuery(item.name);
318-
autocomplete.setIsOpen(false);
319-
onValueChange?.(item.name);
320-
isSelecting = false;
321-
322-
setTimeout(() => {
323-
inputEl.dispatchEvent(new KeyboardEvent("keydown", {
324-
key: "Enter",
325-
code: "Enter",
326-
keyCode: 13,
327-
which: 13,
328-
bubbles: true,
329-
cancelable: true
330-
}));
331-
}, 0);
335+
handleSelect(item);
332336
},
333337
},
334338
];
@@ -343,26 +347,7 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
343347
const activeId = state.activeItemId ?? null;
344348

345349
if (state.isOpen && items.length > 0) {
346-
renderItems(panelEl, items, activeId, (item) => {
347-
isSelecting = true;
348-
inputEl.value = item.name;
349-
inputEl.dispatchEvent(new Event("input", { bubbles: true }));
350-
autocomplete.setQuery(item.name);
351-
autocomplete.setIsOpen(false);
352-
onValueChange?.(item.name);
353-
isSelecting = false;
354-
355-
setTimeout(() => {
356-
inputEl.dispatchEvent(new KeyboardEvent("keydown", {
357-
key: "Enter",
358-
code: "Enter",
359-
keyCode: 13,
360-
which: 13,
361-
bubbles: true,
362-
cancelable: true
363-
}));
364-
}, 0);
365-
});
350+
renderItems(panelEl, items, activeId, handleSelect);
366351
startPositioning();
367352
} else {
368353
panelEl.style.display = "none";
@@ -396,7 +381,7 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
396381
panelEl.style.display = "none";
397382
stopPositioning();
398383
onValueChange?.(inputEl.value);
399-
}, 200);
384+
}, 50);
400385
};
401386
const onKeyDown = (e: KeyboardEvent) => {
402387
if (e.key === "Enter" && isPanelOpen && hasActiveItem) {
@@ -430,7 +415,7 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
430415
}
431416
}
432417

433-
export function destroyAttributeNameAutocomplete($el: JQuery<HTMLElement> | HTMLElement) {
418+
export function destroyAutocomplete($el: JQuery<HTMLElement> | HTMLElement) {
434419
const inputEl = $el instanceof HTMLElement ? $el : $el[0] as HTMLInputElement;
435420
const instance = instanceMap.get(inputEl);
436421
if (instance) {
@@ -441,6 +426,6 @@ export function destroyAttributeNameAutocomplete($el: JQuery<HTMLElement> | HTML
441426

442427
export default {
443428
initAttributeNameAutocomplete,
444-
destroyAttributeNameAutocomplete,
429+
destroyAutocomplete,
445430
initLabelValueAutocomplete,
446431
};

0 commit comments

Comments
 (0)