-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(vue): vue subcomponents example (#5631)
* examples: vue sub-components * examples: fix vue sub-components package name * prettier and update lock file --------- Co-authored-by: Kevin Vandy <kevinvandy656@gmail.com>
- Loading branch information
1 parent
f159b04
commit 78b9012
Showing
12 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` | ||
- `npm run dev` or `yarn dev` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "tanstack-table-example-vue-subcomponents", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview", | ||
"test:types": "vue-tsc" | ||
}, | ||
"dependencies": { | ||
"@faker-js/faker": "^8.4.1", | ||
"vue": "^3.4.25", | ||
"@tanstack/vue-table": "^8.17.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.12.7", | ||
"@vitejs/plugin-vue": "^5.0.4", | ||
"typescript": "5.4.5", | ||
"vite": "^5.2.10", | ||
"vue-tsc": "^2.0.14" | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
<script setup lang="ts"> | ||
import { | ||
FlexRender, | ||
createColumnHelper, | ||
getCoreRowModel, | ||
getExpandedRowModel, | ||
useVueTable, | ||
type ExpandedState, | ||
type Row, | ||
} from '@tanstack/vue-table' | ||
import { Text, h, ref } from 'vue' | ||
type Person = { | ||
firstName: string | ||
lastName: string | ||
age: number | ||
visits: number | ||
status: string | ||
progress: number | ||
} | ||
const defaultData: Person[] = [ | ||
{ | ||
firstName: 'tanner', | ||
lastName: 'linsley', | ||
age: 24, | ||
visits: 100, | ||
status: 'In Relationship', | ||
progress: 50, | ||
}, | ||
{ | ||
firstName: 'tandy', | ||
lastName: 'miller', | ||
age: 40, | ||
visits: 40, | ||
status: 'Single', | ||
progress: 80, | ||
}, | ||
{ | ||
firstName: 'joe', | ||
lastName: 'dirte', | ||
age: 45, | ||
visits: 20, | ||
status: 'Complicated', | ||
progress: 10, | ||
}, | ||
] | ||
const columnHelper = createColumnHelper<Person>() | ||
function renderExpanded(row: Row<Person>) { | ||
if (!row.getCanExpand()) { | ||
return h(Text, '🔵') | ||
} | ||
return h( | ||
'button', | ||
{ | ||
onClick: row.getToggleExpandedHandler(), | ||
style: { cursor: 'pointer' }, | ||
}, | ||
row.getIsExpanded() ? '👇' : '👉' | ||
) | ||
} | ||
const columns = [ | ||
columnHelper.group({ | ||
header: 'Name', | ||
footer: props => props.column.id, | ||
columns: [ | ||
columnHelper.display({ | ||
id: 'expander', | ||
header: () => null, | ||
cell: ({ row }) => renderExpanded(row), | ||
}), | ||
columnHelper.accessor('firstName', { | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.accessor(row => row.lastName, { | ||
id: 'lastName', | ||
cell: info => info.getValue(), | ||
header: () => 'Last Name', | ||
footer: props => props.column.id, | ||
}), | ||
], | ||
}), | ||
columnHelper.group({ | ||
header: 'Info', | ||
footer: props => props.column.id, | ||
columns: [ | ||
columnHelper.accessor('age', { | ||
header: () => 'Age', | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.group({ | ||
header: 'More Info', | ||
columns: [ | ||
columnHelper.accessor('visits', { | ||
header: () => 'Visits', | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.accessor('status', { | ||
header: 'Status', | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.accessor('progress', { | ||
header: 'Profile Progress', | ||
footer: props => props.column.id, | ||
}), | ||
], | ||
}), | ||
], | ||
}), | ||
] | ||
const data = ref(defaultData) | ||
const expanded = ref<ExpandedState>({}) | ||
const rerender = () => { | ||
data.value = defaultData | ||
} | ||
const table = useVueTable({ | ||
get data() { | ||
return data.value | ||
}, | ||
state: { | ||
get expanded() { | ||
return expanded.value | ||
}, | ||
}, | ||
columns, | ||
getRowCanExpand: () => true, | ||
getCoreRowModel: getCoreRowModel(), | ||
getExpandedRowModel: getExpandedRowModel(), | ||
onExpandedChange: updaterOrValue => { | ||
expanded.value = | ||
typeof updaterOrValue === 'function' | ||
? updaterOrValue(expanded.value) | ||
: updaterOrValue | ||
}, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="p-2"> | ||
<table> | ||
<thead> | ||
<tr | ||
v-for="headerGroup in table.getHeaderGroups()" | ||
:key="headerGroup.id" | ||
> | ||
<th | ||
v-for="header in headerGroup.headers" | ||
:key="header.id" | ||
:colSpan="header.colSpan" | ||
> | ||
<FlexRender | ||
v-if="!header.isPlaceholder" | ||
:render="header.column.columnDef.header" | ||
:props="header.getContext()" | ||
/> | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<template v-for="row in table.getRowModel().rows" :key="row.id"> | ||
<tr> | ||
<td v-for="cell in row.getVisibleCells()" :key="cell.id"> | ||
<FlexRender | ||
:render="cell.column.columnDef.cell" | ||
:props="cell.getContext()" | ||
/> | ||
</td> | ||
</tr> | ||
<tr v-if="row.getIsExpanded()"> | ||
<td :colspan="row.getAllCells().length"> | ||
<pre :style="{ fontSize: '10px' }"> | ||
<code>{{ JSON.stringify(row.original, null, 2) }}</code> | ||
</pre> | ||
</td> | ||
</tr> | ||
</template> | ||
</tbody> | ||
<tfoot> | ||
<tr | ||
v-for="footerGroup in table.getFooterGroups()" | ||
:key="footerGroup.id" | ||
> | ||
<th | ||
v-for="header in footerGroup.headers" | ||
:key="header.id" | ||
:colSpan="header.colSpan" | ||
> | ||
<FlexRender | ||
v-if="!header.isPlaceholder" | ||
:render="header.column.columnDef.footer" | ||
:props="header.getContext()" | ||
/> | ||
</th> | ||
</tr> | ||
</tfoot> | ||
</table> | ||
<div class="h-4" /> | ||
<button @click="rerender" class="border p-2">Rerender</button> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
html { | ||
font-family: sans-serif; | ||
font-size: 14px; | ||
} | ||
table { | ||
border: 1px solid lightgray; | ||
} | ||
tbody { | ||
border-bottom: 1px solid lightgray; | ||
} | ||
th { | ||
border-bottom: 1px solid lightgray; | ||
border-right: 1px solid lightgray; | ||
padding: 2px 4px; | ||
} | ||
tfoot { | ||
color: gray; | ||
} | ||
tfoot th { | ||
font-weight: normal; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
|
||
createApp(App).mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"useDefineForClassFields": true, | ||
"module": "ESNext", | ||
"lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "preserve", | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [vue()], | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.