-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add `onDestroy` modifier * chore: Add tests * chore: Add `on-click-outside` to index * chore: PR review
- Loading branch information
Showing
5 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
apps/test-app/tests/integration/modifiers/on-destroy-test.gts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { clearRender } from '@ember/test-helpers'; | ||
import { find, render } from '@ember/test-helpers'; | ||
import { onDestroy } from '@nrg-ui/core'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Integration | Modifier | on-destroy', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders', async function (assert) { | ||
assert.expect(6); | ||
|
||
let element: HTMLElement | null = null; | ||
|
||
const callback = (el: HTMLElement, ...args: unknown[]) => { | ||
assert.step('callback'); | ||
assert.strictEqual(el, element, 'Element is passed'); | ||
assert.deepEqual( | ||
args, | ||
[5, 'foo', 'BAR'], | ||
'Additional arguments are passed', | ||
); | ||
}; | ||
|
||
assert.step('before render'); | ||
|
||
await render(<template> | ||
<div {{onDestroy callback 5 "foo" "BAR"}}></div> | ||
</template>); | ||
|
||
assert.step('after render'); | ||
|
||
element = await find('div'); | ||
|
||
await clearRender(); | ||
|
||
assert.verifySteps( | ||
['before render', 'after render', 'callback'], | ||
'Steps are correct', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as onInsert } from './on-insert.ts'; | ||
export { default as onClickOutside } from './on-click-outside.ts'; | ||
export { default as onDestroy } from './on-destroy.ts'; | ||
export { default as onInsert } from './on-insert.ts'; | ||
export { default as onUpdate } from './on-update.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { registerDestructor } from '@ember/destroyable'; | ||
import Modifier from 'ember-modifier'; | ||
|
||
import type Owner from '@ember/owner'; | ||
import type { ArgsFor } from 'ember-modifier'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type AnyArray = any[]; | ||
|
||
type CallbackFn<Args extends AnyArray> = ( | ||
element: HTMLElement, | ||
...args: Args | ||
) => void; | ||
|
||
export interface OnDestroySignature<Args extends AnyArray> { | ||
Element: HTMLElement; | ||
Args: { | ||
Positional: [CallbackFn<Args>, ...Args]; | ||
}; | ||
} | ||
|
||
export default class OnDestroy<Args extends AnyArray> extends Modifier< | ||
OnDestroySignature<Args> | ||
> { | ||
declare callback: () => void; | ||
|
||
constructor(owner: Owner, args: ArgsFor<OnDestroySignature<Args>>) { | ||
super(owner, args); | ||
|
||
registerDestructor(this, () => { | ||
this.callback?.(); | ||
}); | ||
} | ||
|
||
modify( | ||
element: HTMLElement, | ||
|
||
[callback, ...args]: [CallbackFn<Args>, ...Args], | ||
): void { | ||
this.callback = () => callback(element, ...args); | ||
} | ||
} |