From 8a1bfb1ec07625f28e5d71adc8249720b31d6402 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Wed, 8 Jul 2020 00:20:02 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#45885=20d3-selecti?= =?UTF-8?q?on=20-=20key=20function=20in=20`.data`=20may=20return=20`number?= =?UTF-8?q?`=20as=20well=20by=20@kum-deepak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Test cases to demonstrate `key` function in `.data` calls returning non `string` causes failure. * Update definitions to allow `key` function in `.data` calls to return `number` apart from `string`. --- types/d3-selection/d3-selection-tests.ts | 15 +++++++++++++++ types/d3-selection/index.d.ts | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/types/d3-selection/d3-selection-tests.ts b/types/d3-selection/d3-selection-tests.ts index 318243dcfc..354cdbd96f 100644 --- a/types/d3-selection/d3-selection-tests.ts +++ b/types/d3-selection/d3-selection-tests.ts @@ -771,6 +771,21 @@ tr = d3Selection.select('body') .data([{test: 1}, {test: 2}]) // fails, using this data statement instead, would fail because of its type parameter not being met by input .enter().append('tr'); +let li: d3Selection.Selection; +li = d3Selection.select('li') + .data(matrix[0], d => d) + .enter().append('li'); + +const tr1 = d3Selection.select("body") + .append("table") + .selectAll("tr") + .data(matrix) + .join("tr") + .selectAll("td") + .data(d => d, e => e) + .join("td") + .text(d => d); + nMatrix = tr.data(); // i.e. matrix let td: d3Selection.Selection; diff --git a/types/d3-selection/index.d.ts b/types/d3-selection/index.d.ts index 6f9e2f93be..4d23693039 100644 --- a/types/d3-selection/index.d.ts +++ b/types/d3-selection/index.d.ts @@ -20,6 +20,11 @@ */ export type BaseType = Element | EnterElement | Document | Window | null; +/** + * KeyType serves as alias for valid types that d3 supports as key for data binding + */ +export type KeyType = string | number; + /** * A helper interface which covers arguments like NodeListOf or HTMLCollectionOf * argument types @@ -747,7 +752,7 @@ export interface Selection(data: NewDatum[], key?: ValueFn): Selection; + data(data: NewDatum[], key?: ValueFn): Selection; /** * Joins the data returned by the specified value function with the selected elements, returning a new selection that it represents * the update selection: the elements successfully bound to data. Also defines the enter and exit selections on @@ -780,7 +785,7 @@ export interface Selection(data: ValueFn, key?: ValueFn): Selection; + data(data: ValueFn, key?: ValueFn): Selection; /** * Appends, removes and reorders elements as necessary to match the data that was previously bound by `selection.data`, returning the merged enter and update selection.