From 69e557cd8cd68245262df3bb0f215fd0b380148e Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Tue, 6 Dec 2022 11:37:44 -0500 Subject: [PATCH] convert to static method --- docs/resources/changelog.md | 1 + src/components/tree-item/tree-item.ts | 12 ++++++------ src/components/tree/tree.ts | 11 +++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/resources/changelog.md b/docs/resources/changelog.md index c80941ec..2fb91662 100644 --- a/docs/resources/changelog.md +++ b/docs/resources/changelog.md @@ -18,6 +18,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti - Added `header-actions` slot to `` and `` - Added the `expand-icon` and `collapse-icon` slots to `` and refactored the icon animation [#1046](https://github.com/shoelace-style/shoelace/discussions/1046) - Added the `play-icon` and `pause-icon` slots to `` so you can customize the default icons +- Converted `isTreeItem()` export to a static method of `` - Fixed a bug in `` where `sl-selection-change` was emitted when the selection didn't change [#1030](https://github.com/shoelace-style/shoelace/pull/1030) - Fixed a bug in `` that caused the border to render incorrectly when hovering over icons inside buttons [#1035](https://github.com/shoelace-style/shoelace/issues/1035) - Fixed an incorrect default for `flip-fallback-strategy` in `` that caused the fallback strategy to be `initial` instead of `best-fit`, which is inconsistent with Floating UI's default [#1036](https://github.com/shoelace-style/shoelace/issues/1036) diff --git a/src/components/tree-item/tree-item.ts b/src/components/tree-item/tree-item.ts index dcf92128..d6cf03f5 100644 --- a/src/components/tree-item/tree-item.ts +++ b/src/components/tree-item/tree-item.ts @@ -14,10 +14,6 @@ import '../spinner/spinner'; import styles from './tree-item.styles'; import type { CSSResultGroup, PropertyValueMap } from 'lit'; -export function isTreeItem(node: Node) { - return node instanceof Element && node.getAttribute('role') === 'treeitem'; -} - /** * @summary A tree item serves as a hierarchical node that lives inside a [tree](/components/tree). * @@ -56,6 +52,10 @@ export function isTreeItem(node: Node) { export default class SlTreeItem extends ShoelaceElement { static styles: CSSResultGroup = styles; + static isTreeItem(node: Node) { + return node instanceof Element && node.getAttribute('role') === 'treeitem'; + } + private readonly localize = new LocalizeController(this); @state() indeterminate = false; @@ -185,7 +185,7 @@ export default class SlTreeItem extends ShoelaceElement { getChildrenItems({ includeDisabled = true }: { includeDisabled?: boolean } = {}): SlTreeItem[] { return this.childrenSlot ? ([...this.childrenSlot.assignedElements({ flatten: true })].filter( - (item: SlTreeItem) => isTreeItem(item) && (includeDisabled || !item.disabled) + (item: SlTreeItem) => SlTreeItem.isTreeItem(item) && (includeDisabled || !item.disabled) ) as SlTreeItem[]) : []; } @@ -193,7 +193,7 @@ export default class SlTreeItem extends ShoelaceElement { // Checks whether the item is nested into an item private isNestedItem(): boolean { const parent = this.parentElement; - return !!parent && isTreeItem(parent); + return !!parent && SlTreeItem.isTreeItem(parent); } handleChildrenSlotChange() { diff --git a/src/components/tree/tree.ts b/src/components/tree/tree.ts index 5305f304..e581df5a 100644 --- a/src/components/tree/tree.ts +++ b/src/components/tree/tree.ts @@ -4,16 +4,15 @@ import { clamp } from '../../internal/math'; import ShoelaceElement from '../../internal/shoelace-element'; import { watch } from '../../internal/watch'; import { LocalizeController } from '../../utilities/localize'; -import { isTreeItem } from '../tree-item/tree-item'; +import SlTreeItem from '../tree-item/tree-item'; import styles from './tree.styles'; -import type SlTreeItem from '../tree-item/tree-item'; import type { CSSResultGroup } from 'lit'; function syncCheckboxes(changedTreeItem: SlTreeItem) { function syncAncestors(treeItem: SlTreeItem) { const parentItem: SlTreeItem | null = treeItem.parentElement as SlTreeItem; - if (isTreeItem(parentItem)) { + if (SlTreeItem.isTreeItem(parentItem)) { const children = parentItem.getChildrenItems({ includeDisabled: false }); const allChecked = !!children.length && children.every(item => item.selected); const allUnchecked = children.every(item => !item.selected && !item.indeterminate); @@ -143,8 +142,8 @@ export default class SlTree extends ShoelaceElement { handleTreeChanged = (mutations: MutationRecord[]) => { for (const mutation of mutations) { - const addedNodes: SlTreeItem[] = [...mutation.addedNodes].filter(isTreeItem) as SlTreeItem[]; - const removedNodes = [...mutation.removedNodes].filter(isTreeItem) as SlTreeItem[]; + const addedNodes: SlTreeItem[] = [...mutation.addedNodes].filter(SlTreeItem.isTreeItem) as SlTreeItem[]; + const removedNodes = [...mutation.removedNodes].filter(SlTreeItem.isTreeItem) as SlTreeItem[]; addedNodes.forEach(this.initTreeItem); @@ -343,7 +342,7 @@ export default class SlTree extends ShoelaceElement { } // If the target is a tree item, update the tabindex - if (isTreeItem(target) && !target.disabled) { + if (SlTreeItem.isTreeItem(target) && !target.disabled) { if (this.lastFocusedItem) { this.lastFocusedItem.tabIndex = -1; }