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

Simplify usage of selection.join with transitions #286

Merged
merged 7 commits into from
Jun 7, 2021
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ svg.selectAll("circle")

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

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.
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.

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

Expand Down
12 changes: 10 additions & 2 deletions src/selection/join.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export default function(onenter, onupdate, onexit) {
var enter = this.enter(), update = this, exit = this.exit();
enter = typeof onenter === "function" ? onenter(enter) : enter.append(onenter + "");
if (onupdate != null) update = onupdate(update);
if (typeof onenter === "function") {
enter = onenter(enter);
if (enter) enter = enter.selection();
} else {
enter = enter.append(onenter + "");
}
if (onupdate != null) {
update = onupdate(update);
if (update) update = update.selection();
}
if (onexit == null) exit.remove(); else onexit(exit);
return enter && update ? enter.merge(update).order() : update;
}
18 changes: 18 additions & 0 deletions test/selection/join-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ tape("selection.join(…) reorders nodes to match the data", function(test) {
test.equal(document.body.innerHTML, "<p>0</p><p>3</p><p>1</p><p>2</p><p>4</p>");
test.end();
});

function mockTransition(selection){
return {
selection: function() { return selection; }
}
}

tape("selection.join(enter, update, exit) allows callbacks to return a transition", function(test) {
var document = jsdom("<p>1</p><p>2</p>"),
p = d3.select(document.body).selectAll("p").datum(function() { return this.textContent; });
p = p.data([1, 3], d => d).join(
enter => mockTransition(enter.append("p").attr("class", "enter").text(d => d)),
update => mockTransition(update.attr("class", "update")),
exit => mockTransition(exit.attr("class", "exit"))
);
test.equal(document.body.innerHTML, "<p class=\"update\">1</p><p class=\"exit\">2</p><p class=\"enter\">3</p>");
test.end();
});