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

fix(AutoComplete): error getText & getIdentifier used before initialized #221

Merged
merged 1 commit into from
Jul 11, 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
12 changes: 10 additions & 2 deletions src/components/forms/auto-complete/RuiAutoComplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ const slots = useSlots();

const { dense, variant, disabled, options } = toRefs(props);

const {
getText,
getIdentifier,
} = useDropdownOptionProperty<T, K>(
{
keyAttr: props.keyAttr,
textAttr: props.textAttr,
},
);

const textInput = ref();
const activator = ref();
const noDataContainer = ref();
Expand Down Expand Up @@ -237,8 +247,6 @@ const {
renderedData,
isOpen,
menuWidth,
getText,
getIdentifier,
isActiveItem,
itemIndexInValue,
highlightedIndex,
Expand Down
62 changes: 40 additions & 22 deletions src/composables/dropdown-menu.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Ref } from 'vue';

export interface DropdownOptions<T, K> {
export interface DropdownItemAttr<T, K> {
keyAttr?: K;
textAttr?: K | ((item: T) => string);
}

export interface DropdownOptions<T, K> extends DropdownItemAttr<T, K> {
options: Ref<T[]>;
dense?: Ref<boolean>;
value: Ref<T | T[] | undefined>;
menuRef: Ref<HTMLElement>;
keyAttr?: K;
textAttr?: K | ((item: T) => string);
appendWidth?: number;
prependWidth?: number;
itemHeight?: number;
Expand All @@ -17,6 +20,35 @@ export interface DropdownOptions<T, K> {
hideSelected?: boolean;
}

export function useDropdownOptionProperty<T, K extends keyof T>({
keyAttr,
textAttr,
}: DropdownItemAttr<T, K>) {
function getText(item: T): T[K] | T | string {
if (textAttr) {
if (typeof textAttr === 'function')
return textAttr(item);

else
return item[textAttr];
}

return item;
}

function getIdentifier(item: T): T[K] | T {
if (keyAttr)
return item[keyAttr];

return item;
}

return {
getIdentifier,
getText,
};
};

export function useDropdownMenu<T, K extends keyof T>({
appendWidth,
autoFocus,
Expand All @@ -41,6 +73,11 @@ export function useDropdownMenu<T, K extends keyof T>({
return options.filter(item => !isActiveItem(item));
});

const { getIdentifier, getText } = useDropdownOptionProperty({
keyAttr,
textAttr,
});

const { containerProps, list, scrollTo, wrapperProps } = useVirtualList<T>(
options,
{
Expand Down Expand Up @@ -99,25 +136,6 @@ export function useDropdownMenu<T, K extends keyof T>({
set(isOpen, state);
}

function getText(item: T): T[K] | T | string {
if (textAttr) {
if (typeof textAttr === 'function')
return textAttr(item);

else
return item[textAttr];
}

return item;
}

function getIdentifier(item: T): T[K] | T {
if (keyAttr)
return item[keyAttr];

return item;
}

function itemIndexInValue(item: T): number {
const val = get(value);
const selected: T[] = Array.isArray(val) ? val : (val ? [val] : []);
Expand Down
Loading