Skip to content

Commit

Permalink
feat: minor cleanup of filter types and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jan 16, 2025
1 parent 3f516ee commit 1f58f4f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
66 changes: 33 additions & 33 deletions packages/core/src/collections/api.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { computed, ComputedRef, MaybeRefOrGetter, toValue } from 'vue';
import { getFromPath } from '../utils/path';
import { isObject } from '../../../shared/src';
// import { computed, ComputedRef, MaybeRefOrGetter, toValue } from 'vue';
// import { getFromPath } from '../utils/path';
// import { isObject } from '../../../shared/src';

export interface CollectionInit<TItem> {
/**
* The items to be displayed in the collection.
*/
items: MaybeRefOrGetter<TItem[]>;
/**
* The property to track by, it can be a function that extracts the value from the item. Should be the same as the "value" prop of the option.
*/
key?: string | ((item: TItem) => unknown);
}
// export interface CollectionInit<TItem> {
// /**
// * The items to be displayed in the collection.
// */
// items: MaybeRefOrGetter<TItem[]>;
// /**
// * The property to track by, it can be a function that extracts the value from the item. Should be the same as the "value" prop of the option.
// */
// key?: string | ((item: TItem) => unknown);
// }

export interface CollectionManager<TItem> {
items: ComputedRef<TItem[]>;
key: (item: TItem) => unknown;
}
// export interface CollectionManager<TItem> {
// items: ComputedRef<TItem[]>;
// key: (item: TItem) => unknown;
// }

// TODO: Implement fetching, loading, pagination, adding a new item, etc...
export function defineCollection<TItem>(init: CollectionInit<TItem>): CollectionManager<TItem> {
const { items, key } = init;
// // TODO: Implement fetching, loading, pagination, adding a new item, etc...
// export function defineCollection<TItem>(init: CollectionInit<TItem>): CollectionManager<TItem> {
// const { items, key } = init;

return {
items: computed(() => toValue(items)),
key:
typeof key === 'function'
? key
: item => {
if (key && isObject(item)) {
return getFromPath(item, key, item);
}
// return {
// items: computed(() => toValue(items)),
// key:
// typeof key === 'function'
// ? key
// : item => {
// if (key && isObject(item)) {
// return getFromPath(item, key, item);
// }

return item;
},
};
}
// return item;
// },
// };
// }
4 changes: 2 additions & 2 deletions packages/core/src/collections/filter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface FilterContext<TItem> {
export interface FilterContext<TValue> {
search: string;
option: {
label: string;
item: TItem;
value: TValue;
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/collections/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './api';
// export * from './api';
export * from './filter';
18 changes: 11 additions & 7 deletions packages/core/src/useComboBox/useComboBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,18 @@ export function useComboBox<TOption, TValue = TOption>(
});

const filter = collectionOptions?.filter;

watch(inputValue, textValue => {
renderedOptions.value.forEach(opt => {
opt.setHidden(
filter ? !filter({ option: { item: opt.getValue(), label: opt.getLabel() }, search: textValue }) : false,
);
if (filter) {
watch(inputValue, textValue => {
renderedOptions.value.forEach(opt => {
opt.setHidden(
!filter({
option: { value: opt.getValue(), label: opt.getLabel() },
search: textValue,
}),
);
});
});
});
}

return exposeField(
{
Expand Down

0 comments on commit 1f58f4f

Please sign in to comment.