-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat(manager): data table primitive #2273
Draft
daniellacosse
wants to merge
1
commit into
master
Choose a base branch
from
daniellacosse/data-table-primitive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
203 changes: 203 additions & 0 deletions
203
server_manager/www/views/server_view/server_data_table/index.ts
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,203 @@ | ||
/** | ||
* Copyright 2024 The Outline Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {css, html, LitElement, TemplateResult, nothing} from 'lit'; | ||
import {customElement, property} from 'lit/decorators.js'; | ||
import {classMap} from 'lit/directives/class-map.js'; | ||
|
||
const DEFAULT_COMPARATOR = (value1: string, value2: string): -1 | 0 | 1 => { | ||
if (value1 === value2) return 0; | ||
if (value1 < value2) return -1; | ||
if (value1 > value2) return 1; | ||
}; | ||
|
||
const DEFAULT_RENDER = (value: string): TemplateResult<1> => { | ||
return html`${value}`; | ||
}; | ||
|
||
@customElement('server-data-table') | ||
export class ServerDataTable extends LitElement { | ||
@property({type: Array}) columns: Map< | ||
string, | ||
{ | ||
comparator?: (_value1: string, _value2: string) => -1 | 0 | 1; | ||
render?: (_value: string) => TemplateResult<1>; | ||
} | ||
>; | ||
@property({type: Array}) data: {[columnName: string]: string}[]; | ||
|
||
@property({type: String}) sortColumn?: string; | ||
@property({type: String}) sortDescending?: boolean; | ||
|
||
static styles = css` | ||
.table-container { | ||
container-type: size; | ||
} | ||
|
||
.table { | ||
display: grid; | ||
grid-template-columns: repeat(var(--server-data-table-columns), auto); | ||
} | ||
|
||
.table-header { | ||
font-weight: bold; | ||
color: white; | ||
background-color: hsl(200, 19%, 18%); | ||
position: sticky; | ||
top: 0; | ||
z-index: 1; | ||
} | ||
|
||
.table-row { | ||
background-color: white; | ||
} | ||
|
||
.table-row-shaded { | ||
background-color: hsl(0, 0%, 95%); | ||
} | ||
|
||
.table-header, | ||
.table-row { | ||
box-sizing: border-box; | ||
content-visibility: auto; | ||
font-family: Roboto, system-ui; | ||
padding: 1rem; | ||
} | ||
|
||
.table-row-label { | ||
display: none; | ||
font-weight: bold; | ||
text-transform: uppercase; | ||
font-size: 0.75rem; | ||
margin: 0.25rem 0; | ||
} | ||
|
||
@container (max-width: 540px) { | ||
.table { | ||
grid-template-columns: auto; | ||
} | ||
|
||
.table-header { | ||
display: none; | ||
} | ||
|
||
.table-row { | ||
padding: 0.25rem 1rem; | ||
} | ||
|
||
.table-row-empty { | ||
padding-top: 0; | ||
padding-bottom: 0; | ||
} | ||
|
||
.table-row-start { | ||
padding-top: 1rem; | ||
} | ||
|
||
.table-row-end { | ||
padding-bottom: 1rem; | ||
} | ||
|
||
.table-row-label { | ||
display: block; | ||
} | ||
} | ||
`; | ||
|
||
private get columnNames() { | ||
return [...this.columns.keys()]; | ||
} | ||
|
||
private get transformedData() { | ||
if (this.sortColumn) { | ||
const comparator = | ||
this.columns.get(this.sortColumn)?.comparator ?? DEFAULT_COMPARATOR; | ||
|
||
return this.data.sort((row1, row2) => { | ||
const [value1, value2] = [row1[this.sortColumn], row2[this.sortColumn]]; | ||
|
||
if (this.sortDescending) { | ||
return comparator(value2, value1); | ||
} | ||
|
||
return comparator(value1, value2); | ||
}); | ||
} | ||
|
||
return this.data; | ||
} | ||
|
||
render() { | ||
return html` | ||
<div class="table-container"> | ||
<style> | ||
:host { | ||
--server-data-table-columns: ${this.columnNames.length}; | ||
} | ||
</style> | ||
<div class="table"> | ||
${this.columnNames.map((columnName: string) => { | ||
return this.renderTableHeaderCell(columnName); | ||
})} | ||
${this.transformedData.flatMap((row, rowIndex) => { | ||
return this.columnNames.map((columnName, columnIndex) => { | ||
return this.renderTableDataCell( | ||
columnName, | ||
columnIndex, | ||
row[columnName], | ||
rowIndex | ||
); | ||
}); | ||
})} | ||
</div> | ||
</div> | ||
`; | ||
} | ||
|
||
renderTableHeaderCell(columnName: string) { | ||
if (this.sortColumn === columnName) { | ||
return html`<div class="table-header"> | ||
${columnName} ${this.sortDescending ? '↑' : '↓'} | ||
</div>`; | ||
} | ||
|
||
return html`<div class="table-header">${columnName}</div>`; | ||
} | ||
|
||
renderTableDataCell( | ||
columnName: string, | ||
columnIndex: number, | ||
rowValue: string, | ||
rowIndex: number | ||
) { | ||
const dataCellContents = rowValue | ||
? html`<div class="table-row-label">${columnName}</div> | ||
${(this.columns.get(columnName)?.render ?? DEFAULT_RENDER)(rowValue)}` | ||
: nothing; | ||
|
||
return html`<div | ||
class=${classMap({ | ||
['table-row-end']: columnIndex === this.columnNames.length - 1, | ||
['table-row-shaded']: rowIndex % 2 === 0, | ||
['table-row-start']: columnIndex === 0, | ||
['table-row-empty']: !rowValue, | ||
['table-row']: true, | ||
})} | ||
> | ||
${dataCellContents} | ||
</div>`; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
server_manager/www/views/server_view/server_data_table/stories.ts
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,130 @@ | ||
import {html} from 'lit'; | ||
|
||
import './index'; | ||
import {ServerDataTable} from './index'; | ||
|
||
export default { | ||
title: 'Manager/Server View/Server Data Table', | ||
component: 'server-data-table', | ||
args: { | ||
columns: [ | ||
['id', {}], | ||
['value', {}], | ||
], | ||
data: [ | ||
{id: '0', value: 'value-0'}, | ||
{id: '1', value: 'value-1'}, | ||
{id: '2', value: 'value-2'}, | ||
], | ||
sortColumn: 'id', | ||
sortDescending: true, | ||
}, | ||
}; | ||
|
||
export const BasicExample = ({ | ||
columns, | ||
data, | ||
sortColumn, | ||
sortDescending, | ||
}: ServerDataTable) => { | ||
return html`<server-data-table | ||
.columns=${new Map(columns)} | ||
.data=${data} | ||
.sortColumn=${sortColumn} | ||
.sortDescending=${sortDescending} | ||
/>`; | ||
}; | ||
|
||
export const RenderExample = () => { | ||
return html`<server-data-table | ||
.columns=${new Map([ | ||
['Employee Name', {}], | ||
[ | ||
'Tags', | ||
{ | ||
render(value: string) { | ||
const tags = value.split(','); | ||
|
||
return html`${tags.map( | ||
tag => | ||
html`<span | ||
style="background-color: hsl(200, 19%, 18%); color: white; padding: 2px 6px; margin: 2px; border-radius: 5px;" | ||
>${tag}</span | ||
>` | ||
)}`; | ||
}, | ||
}, | ||
], | ||
])} | ||
.data=${[ | ||
{ | ||
'Employee Name': 'Vini', | ||
Tags: 'Lead,IC,Manager', | ||
}, | ||
{ | ||
'Employee Name': 'Sander', | ||
Tags: 'Lead,IC', | ||
}, | ||
{ | ||
'Employee Name': 'Jyyi', | ||
Tags: 'IC', | ||
}, | ||
{ | ||
'Employee Name': 'Daniel', | ||
Tags: 'IC', | ||
}, | ||
]} | ||
/>`; | ||
}; | ||
|
||
export const ComparatorExample = () => { | ||
return html`<server-data-table | ||
.columns=${new Map([ | ||
['Player Name', {}], | ||
[ | ||
'Score', | ||
{ | ||
comparator(value1: string, value2: string) { | ||
return Number(value1) - Number(value2); | ||
}, | ||
}, | ||
], | ||
])} | ||
.data=${[ | ||
{ | ||
'Player Name': 'graxxxor23', | ||
Score: '32342', | ||
}, | ||
{ | ||
'Player Name': 'kron3_killa', | ||
Score: '123', | ||
}, | ||
{ | ||
'Player Name': 'bigbungus1007', | ||
Score: '123432', | ||
}, | ||
]} | ||
.sortColumn=${'Score'} | ||
.sortDescending=${true} | ||
/>`; | ||
}; | ||
|
||
export const HeavyDataExample = () => { | ||
const data = []; | ||
|
||
let index = 1000; | ||
while (index--) { | ||
data.push({ | ||
id: String(index), | ||
value: String(index), | ||
}); | ||
} | ||
|
||
return html`<server-data-table | ||
.columns=${new Map([ | ||
['id', {}], | ||
['value', {}], | ||
])} | ||
.data=${data} | ||
/>`; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: text and color theming