Skip to content

Commit c75254f

Browse files
authored
Merge pull request #38 from simonihmig/callbacks
Add onCreate and onUpdate actions
2 parents 5bfd9b5 + c8031ba commit c75254f

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

addon/components/ember-popper-base.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ export default class EmberPopperBase extends Component {
3232
*/
3333
@property onFoundTarget = null
3434

35+
/**
36+
* onCreate callback merged (if present) into the Popper instance's options hash.
37+
* https://popper.js.org/popper-documentation.html#Popper.Defaults.onCreate
38+
*/
39+
@property onCreate = null
40+
41+
/**
42+
* onUpdate callback merged (if present) into the Popper instance's options hash.
43+
* https://popper.js.org/popper-documentation.html#Popper.Defaults.onUpdate
44+
*/
45+
@property onUpdate = null
46+
3547
/**
3648
* Placement of the popper. One of ['top', 'right', 'bottom', 'left'].
3749
*/
@@ -100,6 +112,16 @@ export default class EmberPopperBase extends Component {
100112
*/
101113
@property _updateRAF = null
102114

115+
/**
116+
* Tracks current/previous value of `onCreate` callback
117+
*/
118+
@property _onCreate = null
119+
120+
/**
121+
* Tracks current/previous value of `onUpdate` callback
122+
*/
123+
@property _onUpdate = null
124+
103125
// ================== LIFECYCLE HOOKS ==================
104126

105127
didUpdateAttrs() {
@@ -153,6 +175,7 @@ export default class EmberPopperBase extends Component {
153175
const eventsEnabled = this.get('eventsEnabled');
154176
const modifiers = this.get('modifiers');
155177
const placement = this.get('placement');
178+
const { onCreate, onUpdate } = this;
156179

157180
const isPopperTargetDifferent = popperTarget !== this._popperTarget;
158181

@@ -161,7 +184,9 @@ export default class EmberPopperBase extends Component {
161184
|| isPopperTargetDifferent
162185
|| eventsEnabled !== this._eventsEnabled
163186
|| modifiers !== this._modifiers
164-
|| placement !== this._placement;
187+
|| placement !== this._placement
188+
|| onCreate !== this._onCreate
189+
|| onUpdate !== this._onUpdate;
165190

166191
if (didChange === true) {
167192
if (this._popper !== null) {
@@ -176,8 +201,26 @@ export default class EmberPopperBase extends Component {
176201
this._eventsEnabled = eventsEnabled;
177202
this._modifiers = modifiers;
178203
this._placement = placement;
204+
this._onCreate = onCreate;
205+
this._onUpdate = onUpdate;
206+
207+
const options = {
208+
eventsEnabled,
209+
modifiers,
210+
placement
211+
};
212+
213+
if (onCreate) {
214+
assert('onCreate of ember-popper must be a function', typeof onCreate === 'function');
215+
options.onCreate = onCreate;
216+
}
217+
218+
if (onUpdate) {
219+
assert('onUpdate of ember-popper must be a function', typeof onUpdate === 'function');
220+
options.onUpdate = onUpdate;
221+
}
179222

180-
this._popper = new Popper(popperTarget, popperElement, { eventsEnabled, modifiers, placement });
223+
this._popper = new Popper(popperTarget, popperElement, options);
181224

182225
// Execute the onFoundTarget hook last to ensure the Popper is initialized on the target
183226
if (isPopperTargetDifferent && this.get('onFoundTarget')) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { moduleForComponent, test } from 'ember-qunit';
2+
import hbs from 'htmlbars-inline-precompile';
3+
import { triggerEvent } from 'ember-native-dom-helpers';
4+
import wait from 'ember-test-helpers/wait';
5+
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
6+
7+
if (hasEmberVersion(1, 13)) {
8+
moduleForComponent('ember-popper', 'Integration | Component | actions', {
9+
integration: true
10+
});
11+
12+
test('it calls onCreate', function(assert) {
13+
assert.expect(2);
14+
let called = 0;
15+
const action = (data) => {
16+
called++;
17+
assert.ok(data && data.instance, 'onCreate action is called with dataObject');
18+
};
19+
this.on('create', action);
20+
this.render(hbs`
21+
{{#ember-popper onCreate=(action "create")}}
22+
template block text
23+
{{/ember-popper}}
24+
`);
25+
26+
return wait()
27+
.then(() => assert.equal(called, 1, 'onCreate action has been called'));
28+
});
29+
30+
test('it calls onUpdate', function(assert) {
31+
assert.expect(2);
32+
let called = 0;
33+
const action = (data) => {
34+
called++;
35+
assert.ok(data && data.instance, 'onUpdate action is called with dataObject');
36+
};
37+
this.on('update', action);
38+
this.render(hbs`
39+
{{#ember-popper onUpdate=(action "update")}}
40+
template block text
41+
{{/ember-popper}}
42+
`);
43+
44+
return wait()
45+
.then(() => triggerEvent(document.querySelector('body'), 'scroll'))
46+
.then(wait)
47+
.then(() => {
48+
assert.equal(called, 1, 'onUpdate action has been called after event');
49+
return wait();
50+
});
51+
});
52+
}

0 commit comments

Comments
 (0)