From f464d27b8483d91d10b835a1eec3e96a194ca59d Mon Sep 17 00:00:00 2001 From: callmeroot Date: Sun, 9 Jun 2024 17:15:23 +0530 Subject: [PATCH] added scoped slots and sorting composable --- docs/pages/playground.vue | 57 ++++++-- lib/KTable/KTable.vue | 234 -------------------------------- lib/KTable/KTableGridItem.vue | 35 ++--- lib/KTable/index.vue | 247 ++++++++++++++++++++++++++++++++++ lib/KTable/useSorting.js | 55 ++++++++ lib/KThemePlugin.js | 2 +- lib/__tests__/KTable.spec.js | 2 +- 7 files changed, 368 insertions(+), 264 deletions(-) delete mode 100644 lib/KTable/KTable.vue create mode 100644 lib/KTable/index.vue create mode 100644 lib/KTable/useSorting.js diff --git a/docs/pages/playground.vue b/docs/pages/playground.vue index 0d9e6f643..6dee4f386 100644 --- a/docs/pages/playground.vue +++ b/docs/pages/playground.vue @@ -21,7 +21,39 @@ { text: 'Level 2 ', link: { path: '#' } }, ]" /> --> - +

KTable Component Example

+

Sortable Table

+ + + + + +

Non-Sortable Table

+ + + + @@ -34,11 +66,11 @@ - - - \ No newline at end of file diff --git a/lib/KTable/KTableGridItem.vue b/lib/KTable/KTableGridItem.vue index 4e7eec564..fe3264eb3 100644 --- a/lib/KTable/KTableGridItem.vue +++ b/lib/KTable/KTableGridItem.vue @@ -1,7 +1,9 @@ @@ -16,22 +18,27 @@ type: [String, Number], required: true, }, - align: { + dataType: { type: String, - default: 'left', + default: 'string', }, rowIndex: { type: Number, required: true, }, - cellIndex: { + colIndex: { type: Number, required: true, }, }, + computed: { + textAlign() { + return this.dataType === 'numeric' ? 'right' : 'left'; + }, + }, methods: { onKeydown(event) { - this.$emit('keydown', event, this.rowIndex, this.cellIndex); + this.$emit('keydown', event, this.rowIndex, this.colIndex); }, }, }; @@ -40,14 +47,10 @@ \ No newline at end of file diff --git a/lib/KTable/index.vue b/lib/KTable/index.vue new file mode 100644 index 000000000..ab8a8a22e --- /dev/null +++ b/lib/KTable/index.vue @@ -0,0 +1,247 @@ + + + + + + + \ No newline at end of file diff --git a/lib/KTable/useSorting.js b/lib/KTable/useSorting.js new file mode 100644 index 000000000..97cbab03d --- /dev/null +++ b/lib/KTable/useSorting.js @@ -0,0 +1,55 @@ +import { ref, computed } from '@vue/composition-api'; + +export const SORT_ORDER_ASC = 'asc'; +export const SORT_ORDER_DESC = 'desc'; +export const DATA_TYPE_STRING = 'string'; +export const DATA_TYPE_NUMERIC = 'numeric'; +export const DATA_TYPE_DATE = 'date'; +export const DATA_TYPE_OTHERS = 'others'; + +export default function useSorting(headers, rows, useLocalSorting) { + const sortKey = ref(null); + const sortOrder = ref(null); + + const sortedRows = computed(() => { + if (!useLocalSorting.value || sortKey.value === null) return rows.value; + + return [...rows.value].sort((a, b) => { + const aValue = a[sortKey.value]; + const bValue = b[sortKey.value]; + + if (sortOrder.value === SORT_ORDER_ASC) { + return aValue > bValue ? 1 : aValue < bValue ? -1 : 0; + } else { + return aValue < bValue ? 1 : aValue > bValue ? -1 : 0; + } + }); + }); + + const handleSort = index => { + if (headers.value[index].dataType === DATA_TYPE_OTHERS) return; + + if (sortKey.value === index) { + sortOrder.value = sortOrder.value === SORT_ORDER_ASC ? SORT_ORDER_DESC : SORT_ORDER_ASC; + } else { + sortKey.value = index; + sortOrder.value = SORT_ORDER_ASC; + } + }; + + const getAriaSort = index => { + if (sortKey.value === index) { + return sortOrder.value === SORT_ORDER_ASC ? 'ascending' : 'descending'; + } else { + return 'none'; + } + }; + + return { + sortKey, + sortOrder, + sortedRows, + handleSort, + getAriaSort, + }; +} diff --git a/lib/KThemePlugin.js b/lib/KThemePlugin.js index d9115a87b..cc021fc54 100644 --- a/lib/KThemePlugin.js +++ b/lib/KThemePlugin.js @@ -26,7 +26,7 @@ import KRadioButton from './KRadioButton'; import KRouterLink from './buttons-and-links/KRouterLink'; import KSelect from './KSelect'; import KSwitch from './KSwitch'; -import KTable from './KTable/KTable'; +import KTable from './KTable'; import KTabs from './tabs/KTabs'; import KTabsList from './tabs/KTabsList'; import KTabsPanel from './tabs/KTabsPanel'; diff --git a/lib/__tests__/KTable.spec.js b/lib/__tests__/KTable.spec.js index 6e8b2ad4c..df85c5259 100644 --- a/lib/__tests__/KTable.spec.js +++ b/lib/__tests__/KTable.spec.js @@ -1,5 +1,5 @@ import { shallowMount } from '@vue/test-utils'; -import KTable from '../../lib/KTable/KTable.vue'; +import KTable from '../../lib/KTable'; describe('KTable.vue', () => { it('should mount the component', () => {