Skip to content

Commit

Permalink
Merge pull request #2453 from hashicorp/hds-3889/fix-dialog-footer-close
Browse files Browse the repository at this point in the history
`DialogPrimitive`: add a guard so the yielded `close` function is always defined
  • Loading branch information
shleewhite committed Sep 25, 2024
2 parents c69a550 + 7a03dc9 commit e6b4955
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-forks-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hashicorp/design-system-components": patch
---

`DialogPrimitive`: added a guard so the yielded close function is always defined'
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
SPDX-License-Identifier: MPL-2.0
}}
<div class="hds-dialog-primitive__footer {{@contextualClass}}" ...attributes>
{{yield (hash close=@onDismiss)}}
{{yield (hash close=this.onDismiss)}}
</div>
22 changes: 17 additions & 5 deletions packages/components/src/components/hds/dialog-primitive/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: MPL-2.0
*/

import templateOnlyComponent from '@ember/component/template-only';
import Component from '@glimmer/component';

export interface HdsDialogPrimitiveFooterSignature {
Args: {
Expand All @@ -15,14 +15,26 @@ export interface HdsDialogPrimitiveFooterSignature {
default: [
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
close?: (event: MouseEvent, ...args: any[]) => void;
close: (event: MouseEvent, ...args: any[]) => void;
},
];
};
Element: HTMLDivElement;
}

const HdsDialogPrimitiveFooter =
templateOnlyComponent<HdsDialogPrimitiveFooterSignature>();
const NOOP = (): void => {};

export default HdsDialogPrimitiveFooter;
export default class HdsDialogPrimitiveFooter extends Component<HdsDialogPrimitiveFooterSignature> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get onDismiss(): (event: MouseEvent, ...args: any[]) => void {
const { onDismiss } = this.args;

// notice: this is to make sure the function is always defined when consumers add `{{on 'click' F.close}}` to a button in the DialogFooter.
// in reality it's always used inside the main components as a yielded component, so the onDismiss handler is always defined
if (typeof onDismiss === 'function') {
return onDismiss;
} else {
return NOOP;
}
}
}

0 comments on commit e6b4955

Please sign in to comment.