score weight + quantils tooltip
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<script setup lang="ts">
|
||||
import { SortDirection } from './sort';
|
||||
|
||||
interface Props {
|
||||
currentSortKey: string | null;
|
||||
sortDirection?: SortDirection | null;
|
||||
sortKey: string;
|
||||
}
|
||||
interface Emit {
|
||||
(e: 'sort', key: string, direction: SortDirection): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emit>();
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<th>
|
||||
<slot />
|
||||
<span class="asc" :class="{'opacity-20': (currentSortKey != sortKey || sortDirection != 'asc')}" @click="emit('sort', sortKey, 'asc')">▲</span>
|
||||
|
||||
<span class="desc" :class="{'opacity-20': (currentSortKey != sortKey || sortDirection != 'desc')}" @click="emit('sort', sortKey, 'desc')">▼</span>
|
||||
</th>
|
||||
</template>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
th {
|
||||
@apply relative h-8 pe-3;
|
||||
}
|
||||
span.asc, span.desc {
|
||||
@apply absolute end-1 cursor-pointer text-xs;
|
||||
}
|
||||
span.asc {
|
||||
@apply top-0.5;
|
||||
}
|
||||
span.desc {
|
||||
@apply bottom-0.5;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,3 @@
|
||||
export { default as SortableHeader } from './SortableHeader.vue';
|
||||
export * from './sort';
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { MaybeRefOrGetter, computed, ref, toValue } from "vue";
|
||||
|
||||
export type SortDirection = "asc" | "desc";
|
||||
export type UseSortOptions = {
|
||||
defaultSortKey?: string;
|
||||
defaultSortDirection?: SortDirection;
|
||||
};
|
||||
|
||||
export const useSort = <T>(array: MaybeRefOrGetter<T[]>, options?: UseSortOptions) => {
|
||||
const sortKey = ref<string | null>(options?.defaultSortKey ?? null);
|
||||
const sortDirection = ref<SortDirection | null>(options?.defaultSortDirection ?? null);
|
||||
const sortBy = (key: string, direction: SortDirection) => {
|
||||
sortKey.value = key;
|
||||
sortDirection.value = direction;
|
||||
};
|
||||
const headerProps = computed(() => ({
|
||||
onSort: sortBy,
|
||||
currentSortKey: sortKey.value,
|
||||
sortDirection: sortDirection.value,
|
||||
}));
|
||||
|
||||
const sortedArray = computed(() => toValue(array).sort((a, b) => {
|
||||
if (sortKey.value === null || sortDirection.value === null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const aValue = (a as any)[sortKey.value];
|
||||
const bValue = (b as any)[sortKey.value];
|
||||
|
||||
if (aValue === bValue) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sortDirection.value === "asc") {
|
||||
return aValue > bValue ? 1 : -1;
|
||||
} else {
|
||||
return aValue > bValue ? -1 : 1;
|
||||
}
|
||||
}));
|
||||
|
||||
return { sortedArray, headerProps };
|
||||
}
|
||||
Reference in New Issue
Block a user