Skip to content

Commit

Permalink
feat: table grouping, provide table defaults, adjust examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tewshi committed Jan 12, 2024
1 parent 2c096f5 commit 64561ba
Show file tree
Hide file tree
Showing 11 changed files with 988 additions and 435 deletions.
17 changes: 11 additions & 6 deletions .storybook/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { createApp } from 'vue';
import { createApp, ref } from 'vue';
import { createRui } from '../src';
import * as Icons from '../src/icons';

const RuiPlugin = createRui({
theme: {
icons: Object.values(Icons),
}
})
},
defaults: {
table: {
itemsPerPage: ref(10),
globalItemsPerPage: false,
limits: [5, 10, 15, 25, 50, 100, 200],
},
},
});

const app = createApp({ template: '<div />' });
app
.use(RuiPlugin)
.mount(null);
app.use(RuiPlugin).mount(null);

export const vueInstance = app;
10 changes: 5 additions & 5 deletions example/cypress/e2e/data-table.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ describe('DataTable', () => {
cy.get('@buttons').eq(2).as('button2');

cy.get('@multiple')
.find('table tbody tr[hidden] div[data-cy=expanded-content]')
.should('exist');
.find('table tbody tr div[data-cy=expanded-content]')
.should('not.exist');

cy.get('@button1').click();

Expand Down Expand Up @@ -163,8 +163,8 @@ describe('DataTable', () => {
cy.get('@buttons').eq(2).as('button2');

cy.get('@single')
.find('table tbody tr[hidden] div[data-cy=expanded-content]')
.should('exist');
.find('table tbody tr div[data-cy=expanded-content]')
.should('not.exist');

cy.get('@button1').click();

Expand Down Expand Up @@ -196,7 +196,7 @@ describe('DataTable', () => {
});
});

it.only('checks for data tables with sticky header', () => {
it('checks for data tables with sticky header', () => {
cy.get('div[data-cy="table-expandable-0"]').as('sticky');

cy.get('@sticky')
Expand Down
5 changes: 5 additions & 0 deletions example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
RiArrowUpSLine,
RiCheckboxCircleLine,
RiCloseFill,
RiDeleteBinLine,
RiErrorWarningLine,
RiFileCopyLine,
RiInformationLine,
RiMacbookLine,
RiMoonLine,
Expand Down Expand Up @@ -60,12 +62,15 @@ const RuiPlugin = createRui({
RiArrowUpSLine,
RiArrowDownSLine,
RiArrowDownCircleLine,
RiDeleteBinLine,
RiFileCopyLine,
],
},
defaults: {
table: {
itemsPerPage,
globalItemsPerPage: false,
limits: [5, 10, 15, 25, 50, 100, 200],
},
},
});
Expand Down
60 changes: 57 additions & 3 deletions example/src/views/DataTableView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const fixedColumns: DataTableColumn<BaseUser>[] = [
{
key: 'name',
label: 'Full name',
sortable: true,
},
{
key: 'username',
Expand Down Expand Up @@ -164,7 +165,7 @@ const fixedRows: ExtendedUser[] = [
{
id: 3,
name: 'Clementine Bauch',
username: 'Samantha',
username: 'Kamren',
email: '[email protected]',
website: 'ramiro.info',
'address.street': 'Douglas Extension',
Expand All @@ -180,14 +181,50 @@ const fixedRows: ExtendedUser[] = [
'address.city': 'Wisokyburgh',
},
{
id: 9,
id: 19,
name: 'Glenna Reichert',
username: 'Delphine',
username: 'Kamren',
email: '[email protected]',
website: 'conrad.com',
'address.street': 'Dayna Park',
'address.city': 'Bartholomebury',
},
{
id: 15,
name: 'Chelsey Dietrich',
username: 'Kamren',
email: '[email protected]',
website: 'demarco.info',
'address.street': 'Skiles Walks',
'address.city': 'Roscoeview',
},
{
id: 110,
name: 'Clementina DuBuque',
username: 'Moriah.Stanton',
email: '[email protected]',
website: 'ambrose.net',
'address.street': 'Kattie Turnpike',
'address.city': 'Lebsackbury',
},
{
id: 13,
name: 'Clementine Bauch',
username: 'Kamren',
email: '[email protected]',
website: 'ramiro.info',
'address.street': 'Douglas Extension',
'address.city': 'McKenziehaven',
},
{
id: 12,
name: 'Ervin Howell',
username: 'Antonette',
email: '[email protected]',
website: 'anastasia.net',
'address.street': 'Victor Plains',
'address.city': 'Wisokyburgh',
},
];
const emptyTables = ref<
Expand Down Expand Up @@ -256,6 +293,8 @@ const emptyTables = ref<
pagination: { limit: 5, page: 1, total: 5 },
stickyHeader: true,
stickyOffset: 72,
group: ['username'],
collapsed: [],
},
},
]);
Expand Down Expand Up @@ -305,6 +344,8 @@ const expandableTables = ref<
sort: [{ column: 'name', direction: 'asc' }],
pagination: { limit: 5, page: 1, total: 5 },
expanded: [],
stickyHeader: true,
stickyOffset: 72,
},
},
]);
Expand Down Expand Up @@ -510,6 +551,8 @@ const apiDatatables = ref<
search: '',
sort: { column: 'name', direction: 'asc' },
pagination: { limit: 5, page: 1, total: 0 },
group: ['username'],
collapsed: [],
},
},
{
Expand Down Expand Up @@ -733,8 +776,11 @@ const toggleRow = (row: any, expanded: any[] | undefined) => {
v-model="table.modelValue"
v-model:pagination="table.pagination"
v-model:sort="table.sort"
v-model:group="table.group"
v-model:collapsed="table.collapsed"
:data-cy="`table-empty-${i}`"
>
<template #header.address.city> city custom header</template>
<template #item.actions>
<RuiButton icon variant="text" size="sm">
<RuiIcon name="more-fill" color="primary" />
Expand Down Expand Up @@ -877,6 +923,8 @@ const toggleRow = (row: any, expanded: any[] | undefined) => {
v-model="table.modelValue"
v-model:pagination.external="table.pagination"
v-model:sort.external="table.sort"
v-model:group="table.group"
v-model:collapsed="table.collapsed"
:data-cy="`table-api-${i}`"
@update:options="fetchData(i, $event, table.search, true)"
>
Expand All @@ -885,6 +933,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>

<!-- check types -->
Expand All @@ -894,6 +945,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: 7 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ import {
} from '@/components/forms/select/SimpleSelect.vue';
import {
type TableColumn as DataTableColumn,
type GroupKeys as DataTableGroupKeys,
type SortColumn as DataTableSortColumn,
type TableSortData as DataTableSortData,
} from '@/components/tables/TableHead.vue';
import {
type TableOptions as DataTableOptions,
type Props as DataTableProps,
type SortColumn as DataTableSortColumn,
default as RuiDataTable,
} from '@/components/tables/DataTable.vue';
import {
Expand Down Expand Up @@ -195,4 +199,6 @@ export {
DividerProps,
TextAreaProps,
ExpandButtonProps,
DataTableSortData,
DataTableGroupKeys,
};
38 changes: 22 additions & 16 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 { type ComponentMountingOptions, mount } from '@vue/test-utils';
import DataTable, { type TableColumn } from '@/components/tables/DataTable.vue';
import { type TableColumn } from '@/components/tables/TableHead.vue';
import DataTable from '@/components/tables/DataTable.vue';
import TablePagination from '@/components/tables/TablePagination.vue';
import { RuiSimpleSelect } from '~/src';

Expand All @@ -19,7 +20,9 @@ const createWrapper = (
...options,
global: {
provide: {
[TableSymbol.valueOf()]: createTableDefaults(),
[TableSymbol.valueOf()]: createTableDefaults({
limits: [5, 10, 15, 25, 50, 100, 200],
}),
},
},
});
Expand Down Expand Up @@ -123,9 +126,9 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr[hidden]:nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
).toBeFalsy();

await wrapper
.find('tbody tr:nth-child(1) button[class*=_tr__expander_button]')
Expand All @@ -139,9 +142,9 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr[hidden]:nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeFalsy();
).toBeTruthy();
});

