Skip to content

Commit 105ecb9

Browse files
committed
Merge branch 'patch-5' of https://github.com/curran/d3-selection into curran-patch-5
2 parents 690dd20 + c04c9bc commit 105ecb9

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ svg.selectAll("circle")
583583

584584
The selections returned by the *enter* and *update* functions are merged and then returned by *selection*.join.
585585

586-
You also animate enter, update and exit by creating transitions inside the *enter*, *update* and *exit* functions. To avoid breaking the method chain, use *selection*.call to create transitions, or return an undefined enter or update selection to prevent merging: the return value of the *enter* and *update* functions specifies the two selections to merge and return by *selection*.join.
586+
You can animate enter, update and exit by creating transitions inside the *enter*, *update* and *exit* functions. If the *enter* and *update* functions return transitions, their underlying selections are merged and then returned by *selection*.join. The return value of the *exit* function is not used.
587587

588588
For more, see the [*selection*.join notebook](https://observablehq.com/@d3/selection-join).
589589

src/selection/join.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
export default function(onenter, onupdate, onexit) {
22
var enter = this.enter(), update = this, exit = this.exit();
3-
enter = typeof onenter === "function" ? onenter(enter) : enter.append(onenter + "");
4-
if (onupdate != null) update = onupdate(update);
3+
if (typeof onenter === "function") {
4+
enter = onenter(enter);
5+
if (enter) enter = enter.selection();
6+
} else {
7+
enter = enter.append(onenter + "");
8+
}
9+
if (onupdate != null) {
10+
update = onupdate(update);
11+
if (update) update = update.selection();
12+
}
513
if (onexit == null) exit.remove(); else onexit(exit);
614
return enter && update ? enter.merge(update).order() : update;
715
}

test/selection/join-test.js

+19
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@ it("selection.join(…) reorders nodes to match the data", () => {
3535
assert.strictEqual(document.body.innerHTML, "<p>0</p><p>3</p><p>1</p><p>2</p><p>4</p>");
3636
p;
3737
});
38+
39+
it("selection.join(enter, update, exit) allows callbacks to return a transition", "<p>1</p><p>2</p>", () => {
40+
let p = select(document.body).selectAll("p").datum(function() { return this.textContent; });
41+
p = p.data([1, 3], d => d).join(
42+
enter => mockTransition(enter.append("p").attr("class", "enter").text(d => d)),
43+
update => mockTransition(update.attr("class", "update")),
44+
exit => mockTransition(exit.attr("class", "exit"))
45+
);
46+
p;
47+
assert.strictEqual(document.body.innerHTML, "<p class=\"update\">1</p><p class=\"exit\">2</p><p class=\"enter\">3</p>");
48+
});
49+
50+
function mockTransition(selection) {
51+
return {
52+
selection() {
53+
return selection;
54+
}
55+
};
56+
}

0 commit comments

Comments
 (0)