Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SELF-253: Add DataTable #487

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v2.0.51

- Adds the `DataTable` component

## v2.0.50

- Adjust the margin on the menu's divider if the menu has a footer.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lob/ui-components",
"version": "2.0.50",
"version": "2.0.51",
"engines": {
"node": ">=20.2.0",
"npm": ">=10.2.0"
Expand Down
1 change: 1 addition & 0 deletions src/components/Badge/Badge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ withDefaults(
.uic-badge {
@apply inline-flex flex-row gap-1 items-center justify-center;
@apply px-4 py-1 border rounded-full;
@apply truncate;

&.uic-size {
&-sm {
Expand Down
103 changes: 103 additions & 0 deletions src/components/DataTable/DataTable.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Canvas } from '@storybook/addon-docs';
import { Primary } from './DataTable.stories';

# DataTable

<Canvas of={Primary} />

## How to Use

### Basic

```html
<template>
<DataTable :data="[{ id: 'test', name: 'John Smith' }]" data-key="id">
<Column field="id" header="ID" />
<Column field="name" header="Full Name" />
</DataTable>
</template>

<script setup lang="ts">
import { Column, DataTable } from '@lob/ui-components';
</script>
```

### With Async Data

#### Without Pagination

```html
<template>
<DataTable async :data data-key="id" :error="errorMessage" :loading :total>
<Column field="id" header="ID" />
<Column field="name" header="Full Name" />
</DataTable>
</template>

<script setup lang="ts">
import { Column, DataTable } from '@lob/ui-components';
const loading = ref(false);
const data = ref<{ id: number; name: string }>([]);
const total = ref<number>();
const errorMessage = ref<string>();
const fetchData = async () => {
isLoading.value = true;
try {
const response = await fetch('...');
data.value = response.data;
total.value = response.total;
} catch (e) {
errorMessage.value = e.message;
}
isLoading.value = false;
};
</script>
```

#### With Pagination

Pagination state is handled by the component

There is also support for `next`/ `previous` pagination with the `list` prop
using the props `next` and `previous` for button state and the events `@next`
and `@previous`.

```html
<template>
<DataTable
async
:data
data-key="id"
:error="errorMessage"
:loading
:total
@paginate="fetchData"
>
<Column field="id" header="ID" />
<Column field="name" header="Full Name" />
</DataTable>
</template>

<script setup lang="ts">
import { Column, DataTable } from '@lob/ui-components';
const loading = ref(false);
const data = ref<{ id: number; name: string }>([]);
const total = ref<number>([]);
const errorMessage = ref<string>();
const fetchData = async (paginationData) => {
isLoading.value = true;
try {
const response = await fetch('...');
data.value = response.data;
total.value = response.total;
} catch (e) {
errorMessage.value = e.message;
}
isLoading.value = false;
};
</script>
```
101 changes: 101 additions & 0 deletions src/components/DataTable/DataTable.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Meta, StoryObj } from '@storybook/vue3';
import Column from 'primevue/column';
import DataTable from './DataTable.vue';
import mdx from './DataTable.mdx';

const meta: Meta<typeof DataTable> = {
title: 'Components/DataTable',
component: DataTable,
parameters: {
docs: {
page: mdx
}
}
};

export default meta;

export const Primary: StoryObj<typeof DataTable> = {
render: (args) => ({
components: { Column, DataTable },
setup: () => ({ args }),
template: `
<DataTable v-bind="args">
<Column field="firstName" header="First Name" />
<Column field="lastName" header="Last Name" />
<Column field="favoriteColor" header="Favorite Color" />
</DataTable>
`
}),
args: {
dataKey: 'id',
data: [
{
id: 1,
firstName: 'John',
lastName: 'Doe',
favoriteColor: 'Blue'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Doe',
favoriteColor: 'Green'
},
{
id: 3,
firstName: 'Jack',
lastName: 'Doe',
favoriteColor: 'Red'
},
{
id: 4,
firstName: 'Jill',
lastName: 'Doe',
favoriteColor: 'Yellow'
},
{
id: 5,
firstName: 'Jim',
lastName: 'Doe',
favoriteColor: 'Purple'
},
{
id: 6,
firstName: 'Jenny',
lastName: 'Doe',
favoriteColor: 'Orange'
},
{
id: 7,
firstName: 'Joe',
lastName: 'Doe',
favoriteColor: 'Black'
},
{
id: 8,
firstName: 'Jill',
lastName: 'Doe',
favoriteColor: 'White'
},
{
id: 9,
firstName: 'Jack',
lastName: 'Doe',
favoriteColor: 'Gray'
},
{
id: 10,
firstName: 'Jill',
lastName: 'Doe',
favoriteColor: 'Brown'
},
{
id: 11,
firstName: 'Jill',
lastName: 'Doe',
favoriteColor: 'Pink'
}
]
}
};