Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions components/view-switcher/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# View Switcher
A segmented component to switch between views. Use a view switcher to change between the presentation of a single set of information. It is not recommended to use the switcher to change the information being presented.

## View Switcher [d2l-view-switcher]

<!-- docs: demo code properties name:d2l-view-switcher sandboxTitle:'View Switcher' autoSize:false -->
```html
<script type="module">
import '@brightspace-ui/core/components/view-switcher/view-switcher.js';
import '@brightspace-ui/core/components/view-switcher/view-switcher-button.js';
</script>

<d2l-view-switcher label="Layout Options">
<d2l-view-switcher-button key="list" text="List"></d2l-view-switcher-button>
<d2l-view-switcher-button selected key="tiles" text="Tiles"></d2l-view-switcher-button>
<d2l-view-switcher-button key="table" text="Table"></d2l-view-switcher-button>
</d2l-view-switcher>
```
<!-- docs: start hidden content -->
### Properties

| Property | Type | Description |
|---|---|---|
| `label` | String, required | Sets an accessible label for the switcher. |
<!-- docs: end hidden content -->

## View Switcher Button [d2l-view-switcher-button]

<!-- docs: demo code properties name:d2l-view-switcher-button autoSize:false -->
```html
<script type="module">
import '@brightspace-ui/core/components/view-switcher/view-switcher.js';
import '@brightspace-ui/core/components/view-switcher/view-switcher-button.js';
</script>

<d2l-view-switcher label="Layout Options">
<d2l-view-switcher-button key="list" text="List"></d2l-view-switcher-button>
<d2l-view-switcher-button selected key="tiles" text="Tiles"></d2l-view-switcher-button>
</d2l-view-switcher>
```

<!-- docs: start hidden content -->
### Properties
| Property | Type | Description |
|---|---|---|
|`key` | String, required | Key for the button. |
|`text`| String, required | Text for the button. |
|`selected`| Boolean, default: `false` | Indicates if the item is selected. |

### Events
* `d2l-view-switcher-select`: Dispatched when the item is selected
<!-- docs: end hidden content -->
93 changes: 85 additions & 8 deletions components/view-switcher/demo/view-switcher.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,99 @@
import '../../demo/demo-page.js';
import '../view-switcher.js';
import '../view-switcher-button.js';
import '../../list/list-item-content.js';
import '../../list/list-item.js';
import { tableStyles } from '../../table/table-wrapper.js';
import { html, LitElement, css } from 'lit';

Check failure on line 14 in components/view-switcher/demo/view-switcher.html

View workflow job for this annotation

GitHub Actions / Lint

Member 'css' of the import declaration should be sorted alphabetically

Check failure on line 14 in components/view-switcher/demo/view-switcher.html

View workflow job for this annotation

GitHub Actions / Lint

Expected 'multiple' syntax before 'single' syntax
import { listLayouts } from '../../list/list.js';
window.listLayouts = listLayouts;

class TableView extends LitElement {
static get styles() {
return [tableStyles, css`
:host([hidden]) {
display: none;
}
`];
}

render() {
return html`
<d2l-table-wrapper>
<table class="d2l-table">
<thead>
<tr>
<th>Name</th>
<th>Section</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<th>Jane Doe</th>
<td>Section 1</td>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc viverra felis sit amet magna eleifend lacinia. Duis faucibus commodo tellus.</td>
</tr>
<tr>
<th>John Smith</th>
<td>Section 2</td>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc viverra felis sit amet magna eleifend lacinia. Duis faucibus commodo tellus.</td>
</tr>
</tbody>
</table>
</d2l-table-wrapper>
`;
}
}
customElements.define('d2l-demo-table-view', TableView);
</script>
</head>
<body unresolved>

<d2l-demo-page page-title="d2l-view-switcher">

<h2>Segmented Button</h2>
<h2>View Switcher</h2>

<d2l-demo-snippet>
<template>
<d2l-view-switcher label="Save Options">
<d2l-view-switcher-button selected key="saveAsDraft" text="Save as Draft"></d2l-view-switcher-button>
<d2l-view-switcher-button key="saveAndClose" text="Save and Close"></d2l-view-switcher-button>
<d2l-view-switcher-button key="saveAndNew" text="Save and New"></d2l-view-switcher-button>
</d2l-view-switcher>
</template>
<d2l-view-switcher label="Layout Options">
<d2l-view-switcher-button key="list" text="List"></d2l-view-switcher-button>
<d2l-view-switcher-button selected key="tiles" text="Tiles"></d2l-view-switcher-button>
<d2l-view-switcher-button key="table" text="Table"></d2l-view-switcher-button>
</d2l-view-switcher>
<br><br>

<d2l-list layout="tiles">
<d2l-list-item label="item 1">
<d2l-list-item-content>
<div>Jane Doe</div>
<div slot="secondary">Section 1</div>
<div slot="supporting-info">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc viverra felis sit amet magna eleifend lacinia. Duis faucibus commodo tellus.</div>
</d2l-list-item-content>
</d2l-list-item>
<d2l-list-item label="item 2">
<d2l-list-item-content>
<div>John Smith</div>
<div slot="secondary">Section 2</div>
<div slot="supporting-info">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc viverra felis sit amet magna eleifend lacinia. Duis faucibus commodo tellus.</div>
</d2l-list-item-content>
</d2l-list-item>
</d2l-list>
<d2l-demo-table-view hidden></d2l-demo-table-view>

<script data-demo-hide>
document.querySelector('d2l-view-switcher').addEventListener('d2l-view-switcher-select', e => {
const list = document.querySelector('d2l-list');
const table = document.querySelector('d2l-demo-table-view');
if (e.detail.key === 'table') {
table.removeAttribute('hidden');
list.setAttribute('hidden', '');
} else {
table.setAttribute('hidden', '');
list.removeAttribute('hidden');
list.layout = (e.detail.key === 'tiles' ? listLayouts.tiles : listLayouts.list);

Check failure on line 100 in components/view-switcher/demo/view-switcher.html

View workflow job for this annotation

GitHub Actions / Lint

'listLayouts' is not defined

Check failure on line 100 in components/view-switcher/demo/view-switcher.html

View workflow job for this annotation

GitHub Actions / Lint

'listLayouts' is not defined
}
});
</script>
</d2l-demo-snippet>


Expand Down
4 changes: 2 additions & 2 deletions components/view-switcher/view-switcher-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class ViewSwitcherButton extends PropertyRequiredMixin(FocusMixin(LitElement)) {
static get properties() {
return {
/**
* REQUIRED: Key of the action
* REQUIRED: Key for the button
* @type {string}
*/
key: { type: String, required: true },
/**
* REQUIRED: Button text
* REQUIRED: Text for the button
* @type {string}
*/
text: { type: String, required: true },
Expand Down
8 changes: 7 additions & 1 deletion components/view-switcher/view-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ class ViewSwitcher extends LocalizeCoreElement(LitElement) {
const items = this.items;
this._count = items.length;
if (items.length === 0) return;
if (!items.find(i => i.selected)) items[0].selected = true;
const selectedItems = items.filter(i => i.selected);
if (selectedItems.length === 0) items[0].selected = true;
if (selectedItems.length > 1) { // unselect all but the first selected item
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added so the d2l-view-switcher-button demo would work properly on the component page

for (let i = 1; i < selectedItems.length; i++) {
selectedItems[i].selected = false;
}
}
}

}
Expand Down
Loading