-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {computed, ref} from 'vue' | ||
import type {Ref} from 'vue' | ||
|
||
export enum FilterConfigTypeEnum { | ||
STRING = 'string', | ||
// DATE = 'date', // todo | ||
// SELECT = 'select', // todo | ||
CUSTOM = 'custom' | ||
} | ||
type FilterConfig = { | ||
property: string | ||
type: FilterConfigTypeEnum | ||
propFunc?: (a: any) => any // todo T | ||
customFunc?: (a: any) => boolean // todo T | ||
} | ||
type Filter = FilterConfig & { | ||
value: any // todo type ? | ||
} | ||
|
||
function propFilter<T>(filter: FilterConfig): (item: any) => bool { | ||
let filterFunc: (prop: any) => bool | ||
switch (item.type) { | ||
Check failure on line 22 in frontend/src/composables/useFilter.ts GitHub Actions / lint-and-testssrc/views/users/__tests__/UsersView.vue.test.ts > UsersView.vue > sorts users
Check failure on line 22 in frontend/src/composables/useFilter.ts GitHub Actions / lint-and-testssrc/views/users/__tests__/UsersView.vue.test.ts > UsersView.vue > filters users
|
||
case FilterConfigTypeEnum.DATE: | ||
// todo | ||
break; | ||
case FilterConfigTypeEnum.SELECT: | ||
// todo | ||
break; | ||
case FilterConfigTypeEnum.STRING: | ||
filterFunc = (prop: string) => { | ||
if (filter.filterValue.length < 1) { | ||
return true | ||
} | ||
return String(prop).toLowerCase().includes(filter.filterValue.toLowerCase()) | ||
} | ||
break; | ||
} | ||
|
||
return (item: any): bool => { | ||
if (FilterConfigTypeEnum.CUSTOM === filter.type) { | ||
return filter.customFunc(item) | ||
} | ||
|
||
return filterFunc(filter.propFunc(item)) | ||
} | ||
} | ||
|
||
export function useFilter<T>(options: FilterConfig[], list: Ref<T>[]) { | ||
const filters: Ref<Filter[]> = ref(options.map((option) => ({...option, value: ''}))) | ||
|
||
const filteredList = computed(() => { | ||
const rList = [...list.value] | ||
filters.value.forEach((filter) => { | ||
rList.filter(propFilter(filter)) | ||
}) | ||
return rList | ||
}) | ||
|
||
return { | ||
filteredList | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters