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

Call onClose callback when Modal gets closed #5420

Merged
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
8 changes: 8 additions & 0 deletions packages/components/src/components/modal/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ export class KolModal implements API {
public validateActiveElement(value?: HTMLElement | null): void {
watchValidator(this, '_activeElement', (value): boolean => typeof value === 'object' || value === null, new Set(['HTMLElement', 'null']), value, {
defaultValue: null,
hooks: {
afterPatch: () => {
/* Call onClose event in the _activeElement watcher because activeElement can be set internally and from the outside and closes the modal when set to null. */
if (this._activeElement === null && this.state._on?.onClose) {
this.state._on.onClose();
}
},
},
});
}

Expand Down
4 changes: 1 addition & 3 deletions packages/components/src/types/modal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { EventCallback } from './callbacks';

export type KoliBriModalEventCallbacks = {
onClose?: EventCallback<Event>;
onClose?: () => void;
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Event bzw. EventCallback benötigt immer ein Event Parameter, das wir hier nicht haben, wenn das Modal von "außen" geschlossen wurde.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man könnte ein eigenes Event erzeugen. So wird es z. B. in der Card gemacht:

this._on.onClose(new Event('Close'));

2 changes: 1 addition & 1 deletion packages/samples/react/src/components/modal/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const ModalBasic: FC = () => {

return (
<div>
<KolModal _ariaLabel="" _width="80%" ref={modalElement}>
<KolModal _ariaLabel="" _width="80%" ref={modalElement} _on={{ onClose: () => console.log('Modal closed') }}>
<KolCard _heading="Ich bin ein Modal" style={{ width: '100%' }}>
<div slot="content">
<KolButton
Expand Down
Loading