Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

fix(AutoComplete): some issues #202

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/components/forms/auto-complete/RuiAutoComplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ function getListeners(on: Record<string, Function | undefined>, $listeners: Reco
...listenersFiltered,
};
}

function setSelectionRange(start: number, end: number) {
set(searchInputFocused, true);
get(textInput)?.setSelectionRange?.(start, end);
}

defineExpose({
focus: setInputFocus,
setSelectionRange,
});
</script>

<template>
Expand All @@ -459,6 +469,10 @@ function getListeners(on: Record<string, Function | undefined>, $listeners: Reco
fullWidth: true,
persistOnActivatorClick: true,
...menuOptions,
menuClass: [
{ hidden: (optionsWithSelectedHidden.length === 0 && customValue && !slots['no-data']) },
menuOptions?.menuClass,
],
errorMessages,
successMessages,
hint,
Expand Down
4 changes: 2 additions & 2 deletions src/components/overlays/menu/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export interface MenuProps {
openDelay?: number;
closeDelay?: number;
popper?: PopperOptions;
wrapperClass?: string;
menuClass?: string;
wrapperClass?: string | object | string[];
menuClass?: string | object | string[];
closeOnContentClick?: boolean;
persistOnActivatorClick?: boolean;
hint?: string;
Expand Down
42 changes: 24 additions & 18 deletions src/composables/dropdown-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,28 @@ export function useDropdownMenu<T, K extends keyof T>({

function adjustScrollByHighlightedIndex(smooth: boolean = false) {
const index = get(highlightedIndex);
nextTick(() => {
const container = get(menuRef)?.parentElement;
if (container && index > -1) {
const highlightedElem = get(menuRef).getElementsByClassName('highlighted')[0];

if (highlightedElem) {
highlightedElem.scrollIntoView?.({ behavior: smooth ? 'smooth' : 'auto', block: 'nearest' });
if (get(autoFocus) && 'focus' in highlightedElem && typeof highlightedElem.focus === 'function')
highlightedElem?.focus();
}
else {
scrollTo(index);
if (get(autoFocus)) {
const elem = get(menuRef).getElementsByClassName('highlighted')[0];
if (elem && 'focus' in elem && typeof elem.focus === 'function')
elem.focus();
if (index > -1) {
nextTick(() => {
const container = get(menuRef)?.parentElement;
if (container) {
const highlightedElem = get(menuRef).getElementsByClassName('highlighted')[0];

if (highlightedElem) {
highlightedElem.scrollIntoView?.({ behavior: smooth ? 'smooth' : 'auto', block: 'nearest' });
if (get(autoFocus) && 'focus' in highlightedElem && typeof highlightedElem.focus === 'function')
highlightedElem?.focus();
}
else {
scrollTo(index);
if (get(autoFocus)) {
const elem = get(menuRef).getElementsByClassName('highlighted')[0];
if (elem && 'focus' in elem && typeof elem.focus === 'function')
elem.focus();
}
}
}
}
});
});
}
}

function updateOpen(open: boolean) {
Expand Down Expand Up @@ -193,12 +195,16 @@ export function useDropdownMenu<T, K extends keyof T>({
if (get(highlightedIndex) !== -1) {
if (get(autoSelectFirst)) {
set(highlightedIndex, 0);
adjustScrollByHighlightedIndex();
}
else {
set(highlightedIndex, -1);
scrollTo(0);
}
}
else {
scrollTo(0);
}
});

const moveHighlight = (up: boolean) => {
Expand Down
3 changes: 2 additions & 1 deletion tests/setup-files/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ vi.mock('@vueuse/core', async () => {
.fn().mockImplementation((options: []) => ({
containerProps: {
ref: ref(),
onScroll: () => {},
onScroll: vi.fn(),
},
list: computed(() => get(options).map((data, index) => ({ data, index }))),
wrapperProps: {},
scrollTo: vi.fn(),
})),
refDebounced: (ref: Ref) => ref,
};
Expand Down
Loading