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

Commit

Permalink
feat(DataTable): enable table grouping (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
tewshi authored Jan 3, 2024
1 parent bd6c3c7 commit 5f297bd
Show file tree
Hide file tree
Showing 9 changed files with 853 additions and 407 deletions.
17 changes: 14 additions & 3 deletions .storybook/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { createRui } from '../src';
import * as Icons from '../src/all-icons';
import Vue from 'vue';
import Vue, { ref } from 'vue';

const RuiPlugin = createRui({
theme: {
icons: Object.values(Icons),
}
},
defaults: {
table: {
itemsPerPage: ref(10),
globalItemsPerPage: false,
}
},
})

Vue.use(RuiPlugin);

export const vueInstance = new Vue({ template: '<div />' }).$mount();
export const vueInstance = new Vue({
template: '<div />',
setup() {
RuiPlugin.setupProvide();
}
}).$mount();
4 changes: 4 additions & 0 deletions example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import {
RiArrowUpSLine,
RiCheckboxCircleLine,
RiCloseFill,
RiDeleteBinLine,
RiErrorWarningLine,
RiFileCopyLine,
RiInformationLine,
RiMacbookLine,
RiMoonLine,
Expand Down Expand Up @@ -67,6 +69,8 @@ const RuiPlugin = createRui({
RiArrowUpSLine,
RiArrowDownSLine,
RiArrowDownCircleLine,
RiDeleteBinLine,
RiFileCopyLine,
],
},
defaults: {
Expand Down
47 changes: 30 additions & 17 deletions example/src/views/DataTableView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useFetch } from '@vueuse/core';
import { get, objectOmit, useDebounceFn } from '@vueuse/shared';
import { computed, onBeforeMount, ref } from 'vue';
interface _User {
interface User {
id: number;
name: string;
username: string;
Expand Down Expand Up @@ -104,6 +104,7 @@ const fixedColumns: DataTableColumn[] = [
{
key: 'name',
label: 'Full name',
sortable: true,
},
{
key: 'username',
Expand Down Expand Up @@ -131,6 +132,15 @@ const fixedColumns: DataTableColumn[] = [
];
const fixedRows = [
{
id: 9,
name: 'Glenna Reichert',
username: 'Kamren',
email: 'Chaim_McDermott@dana.io',
website: 'conrad.com',
'address.street': 'Dayna Park',
'address.city': 'Bartholomebury',
},
{
id: 5,
name: 'Chelsey Dietrich',
Expand All @@ -152,7 +162,7 @@ const fixedRows = [
{
id: 3,
name: 'Clementine Bauch',
username: 'Samantha',
username: 'Kamren',
email: 'Nathan@yesenia.net',
website: 'ramiro.info',
'address.street': 'Douglas Extension',
Expand All @@ -167,15 +177,6 @@ const fixedRows = [
'address.street': 'Victor Plains',
'address.city': 'Wisokyburgh',
},
{
id: 9,
name: 'Glenna Reichert',
username: 'Delphine',
email: 'Chaim_McDermott@dana.io',
website: 'conrad.com',
'address.street': 'Dayna Park',
'address.city': 'Bartholomebury',
},
];
const emptyTables = ref<
Expand Down Expand Up @@ -240,6 +241,8 @@ const emptyTables = ref<
pagination: { limit: 5, page: 1, total: 5 },
stickyHeader: true,
stickyOffset: 72,
group: ['username'],
collapsed: [],
},
},
]);
Expand Down Expand Up @@ -486,6 +489,8 @@ const apiDatatables = ref<{ title: string; table: DataTableProps }[]>([
search: '',
sort: { column: 'name', direction: 'asc' },
pagination: { limit: 5, page: 1, total: 0 },
group: ['username'],
collapsed: [],
},
},
{
Expand All @@ -507,7 +512,7 @@ const users = computed<Record<string, any>[]>(() =>
JSON.parse(get(_users) ?? '[]').map(normalize),
);
const normalize = (user: _User): Record<string, any> => {
const normalize = (user: User): Record<string, any> => {
const { address, company } = user;
return {
...objectOmit(user, ['address', 'company']),
Expand Down Expand Up @@ -545,19 +550,19 @@ const fakeFetch = async (
const sort = (by: DataTableSortColumn) => {
result.sort((a, b) => {
if (!by.column) {
if (!by.column?.toString()) {
return 0;
}
if (by.direction === 'desc') {
return `${b[by.column]}`.localeCompare(
`${a[by.column]}`,
return `${b[by.column?.toString()]}`.localeCompare(
`${a[by.column?.toString()]}`,
undefined,
sortOptions,
);
}
return `${a[by.column]}`.localeCompare(
`${b[by.column]}`,
return `${a[by.column?.toString()]}`.localeCompare(
`${b[by.column?.toString()]}`,
undefined,
sortOptions,
);
Expand Down Expand Up @@ -691,7 +696,10 @@ const toggleRow = (row: any, expanded: any[] | undefined) => {
:pagination.sync="table.pagination"
:sort.sync="table.sort"
:data-cy="`table-empty-${i}`"
:group.sync="table.group"
:collapsed.sync="table.collapsed"
>
<template #header.address.city> city custom header </template>
<template #item.action>
<RuiButton icon variant="text" size="sm">
<RuiIcon name="more-fill" color="primary" />
Expand Down Expand Up @@ -833,6 +841,8 @@ const toggleRow = (row: any, expanded: any[] | undefined) => {
:sort-modifiers="{ external: true }"
:pagination.sync="table.pagination"
:sort.sync="table.sort"
:group.sync="table.group"
:collapsed.sync="table.collapsed"
:data-cy="`table-api-${i}`"
@update:options="fetchData(i, $event, table.search, true)"
>
Expand All @@ -841,6 +851,9 @@ const toggleRow = (row: any, expanded: any[] | undefined) => {
<RuiIcon name="more-fill" color="primary" />
</RuiButton>
</template>
<template #group.header.content="{ groupValue }">
custom group content: {{ groupValue }}
</template>
</RuiDataTable>
</div>
</div>
Expand Down
8 changes: 6 additions & 2 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ import {
type Props as StepperProps,
} from '@/components/steppers/Stepper.vue';
import {
type TableColumn as DataTableColumn,
type TableOptions as DataTableOptions,
type Props as DataTableProps,
type SortColumn as DataTableSortColumn,
default as RuiDataTable,
} from '@/components/tables/DataTable.vue';
import {
type TableColumn as DataTableColumn,
type SortColumn as DataTableSortColumn,
type TableSortData as DataTableSortData,
} from '@/components/tables/TableHead.vue';
import {
type Props as ExpandButtonProps,
default as RuiTableRowExpander,
Expand Down Expand Up @@ -175,4 +178,5 @@ export {
SkeletonLoaderProps,
TextAreaProps,
ExpandButtonProps,
DataTableSortData,
};
48 changes: 23 additions & 25 deletions src/components/tables/DataTable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable max-lines */
import { describe, expect, it } from 'vitest';
import { mount } from '@vue/test-utils';
import DataTable, { type TableColumn } from '@/components/tables/DataTable.vue';
import DataTable from '@/components/tables/DataTable.vue';
import { type TableColumn } from '@/components/tables/TableHead.vue';
import TablePagination from '@/components/tables/TablePagination.vue';
import { RuiSimpleSelect } from '~/src';

Expand Down Expand Up @@ -341,10 +342,11 @@ describe('DataTable', () => {
const paginate = wrapper.findAllComponents(TablePagination);
expect(paginate).toHaveLength(2);

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);
expect(paginate.at(1).vm.value).toMatchObject(
const first = paginate.at(0).vm as unknown as typeof TablePagination;
const second = paginate.at(1).vm as unknown as typeof TablePagination;

expect(first.value).toMatchObject(expect.objectContaining({ limit: 25 }));
expect(second.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);

Expand All @@ -353,11 +355,9 @@ describe('DataTable', () => {

await nextTick();

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);
expect(first.value).toMatchObject(expect.objectContaining({ limit: 10 }));

expect(paginate.at(1).vm.value).toMatchObject(
expect(second.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);

Expand Down Expand Up @@ -388,10 +388,11 @@ describe('DataTable', () => {
const paginate = wrapper.findAllComponents(TablePagination);
expect(paginate).toHaveLength(2);

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);
expect(paginate.at(1).vm.value).toMatchObject(
const first = paginate.at(0).vm as unknown as typeof TablePagination;
const second = paginate.at(1).vm as unknown as typeof TablePagination;

expect(first.value).toMatchObject(expect.objectContaining({ limit: 25 }));
expect(second.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);

Expand All @@ -402,11 +403,9 @@ describe('DataTable', () => {

await nextTick();

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);
expect(first.value).toMatchObject(expect.objectContaining({ limit: 10 }));

expect(paginate.at(1).vm.value).toMatchObject(
expect(second.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);

Expand Down Expand Up @@ -436,11 +435,12 @@ describe('DataTable', () => {
const paginate = wrapper.findAllComponents(TablePagination);
expect(paginate).toHaveLength(2);

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);
const first = paginate.at(0).vm as unknown as typeof TablePagination;
const second = paginate.at(1).vm as unknown as typeof TablePagination;

expect(paginate.at(1).vm.value).toMatchObject(
expect(first.value).toMatchObject(expect.objectContaining({ limit: 10 }));

expect(second.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);

Expand All @@ -451,11 +451,9 @@ describe('DataTable', () => {

await nextTick();

expect(paginate.at(0).vm.value).toMatchObject(
expect.objectContaining({ limit: 25 }),
);
expect(first.value).toMatchObject(expect.objectContaining({ limit: 25 }));

expect(paginate.at(1).vm.value).toMatchObject(
expect(second.value).toMatchObject(
expect.objectContaining({ limit: 10 }),
);

Expand Down
Loading

0 comments on commit 5f297bd

Please sign in to comment.