Skip to content

Commit

Permalink
fix(#89): add custom event to mw-tabs and improve docs on how to hand…
Browse files Browse the repository at this point in the history
…le tab changes
  • Loading branch information
martin-prinz-mw committed Mar 8, 2023
1 parent cddc94f commit 8928f9f
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,12 @@ export class MwTab {
}
}

export declare interface MwTabs extends Components.MwTabs {}
export declare interface MwTabs extends Components.MwTabs {
/**
* Emits an event when tab is changed
*/
emitter: EventEmitter<CustomEvent<{ selected: number }>>;
}

@ProxyCmp({
defineCustomElementFn: undefined,
Expand All @@ -726,6 +731,7 @@ export class MwTabs {
constructor(c: ChangeDetectorRef, r: ElementRef, protected z: NgZone) {
c.detach();
this.el = r.nativeElement;
proxyOutputs(this, this.el, ["emitter"]);
}
}

Expand Down
14 changes: 12 additions & 2 deletions mwui-stencil/docs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timestamp": "2023-03-02T11:03:55",
"timestamp": "2023-03-08T09:54:51",
"compiler": {
"name": "@stencil/core",
"version": "2.22.2",
Expand Down Expand Up @@ -3029,7 +3029,17 @@
}
],
"methods": [],
"events": [],
"events": [
{
"event": "emitter",
"detail": "{ selected: number; }",
"bubbles": true,
"cancelable": false,
"composed": false,
"docs": "Emits an event when tab is changed",
"docsTags": []
}
],
"listeners": [],
"styles": [],
"slots": [],
Expand Down
8 changes: 8 additions & 0 deletions mwui-stencil/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ export interface MwSwitchCustomEvent<T> extends CustomEvent<T> {
detail: T;
target: HTMLMwSwitchElement;
}
export interface MwTabsCustomEvent<T> extends CustomEvent<T> {
detail: T;
target: HTMLMwTabsElement;
}
export interface MwTextfieldCustomEvent<T> extends CustomEvent<T> {
detail: T;
target: HTMLMwTextfieldElement;
Expand Down Expand Up @@ -1441,6 +1445,10 @@ declare namespace LocalJSX {
selected?: boolean;
}
interface MwTabs {
/**
* Emits an event when tab is changed
*/
onEmitter?: (event: MwTabsCustomEvent<{ selected: number }>) => void;
/**
* Dictates which tab is pre-selected. Omit if no tab should be pre-selected.
*/
Expand Down
28 changes: 22 additions & 6 deletions mwui-stencil/src/components/mw-tabs/mw-tabs.docs.mdx
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { Canvas, Story, ArgsTable } from '@storybook/addon-docs';
import { Canvas, Story, ArgsTable } from "@storybook/addon-docs";

# Tabs

`mw-tabs` is a wrapper for a group of `mw-tab` components and allows the user to switch the active tab by pressing on it.

The amount of tabs and their appearance is determined by how many `mw-tab` components were placed in the `mw-tabs` slot.

The `selected` prop determines which tab is enabled by default.

<ArgsTable of="mw-tabs" />

### Default
A `mw-tab` does not represent tab content but only the selectable tab in the navigation. Implementing behaviour to switch between contents by using `mw-tabs` is up to the application using it.

<Canvas>
<Story id="components-tabs--default" />
</Canvas>

### Listen to Tab changes

Selecting one of the tabs fires a `click` Event containing the current `selected` property.

```javascript
import type { MwCheckboxCustomEvent } from "@maibornwolff/mwui-stencil/dist/types/components";

const onClick = (event: MwTabsCustomEvent<number>) => {
console.log("tabs changed/clicked", event.target.selected);
};
```

```javascript
document.getElementsByTagName("mw-tabs")[0].addEventListener("click", event => {
console.log(event.target.selected);
});
```

<ArgsTable of="mw-tabs" />
18 changes: 17 additions & 1 deletion mwui-stencil/src/components/mw-tabs/mw-tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, h, Element, Prop, State } from "@stencil/core";
import { Event, EventEmitter } from "@stencil/core/internal";

@Component({
tag: "mw-tabs",
Expand All @@ -17,6 +18,21 @@ export class MwTabs {
@Prop({ mutable: true, reflect: true }) selected: number | null = null;
@State() tabs = [];

/**
* Emits an event when tab is changed
*/
@Event({
bubbles: true,
cancelable: false,
composed: false,
})
emitter: EventEmitter<{ selected: number }>;

private handleClick(key: number): void {
this.selected = key;
this.emitter.emit({ selected: key });
}

componentWillRender(): void {
this.tabs = [];
for (let i = 0; i < this.host.children.length; i++) {
Expand All @@ -35,7 +51,7 @@ export class MwTabs {
return (
<div test-id={this.testId} class="mw-tabs">
{this.tabs.map((item, key) => (
<mw-tab key={key} icon={item.icon} label={item.label} selected={item.selected} disabled={item.disabled} onClick={() => (this.selected = key)}></mw-tab>
<mw-tab key={key} icon={item.icon} label={item.label} selected={item.selected} disabled={item.disabled} onClick={() => this.handleClick(key)}></mw-tab>
))}
</div>
);
Expand Down
6 changes: 6 additions & 0 deletions mwui-stencil/src/components/mw-tabs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
| `selected` | `selected` | Dictates which tab is pre-selected. Omit if no tab should be pre-selected. | `number` | `null` |
| `testId` | `test-id` | Test Id | `string` | `undefined` |

## Events

| Event | Description | Type |
| --------- | ---------------------------------- | ------------------------------------ |
| `emitter` | Emits an event when tab is changed | `CustomEvent<{ selected: number; }>` |

## Dependencies

### Depends on
Expand Down
7 changes: 7 additions & 0 deletions mwui-stencil/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ <h2>App Bar</h2>
<script>
const cards = document.getElementsByTagName("mw-card");
const chips = document.getElementsByTagName("mw-chip");
const tabs = document.getElementsByTagName("mw-tabs");

for (var i = 0; i < cards.length; i++) {
cards[i].addEventListener("click", event => {
Expand All @@ -454,5 +455,11 @@ <h2>App Bar</h2>
console.log("chip: toggled", event);
});
}

for (var i = 0; i < tabs.length; i++) {
tabs[i].addEventListener("click", event => {
console.log("tabs clicked", event, event.detail);
});
}
</script>
</html>
2 changes: 1 addition & 1 deletion mwui-vue/lib/components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mwui-vue/lib/components.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mwui-vue/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const MwSwitch = /*@__PURE__*/ defineContainer<JSX.MwSwitch>("mw-switch",

export const MwTab = /*@__PURE__*/ defineContainer<JSX.MwTab>("mw-tab", undefined, ["selected", "icon", "label", "disabled"]);

export const MwTabs = /*@__PURE__*/ defineContainer<JSX.MwTabs>("mw-tabs", undefined, ["testId", "selected"]);
export const MwTabs = /*@__PURE__*/ defineContainer<JSX.MwTabs>("mw-tabs", undefined, ["testId", "selected", "emitter"]);

export const MwTextfield = /*@__PURE__*/ defineContainer<JSX.MwTextfield>("mw-textfield", undefined, [
"type",
Expand Down

0 comments on commit 8928f9f

Please sign in to comment.