-
Notifications
You must be signed in to change notification settings - Fork 197
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
docs: dashboard editing example #3983
Merged
+766
−11
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f73045a
add Flow and React example
sissbruecker 9c6bca2
use hilla service for saving and loading
sissbruecker 3d078b6
add comments to lit example
sissbruecker a24ab8a
use default config if none was saved previously
sissbruecker e8b125b
remove setting colspan and rowspan
sissbruecker da7156e
add event listeners for updating widgets state
sissbruecker e8aa43f
move render function into useCallback
sissbruecker 6e7a2a3
remove obsolete comment
sissbruecker 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
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
207 changes: 207 additions & 0 deletions
207
frontend/demo/component/dashboard/dashboard-editable.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,207 @@ | ||
import 'Frontend/demo/init'; // hidden-source-line | ||
import '@vaadin/menu-bar'; | ||
import '@vaadin/dashboard/vaadin-dashboard.js'; | ||
import '@vaadin/dashboard/vaadin-dashboard-widget.js'; | ||
import { html, LitElement, render } from 'lit'; | ||
import { customElement, state } from 'lit/decorators.js'; | ||
import type { Dashboard } from '@vaadin/dashboard'; | ||
import type { MenuBarItem, MenuBarItemSelectedEvent } from '@vaadin/menu-bar'; | ||
import type WidgetConfig from 'Frontend/generated/com/vaadin/demo/component/dashboard/WidgetConfig'; | ||
import WidgetType from 'Frontend/generated/com/vaadin/demo/component/dashboard/WidgetConfig/WidgetType'; | ||
import { DashboardService } from 'Frontend/generated/endpoints'; | ||
import { applyTheme } from 'Frontend/generated/theme'; | ||
|
||
// tag::snippet[] | ||
// NOTE: This example uses the additional classes WidgetConfig and DashboardService, | ||
// which you can find by switching to the respective file tab. | ||
|
||
// This is the default configuration for the dashboard. Note that the order | ||
// of the widgets in the array determines the order in which they are | ||
// displayed in the dashboard. | ||
const defaultConfig: WidgetConfig[] = [ | ||
{ type: WidgetType.VISITORS, colspan: 1, rowspan: 1 }, | ||
{ type: WidgetType.DOWNLOADS, colspan: 1, rowspan: 1 }, | ||
{ type: WidgetType.CONVERSIONS, colspan: 1, rowspan: 1 }, | ||
{ type: WidgetType.VISITORS_BY_COUNTRY, colspan: 1, rowspan: 2 }, | ||
{ type: WidgetType.BROWSER_DISTRIBUTION, colspan: 1, rowspan: 1 }, | ||
{ type: WidgetType.CAT_IMAGE, colspan: 1, rowspan: 1 }, | ||
{ type: WidgetType.VISITORS_BY_BROWSER, colspan: 2, rowspan: 1 }, | ||
]; | ||
|
||
// Define a mapping from widget types to human-readable titles | ||
const widgetTitles: Record<WidgetType, string> = { | ||
[WidgetType.VISITORS]: 'Visitors', | ||
[WidgetType.DOWNLOADS]: 'Downloads', | ||
[WidgetType.CONVERSIONS]: 'Conversions', | ||
[WidgetType.VISITORS_BY_COUNTRY]: 'Visitors by country', | ||
[WidgetType.BROWSER_DISTRIBUTION]: 'Browsers', | ||
[WidgetType.CAT_IMAGE]: 'A kittykat!', | ||
[WidgetType.VISITORS_BY_BROWSER]: 'Visitors by browser', | ||
}; | ||
|
||
// Helper type to allow defining a custom action for a menu item | ||
type CustomMenuItem = MenuBarItem & { | ||
action?(): unknown; | ||
}; | ||
|
||
@customElement('dashboard-editable') | ||
export class Example extends LitElement { | ||
// Stores the current dashboard configuration. The vaadin-dashboard component | ||
// will modify this array in place when editing, so there is no need to | ||
// update it using events. | ||
@state() | ||
widgets: WidgetConfig[] = []; | ||
|
||
@state() | ||
editable = false; | ||
|
||
protected override createRenderRoot() { | ||
const root = super.createRenderRoot(); | ||
// Apply custom theme (only supported if your app uses one) | ||
applyTheme(root); | ||
return root; | ||
} | ||
|
||
firstUpdated() { | ||
// Load the initial configuration of the dashboard | ||
this.load(); | ||
} | ||
|
||
toggleEditing() { | ||
this.editable = !this.editable; | ||
} | ||
|
||
async load() { | ||
// To load the dashboard configuration, we just load it from a server-side | ||
// service. If there is no configuration saved, we use a copy of the default | ||
// configuration. | ||
const config = await DashboardService.loadDashboard(); | ||
this.widgets = config ?? [...defaultConfig]; | ||
} | ||
|
||
save() { | ||
// To save the dashboard configuration, we can just take the current | ||
// widget items array and pass it to a server-side service for | ||
// persisting it. | ||
DashboardService.saveDashboard(this.widgets); | ||
} | ||
|
||
addWidget(type: WidgetType) { | ||
// For adding a new widget, we retrieve the default configuration for the | ||
// widget type and add a copy of that to the widgets array. | ||
const defaultWidgetConfig = defaultConfig.find((widget) => widget.type === type); | ||
if (defaultWidgetConfig) { | ||
this.widgets = [...this.widgets, { ...defaultWidgetConfig }]; | ||
} | ||
} | ||
|
||
restore() { | ||
// To restore defaults, we just set a copy of the default configuration | ||
this.widgets = [...defaultConfig]; | ||
} | ||
|
||
render() { | ||
return html` ${this.renderMenu()} ${this.renderDashboard()} `; | ||
} | ||
|
||
renderMenu() { | ||
const menuItems = [ | ||
{ | ||
text: this.editable ? 'Apply' : 'Edit', | ||
action: this.toggleEditing.bind(this), | ||
theme: 'primary', | ||
}, | ||
{ | ||
text: 'Save', | ||
action: this.save.bind(this), | ||
}, | ||
{ | ||
text: 'Load', | ||
action: this.load.bind(this), | ||
}, | ||
{ | ||
text: 'Add widget', | ||
children: Object.values(WidgetType).map((type) => ({ | ||
text: widgetTitles[type], | ||
action: () => this.addWidget(type), | ||
})), | ||
}, | ||
{ | ||
text: 'Restore default', | ||
action: this.restore.bind(this), | ||
theme: 'error', | ||
}, | ||
]; | ||
|
||
return html` | ||
<vaadin-menu-bar | ||
.items="${menuItems}" | ||
@item-selected="${(e: MenuBarItemSelectedEvent) => { | ||
const item = e.detail.value as CustomMenuItem; | ||
item.action?.(); | ||
}}" | ||
theme="dropdown-indicators" | ||
></vaadin-menu-bar> | ||
`; | ||
} | ||
|
||
renderDashboard() { | ||
return html` | ||
<vaadin-dashboard | ||
style="--vaadin-dashboard-col-min-width: 150px; --vaadin-dashboard-col-max-count: 3;" | ||
.editable="${this.editable}" | ||
.items="${this.widgets}" | ||
.renderer="${this.renderWidget}" | ||
></vaadin-dashboard> | ||
`; | ||
} | ||
|
||
renderWidget(root: HTMLElement, _dashboard: Dashboard, { item }: { item: WidgetConfig }) { | ||
// This method is used to render the actual widgets into the dashboard. | ||
// It is called by vaadin-dashboard once for each config in the widgets | ||
// array and should render content into the provided root element. In | ||
// this example all widget types have the same content, so we can use | ||
// generic logic to render a widget. | ||
render( | ||
html` | ||
<vaadin-dashboard-widget | ||
.widgetTitle="${widgetTitles[item.type]}" | ||
style=" | ||
--vaadin-dashboard-item-colspan: ${item.colspan ?? 1}; | ||
--vaadin-dashboard-item-rowspan: ${item.rowspan ?? 1}; | ||
" | ||
> | ||
<div class="dashboard-widget-content"></div> | ||
</vaadin-dashboard-widget> | ||
`, | ||
root | ||
); | ||
|
||
// In practice, different widget types will have different content. | ||
// In that case you can use a switch statement to render the widget | ||
// content based on the type. | ||
// | ||
// let widget: TemplateResult; | ||
// | ||
// switch (item.type) { | ||
// case WidgetType.Visitors: | ||
// widget = html` | ||
// <vaadin-dashboard-widget | ||
// .widgetTitle="Visitors" | ||
// style=" | ||
// --vaadin-dashboard-item-colspan: ${item.colspan ?? 1}; | ||
// --vaadin-dashboard-item-rowspan: ${item.rowspan ?? 1}; | ||
// " | ||
// > | ||
// <visitors-widget-content></visitors-widget-content> | ||
// </vaadin-dashboard-widget> | ||
// `; | ||
// break; | ||
// ... | ||
// } | ||
// | ||
// render(widget, root); | ||
} | ||
} | ||
|
||
// end::snippet[] |
Oops, something went wrong.
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.
There's another issue with the Lit example where clicking "Apply" after moving widgets, the dashboard reverts to the order.
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.
It seems like this assumption isn't true for the Lit version:
docs/frontend/demo/component/dashboard/dashboard-editable.ts
Lines 49 to 51 in a24ab8a
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.
I've added listeners for all modifications to update the widgets state, which solves the issue.
As discussed internally, there is a separate issue with widgets losing focus in move mode, but it doesn't look like it's an issue with the example setup.