Skip to content

Commit

Permalink
子组件不存在时,点击报错。涉及组件:collapse, menu, navigation-bar, navigation-rail, ra…
Browse files Browse the repository at this point in the history
…dio-group, segmented-button-group, tabs
  • Loading branch information
zdhxiong committed Nov 25, 2023
1 parent f0b1581 commit 2747185
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
7 changes: 4 additions & 3 deletions packages/mdui/src/components/collapse/collapse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ export class Collapse extends LitElement {
return;
}

// collapse-item 被禁用,忽略
const target = event.target as HTMLElement;
const item = target.closest('mdui-collapse-item') as CollapseItem;
if (item.disabled) {
const item = target.closest('mdui-collapse-item') as CollapseItem | null;

// collapse-item 被禁用,忽略
if (!item || item.disabled) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/mdui/src/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ export class Menu extends LitElement {
}

const target = event.target as HTMLElement;
const item = target.closest('mdui-menu-item') as MenuItem;
const item = target.closest('mdui-menu-item') as MenuItem | null;

if (item?.disabled) {
if (!item || item.disabled) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ export class NavigationBar extends ScrollBehaviorMixin(LayoutItemBase) {
const target = event.target as HTMLElement;
const item = target.closest(
'mdui-navigation-bar-item',
) as NavigationBarItem;
) as NavigationBarItem | null;

if (!item) {
return;
}

this.activeKey = item.key;
this.isInitial = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ export class NavigationRail extends LayoutItemBase {
const target = event.target as HTMLElement;
const item = target.closest(
'mdui-navigation-rail-item',
) as NavigationRailItem;
) as NavigationRailItem | null;

if (!item) {
return;
}

this.activeKey = item.key;
this.isInitial = false;
Expand Down
4 changes: 1 addition & 3 deletions packages/mdui/src/components/radio/radio-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ export class RadioGroup extends LitElement implements FormControl {
await this.definedController.whenDefined();

const target = event.target as HTMLElement;
const item = isNodeName(target, 'mdui-radio')
? (target as Radio)
: ($(target).closest('mdui-radio')[0] as Radio | undefined);
const item = target.closest('mdui-radio') as Radio | null;

if (!item || item.disabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,11 @@ export class SegmentedButtonGroup extends LitElement implements FormControl {
await this.definedController.whenDefined();

const target = event.target as HTMLElement;
const item = target.closest('mdui-segmented-button') as SegmentedButton;
const item = target.closest(
'mdui-segmented-button',
) as SegmentedButton | null;

if (item.disabled) {
if (!item || item.disabled) {
return;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/mdui/src/components/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ export class Tabs extends LitElement {
await this.definedController.whenDefined();

const target = event.target as HTMLElement;
const tab = target.closest('mdui-tab') as Tab;
const tab = target.closest('mdui-tab') as Tab | null;

if (!tab) {
return;
}

this.activeKey = tab.key;
this.isInitial = false;
Expand Down

0 comments on commit 2747185

Please sign in to comment.