it('multiple expand toggles correctly', async () => {
Expand All @@ -167,7 +170,7 @@ describe('DataTable', () => {
wrapper
.find('tbody tr[hidden]:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
).toBeFalsy();

await wrapper
.find('tbody tr:nth-child(1) button[class*=_tr__expander_button]')
Expand All @@ -183,7 +186,7 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr:not(hidden):nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();

Expand All @@ -201,7 +204,7 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr:not(hidden):nth-child(4) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(4) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
});
Expand All @@ -228,9 +231,9 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr[hidden]:nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
).toBeFalsy();

await wrapper
.find('tbody tr:nth-child(1) button[class*=_tr__expander_button]')
Expand All @@ -246,7 +249,7 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr:not(hidden):nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();

Expand All @@ -258,9 +261,9 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr[hidden]:nth-child(2) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(2) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
).toBeFalsy();

await wrapper
.find('tbody tr:nth-child(1) button[class*=_tr__expander_button]')
Expand All @@ -286,9 +289,9 @@ describe('DataTable', () => {

expect(
wrapper
.find('tbody tr:not(hidden):nth-child(4) div[data-cy=expanded-content]')
.find('tbody tr:nth-child(4) div[data-cy=expanded-content]')
.exists(),
).toBeTruthy();
).toBeFalsy();
});

it('sticky header behaves as expected', async () => {
Expand Down Expand Up @@ -327,6 +330,7 @@ describe('DataTable', () => {
[TableSymbol.valueOf()]: createTableDefaults({
itemsPerPage,
globalItemsPerPage: true,
limits: [5, 10, 15, 25, 50, 100, 200],
}),
},
},
Expand Down Expand Up @@ -373,6 +377,7 @@ describe('DataTable', () => {
[TableSymbol.valueOf()]: createTableDefaults({
itemsPerPage,
globalItemsPerPage: true,
limits: [5, 10, 15, 25, 50, 100, 200],
}),
},
},
Expand Down Expand Up @@ -423,6 +428,7 @@ describe('DataTable', () => {
provide: {
[TableSymbol.valueOf()]: createTableDefaults({
itemsPerPage,
limits: [5, 10, 15, 25, 50, 100, 200],
}),
},
},
Expand Down
Loading

0 comments on commit 64561ba

Please sign in to comment.