-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7306c21
commit 264ab5e
Showing
11 changed files
with
1,012 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
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 @@ | ||
{ | ||
"includePaths": [ | ||
"node_modules", | ||
"../../node_modules" | ||
] | ||
} |
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,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + Lit + TS</title> | ||
<link rel="stylesheet" href="https://1.www.s81c.com/common/carbon-for-ibm-dotcom/tag/v1/latest/plex.css" /> | ||
<link rel="stylesheet" href="./src/index.scss" /> | ||
<script type="module" src="/src/basic.ts"></script> | ||
</head> | ||
<body> | ||
<basic-tanstack-table></basic-tanstack-table> | ||
</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,22 @@ | ||
{ | ||
"name": "basic", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"@carbon/styles": "^1.34.0", | ||
"@carbon/web-components": "latest", | ||
"@tanstack/lit-table": "^8.20.5", | ||
"sass": "^1.64.1", | ||
"lit": "^3.2.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.5.3", | ||
"vite": "^5.4.1" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,151 @@ | ||
import { LitElement, html, css } from 'lit' | ||
import { customElement, state } from 'lit/decorators.js' | ||
import { repeat } from 'lit/directives/repeat.js' | ||
import { | ||
createColumnHelper, | ||
flexRender, | ||
getCoreRowModel, | ||
RowData, | ||
TableController, | ||
} from '@tanstack/lit-table' | ||
import '@carbon/web-components/es/components/data-table/index.js'; | ||
import '@carbon/web-components/es/components/side-panel/index.js'; | ||
import { makeData } from './makeData'; | ||
|
||
type Resource = { | ||
id: string | ||
name: string | ||
rule: string | ||
status: string | ||
other: string | ||
example: string | ||
} | ||
|
||
const columnHelper = createColumnHelper<Resource>() | ||
|
||
const columns = [ | ||
columnHelper.accessor(row => row.name, { | ||
id: 'lastName', | ||
cell: info => html`<i>${info.getValue()}</i>`, | ||
header: () => html`<span>Name</span>`, | ||
}), | ||
columnHelper.accessor('rule', { | ||
header: () => 'Rule', | ||
cell: info => info.renderValue(), | ||
}), | ||
columnHelper.accessor('status', { | ||
header: () => html`<span>Status</span>`, | ||
}), | ||
columnHelper.accessor('other', { | ||
header: 'Other', | ||
}), | ||
columnHelper.accessor('example', { | ||
header: 'Example', | ||
}), | ||
] | ||
|
||
const data: Resource[] = makeData(10); | ||
|
||
/** | ||
* An example table using `@tanstack/lit-table` and `@carbon/web-components` DataTable. | ||
* | ||
*/ | ||
|
||
@customElement('basic-tanstack-table') | ||
export class MyBasicTable extends LitElement { | ||
private tableController = new TableController<Resource>(this); | ||
|
||
@state() | ||
private _panelRowData: null | RowData = null; | ||
|
||
@state() | ||
private _panelOpen = false; | ||
|
||
_handlePanelState(row: RowData) { | ||
// if (!this._panelRowData) { | ||
// return this._panelRowData = row; | ||
// } | ||
// this._panelRowData = null; | ||
this._panelRowData = row; | ||
this._panelOpen = !this._panelOpen; | ||
} | ||
|
||
|
||
|
||
render() { | ||
console.log(this._panelRowData) | ||
const table = this.tableController.table({ | ||
columns, | ||
data, | ||
getCoreRowModel: getCoreRowModel(), | ||
}) | ||
|
||
return html` | ||
<cds-table> | ||
<cds-table-head> | ||
${repeat( | ||
table.getHeaderGroups(), | ||
headerGroup => headerGroup.id, | ||
headerGroup => | ||
html`<cds-table-header-row> | ||
${repeat( | ||
headerGroup.headers, | ||
header => header.id, | ||
header => | ||
html` <cds-table-header-cell> | ||
${header.isPlaceholder | ||
? null | ||
: flexRender( | ||
header.column.columnDef.header, | ||
header.getContext() | ||
)} | ||
</cds-table-header-cell>` | ||
)}</cds-table-header-row>` | ||
)} | ||
</cds-table-head> | ||
<cds-table-body> | ||
${repeat( | ||
table.getRowModel().rows, | ||
row => row.id, | ||
row => html` | ||
<cds-table-row @click=${() => this._handlePanelState(row)}> | ||
${repeat( | ||
row.getVisibleCells(), | ||
cell => cell.id, | ||
cell => | ||
html` <cds-table-cell> | ||
${flexRender( | ||
cell.column.columnDef.cell, | ||
cell.getContext() | ||
)} | ||
</cds-table-cell>` | ||
)} | ||
</cds-table-row> | ||
` | ||
)} | ||
</cds-table-body> | ||
</cds-table> | ||
<cds-side-panel | ||
title=${this._panelOpen ? (this._panelRowData as any).original.name : ''} | ||
?open=${this._panelOpen} | ||
@cds-side-panel-closed=${() => this._panelOpen = false} | ||
>children content</cds-side-panel> | ||
` | ||
} | ||
|
||
static styles = css` | ||
:host { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
padding: 2rem; | ||
display: flex; | ||
place-items: center; | ||
} | ||
` | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'basic-tanstack-table': MyBasicTable | ||
} | ||
} |
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,9 @@ | ||
@use '@carbon/styles/scss/reset'; | ||
@use '@carbon/styles/scss/theme'; | ||
@use '@carbon/styles/scss/themes'; | ||
|
||
:root { | ||
@include theme.theme(themes.$white); | ||
background-color: var(--cds-background); | ||
color: var(--cds-text-primary); | ||
} |
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,76 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
export type Resource = { | ||
id: string | ||
name: string | ||
rule: string | ||
status: string | ||
other: string | ||
example: string | ||
subRows?: Resource[] | ||
} | ||
|
||
const range = (len: number) => { | ||
const arr: number[] = [] | ||
for (let i = 0; i < len; i++) { | ||
arr.push(i) | ||
} | ||
return arr | ||
} | ||
|
||
const newResource = (id: string, index: number): Resource => { | ||
return { | ||
id, | ||
name: `Load balancer ${index}`, | ||
rule: faker.helpers.shuffle<Resource['rule']>([ | ||
'DNS delegation', | ||
'Round Robin' | ||
])[0], | ||
status: faker.helpers.shuffle<Resource['status']>([ | ||
'starting', | ||
'active', | ||
'disabled', | ||
])[0]!, | ||
other: 'Test', | ||
example: faker.number.int(1000).toString(), | ||
} | ||
} | ||
|
||
export function makeData(...lens: number[]) { | ||
const makeDataLevel = (depth = 0): Resource[] => { | ||
const len = lens[depth]! | ||
return range(len).map((index): Resource => { | ||
return { | ||
...newResource(`load-balancer-${index}`, index), | ||
subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined, | ||
} | ||
}) | ||
} | ||
|
||
return makeDataLevel() | ||
} | ||
|
||
//simulates a backend api | ||
const data = makeData(1000) | ||
export const fetchData = async ( | ||
start: number, | ||
size: number, | ||
) => { | ||
|
||
//simulate a backend api | ||
await new Promise(resolve => setTimeout(resolve, 2000)) | ||
|
||
return { | ||
data: data.slice(start, start + size), | ||
meta: { | ||
totalRowCount: data.length, | ||
}, | ||
} | ||
} | ||
|
||
export type ResourceApiResponse = { | ||
data: Resource[] | ||
meta: { | ||
totalRowCount: number | ||
} | ||
} |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"experimentalDecorators": true, | ||
"useDefineForClassFields": false, | ||
"module": "ESNext", | ||
"lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"isolatedModules": true, | ||
"moduleDetection": "force", | ||
"noEmit": true, | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src"] | ||
} |
Oops, something went wrong.