Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorJ76 committed Dec 10, 2024
1 parent 2badc04 commit 90b0067
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion libs/components/src/lib/tree-item/tree-item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('vwc-tree-item', () => {

expect(treeItem1.contains(document.activeElement)).toBeTruthy();
});
})
});

describe('a11y', () => {
it('should pass html a11y test', async () => {
Expand Down
44 changes: 24 additions & 20 deletions libs/components/src/lib/tree-view/tree-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('vwc-tree-view', () => {
</div>`
)) as HTMLDivElement;
element = divEle.querySelector(COMPONENT_TAG) as TreeView;
const button = divEle.querySelector('button') as HTMLButtonElement;
const button = divEle.querySelector('button') as HTMLButtonElement;
await elementUpdated(element);

element.focus();
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('vwc-tree-view', () => {
});

it('should return true if the click comes from a element that is not a tree-item', async () => {
expect(element.handleClick({ target: {}} as Event)).toBe(true);
expect(element.handleClick({ target: {} } as Event)).toBe(true);
});
});

Expand Down Expand Up @@ -223,47 +223,41 @@ describe('vwc-tree-view', () => {
);
await elementUpdated(element);
await elementUpdated(treeItem2);

expect(treeItem2.contains(document.activeElement)).toBeTruthy();
});

it('should shift focus to the last tree-item when the END key is pressed', async () => {
element.dispatchEvent(
new KeyboardEvent('keydown', { key: keyEnd })
);
element.dispatchEvent(new KeyboardEvent('keydown', { key: keyEnd }));
await elementUpdated(element);
await elementUpdated(treeItem2);

expect(treeItem2.contains(document.activeElement)).toBeTruthy();
});

it('should shift focus to the previous tree-item when the ArrowUp key is pressed', async () => {
element.dispatchEvent(
new KeyboardEvent('keydown', { key: keyEnd })
);
element.dispatchEvent(new KeyboardEvent('keydown', { key: keyEnd }));
await elementUpdated(element);
await elementUpdated(treeItem2);
treeItem2.dispatchEvent(
new KeyboardEvent('keydown', { key: keyArrowUp, bubbles: true })
);
await elementUpdated(element);
await elementUpdated(treeItem1);

expect(treeItem1.contains(document.activeElement)).toBeTruthy();
});

it('should shift focus to the first tree-item when the Home key is pressed', async () => {
element.dispatchEvent(
new KeyboardEvent('keydown', { key: keyEnd })
);
element.dispatchEvent(new KeyboardEvent('keydown', { key: keyEnd }));
await elementUpdated(element);
await elementUpdated(treeItem2);
treeItem2.dispatchEvent(
new KeyboardEvent('keydown', { key: keyHome, bubbles: true })
);
await elementUpdated(element);
await elementUpdated(treeItem1);

expect(treeItem1.contains(document.activeElement)).toBeTruthy();
});

Expand All @@ -272,7 +266,7 @@ describe('vwc-tree-view', () => {
new KeyboardEvent('keydown', { key: keyArrowRight, bubbles: true })
);
await elementUpdated(treeItem1);

expect(treeItem1.getAttribute('aria-expanded')).toEqual('true');
});

Expand All @@ -286,7 +280,7 @@ describe('vwc-tree-view', () => {
new KeyboardEvent('keydown', { key: keyArrowRight, bubbles: true })
);
await elementUpdated(treeItem1_1);

expect(treeItem1_1.contains(document.activeElement)).toBeTruthy();
});

Expand All @@ -295,7 +289,7 @@ describe('vwc-tree-view', () => {
new KeyboardEvent('keydown', { key: keyArrowRight, bubbles: true })
);
await elementUpdated(treeItem1);

expect(treeItem1.getAttribute('aria-expanded')).toEqual('true');

treeItem1.dispatchEvent(
Expand Down Expand Up @@ -335,7 +329,12 @@ describe('vwc-tree-view', () => {
});

it('should return true if the key press is not one the trigger an action', async () => {
expect(element.handleKeyDown({ key: keyBackspace, bubbles: true } as KeyboardEvent)).toBeTruthy();
expect(
element.handleKeyDown({
key: keyBackspace,
bubbles: true,
} as KeyboardEvent)
).toBeTruthy();
});

it('shouold return true if there are no tree-item supplied', async () => {
Expand All @@ -346,7 +345,12 @@ describe('vwc-tree-view', () => {
element.focus();
await elementUpdated(element);

expect(element.handleKeyDown({ key: keyArrowDown, bubbles: true } as KeyboardEvent)).toBeTruthy();
expect(
element.handleKeyDown({
key: keyArrowDown,
bubbles: true,
} as KeyboardEvent)
).toBeTruthy();
});
});

Expand Down
40 changes: 21 additions & 19 deletions libs/components/src/lib/tree-view/tree-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@ import {
import { isTreeItemElement, TreeItem } from '../tree-item/tree-item.js';

export function getDisplayedNodes(
rootNode: HTMLElement,
selector: string
rootNode: HTMLElement,
selector: string
): HTMLElement[] | void {
if (isHTMLElement(rootNode)) {
// get all tree-items
const nodes: HTMLElement[] = Array.from(rootNode.querySelectorAll(selector));

// only include nested items if their parents are expanded
const visibleNodes: HTMLElement[] = nodes.filter((node: HTMLElement) => {
if (node.parentElement instanceof TreeItem) {
if (node.parentElement.getAttribute('aria-expanded') === 'true') return true;
} else {
if (isHTMLElement(rootNode)) {
// get all tree-items
const nodes: HTMLElement[] = Array.from(
rootNode.querySelectorAll(selector)
);

// only include nested items if their parents are expanded
const visibleNodes: HTMLElement[] = nodes.filter((node: HTMLElement) => {
if (node.parentElement instanceof TreeItem) {
if (node.parentElement.getAttribute('aria-expanded') === 'true')
return true;
}
return false;
});
return visibleNodes;
}
} else {
return true;
}
return false;
});
return visibleNodes;
}
}


/**
* @public
* @component tree-view
Expand Down Expand Up @@ -93,7 +95,7 @@ export class TreeView extends FoundationElement {
if (this.currentFocused !== null) {
TreeItem.focusItem(this.currentFocused);
}

return;
}

Expand Down Expand Up @@ -145,7 +147,7 @@ export class TreeView extends FoundationElement {
return true;
}

if(!e.defaultPrevented) {
if (!e.defaultPrevented) {
const treeItems: HTMLElement[] | void = this.getVisibleNodes();

switch (e.key) {
Expand Down

0 comments on commit 90b0067

Please sign in to comment.