Skip to content
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

Tab: Adding id as an "optional" params #2872

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 15 additions & 3 deletions stencil-workspace/src/components/modus-tabs/modus-tabs.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,23 @@ describe('modus-tabs', () => {
expect(tabChange).toHaveReceivedEvent();
});

it('renders changes on tabs even without tab id provided', async () => {
const page = await newE2EPage();

await page.setContent('<modus-tabs></modus-tabs>');
const modusTabs = await page.find('modus-tabs');
modusTabs.setProperty('tabs', [{ label: 'Tab1' }, { id: 0, label: 'Tab 2' }]);
await page.waitForChanges();
const element = await page.find('modus-tabs >>> button');

expect(element.id).toEqual('tab-label-tab1');
});

it('renders aria-label on tabs div when set', async () => {
const page = await newE2EPage();

await page.setContent('<modus-tabs aria-label="test label"></modus-tabs>');
let element = await page.find('modus-tabs >>> .modus-tabs');
const element = await page.find('modus-tabs >>> .modus-tabs');
expect(element).toBeDefined();
expect(element).toHaveAttribute('aria-label');
expect(element.getAttribute('aria-label')).toEqual('test label');
Expand All @@ -71,7 +83,7 @@ describe('modus-tabs', () => {
const page = await newE2EPage();

await page.setContent('<modus-tabs></modus-tabs>');
let element = await page.find('modus-tabs >>> .modus-tabs');
const element = await page.find('modus-tabs >>> .modus-tabs');
expect(element).toBeDefined();
expect(element).not.toHaveAttribute('aria-label');
});
Expand All @@ -80,7 +92,7 @@ describe('modus-tabs', () => {
const page = await newE2EPage();

await page.setContent('<modus-tabs aria-label=""></modus-tabs>');
let element = await page.find('modus-tabs >>> .modus-tabs');
const element = await page.find('modus-tabs >>> .modus-tabs');
expect(element).toBeDefined();
expect(element).not.toHaveAttribute('aria-label');
});
Expand Down
5 changes: 5 additions & 0 deletions stencil-workspace/src/components/modus-tabs/modus-tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line
import { Component, Event, EventEmitter, Fragment, h, JSX, Prop } from '@stencil/core';
import { ModusIconMap } from '../../icons/ModusIconMap';
import { kebabCase } from '../../utils/utils';

export interface Tab {
active?: boolean;
Expand Down Expand Up @@ -87,6 +88,10 @@ export class ModusTabs {

render(): unknown {
const tabs = this.tabs.map((tab: Tab) => {
if (!tab.id) {
tab.id = tab?.label ? `tab-label-${kebabCase(tab.label)}` : `tab-label-${this.tabs.indexOf(tab).toString()}`;
}

return (
<button
id={`${tab.id}`}
Expand Down
8 changes: 7 additions & 1 deletion stencil-workspace/src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createGuid, generateElementId } from './utils';
import { createGuid, generateElementId, kebabCase } from './utils';

describe('createGuid', () => {
it('returns truthy guid value', () => {
Expand All @@ -19,3 +19,9 @@ describe('generateElementId', () => {
expect(generateElementId()).toEqual('mwc_id_1');
});
});

describe('kebabCase', () => {
it('should return kebab case string - with whitespace', () => {
expect(kebabCase('This is a string')).toEqual('this-is-a-string');
});
});
7 changes: 7 additions & 0 deletions stencil-workspace/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ let counter = 0;
export function generateElementId(): string {
return `mwc_id_${counter++}`;
}

export function kebabCase(string: string): string {
return string
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.join('-')
.toLowerCase();
}