@@ -5,18 +5,19 @@ import { useTheme, usePropertyAccessor, useValueFormatter } from '@nivo/core'
5
5
import { useOrdinalColorScale , useInheritedColor } from '@nivo/colors'
6
6
import { sankeyAlignmentFromProp } from './props'
7
7
import {
8
+ DefaultLink ,
9
+ DefaultNode ,
8
10
SankeyAlignFunction ,
9
11
SankeyCommonProps ,
10
12
SankeyDataProps ,
11
- SankeyId ,
12
13
SankeyLinkDatum ,
13
14
SankeyNodeDatum ,
14
15
SankeySortFunction ,
15
16
} from './types'
16
17
17
- const getId = ( d : { id : string | number } ) => d . id
18
+ const getId = ( d : { id : string } ) => d . id
18
19
19
- export const computeNodeAndLinks = < Id extends SankeyId > ( {
20
+ export const computeNodeAndLinks = < N extends DefaultNode , L extends DefaultLink > ( {
20
21
data : _data ,
21
22
formatValue,
22
23
layout,
@@ -31,19 +32,19 @@ export const computeNodeAndLinks = <Id extends SankeyId>({
31
32
getColor,
32
33
getLabel,
33
34
} : {
34
- data : SankeyDataProps < Id > [ 'data' ]
35
- formatValue : ( value : number ) => string | number
36
- layout : SankeyCommonProps < Id > [ 'layout' ]
35
+ data : SankeyDataProps < N , L > [ 'data' ]
36
+ formatValue : ( value : number ) => string
37
+ layout : SankeyCommonProps < N , L > [ 'layout' ]
37
38
alignFunction : SankeyAlignFunction
38
39
sortFunction : SankeySortFunction
39
40
linkSortMode : any
40
- nodeThickness : SankeyCommonProps < Id > [ 'nodeThickness' ]
41
- nodeSpacing : SankeyCommonProps < Id > [ 'nodeSpacing' ]
42
- nodeInnerPadding : SankeyCommonProps < Id > [ 'nodeInnerPadding' ]
41
+ nodeThickness : SankeyCommonProps < N , L > [ 'nodeThickness' ]
42
+ nodeSpacing : SankeyCommonProps < N , L > [ 'nodeSpacing' ]
43
+ nodeInnerPadding : SankeyCommonProps < N , L > [ 'nodeInnerPadding' ]
43
44
width : number
44
45
height : number
45
- getColor : ( node : Omit < SankeyNodeDatum < Id > , 'color' | 'label' > ) => string
46
- getLabel : ( node : Omit < SankeyNodeDatum < Id > , 'color' | 'label' > ) => string | number
46
+ getColor : ( node : Omit < SankeyNodeDatum < N , L > , 'color' | 'label' > ) => string
47
+ getLabel : ( node : Omit < SankeyNodeDatum < N , L > , 'color' | 'label' > ) => string
47
48
} ) => {
48
49
const sankey = d3Sankey ( )
49
50
. nodeAlign ( alignFunction )
@@ -58,8 +59,8 @@ export const computeNodeAndLinks = <Id extends SankeyId>({
58
59
// deep clone is required as the sankey diagram mutates data
59
60
// we need a different identity for correct updates
60
61
const data = ( cloneDeep ( _data ) as unknown ) as {
61
- nodes : SankeyNodeDatum < Id > [ ]
62
- links : SankeyLinkDatum < Id > [ ]
62
+ nodes : SankeyNodeDatum < N , L > [ ]
63
+ links : SankeyLinkDatum < N , L > [ ]
63
64
}
64
65
sankey ( data )
65
66
@@ -109,7 +110,7 @@ export const computeNodeAndLinks = <Id extends SankeyId>({
109
110
return data
110
111
}
111
112
112
- export const useSankey = < Id extends SankeyId > ( {
113
+ export const useSankey = < N extends DefaultNode , L extends DefaultLink > ( {
113
114
data,
114
115
valueFormat,
115
116
layout,
@@ -125,23 +126,23 @@ export const useSankey = <Id extends SankeyId>({
125
126
label,
126
127
labelTextColor,
127
128
} : {
128
- data : SankeyDataProps < Id > [ 'data' ]
129
- valueFormat ?: SankeyCommonProps < Id > [ 'valueFormat' ]
130
- layout : SankeyCommonProps < Id > [ 'layout' ]
129
+ data : SankeyDataProps < N , L > [ 'data' ]
130
+ valueFormat ?: SankeyCommonProps < N , L > [ 'valueFormat' ]
131
+ layout : SankeyCommonProps < N , L > [ 'layout' ]
131
132
width : number
132
133
height : number
133
- sort : SankeyCommonProps < Id > [ 'sort' ]
134
- align : SankeyCommonProps < Id > [ 'align' ]
135
- colors : SankeyCommonProps < Id > [ 'colors' ]
136
- nodeThickness : SankeyCommonProps < Id > [ 'nodeThickness' ]
137
- nodeSpacing : SankeyCommonProps < Id > [ 'nodeSpacing' ]
138
- nodeInnerPadding : SankeyCommonProps < Id > [ 'nodeInnerPadding' ]
139
- nodeBorderColor : SankeyCommonProps < Id > [ 'nodeBorderColor' ]
140
- label : SankeyCommonProps < Id > [ 'label' ]
141
- labelTextColor : SankeyCommonProps < Id > [ 'labelTextColor' ]
134
+ sort : SankeyCommonProps < N , L > [ 'sort' ]
135
+ align : SankeyCommonProps < N , L > [ 'align' ]
136
+ colors : SankeyCommonProps < N , L > [ 'colors' ]
137
+ nodeThickness : SankeyCommonProps < N , L > [ 'nodeThickness' ]
138
+ nodeSpacing : SankeyCommonProps < N , L > [ 'nodeSpacing' ]
139
+ nodeInnerPadding : SankeyCommonProps < N , L > [ 'nodeInnerPadding' ]
140
+ nodeBorderColor : SankeyCommonProps < N , L > [ 'nodeBorderColor' ]
141
+ label : SankeyCommonProps < N , L > [ 'label' ]
142
+ labelTextColor : SankeyCommonProps < N , L > [ 'labelTextColor' ]
142
143
} ) => {
143
- const [ currentNode , setCurrentNode ] = useState < SankeyNodeDatum < Id > | null > ( null )
144
- const [ currentLink , setCurrentLink ] = useState < SankeyLinkDatum < Id > | null > ( null )
144
+ const [ currentNode , setCurrentNode ] = useState < SankeyNodeDatum < N , L > | null > ( null )
145
+ const [ currentLink , setCurrentLink ] = useState < SankeyLinkDatum < N , L > | null > ( null )
145
146
146
147
const sortFunction = useMemo ( ( ) => {
147
148
if ( sort === 'auto' ) return undefined
@@ -163,16 +164,15 @@ export const useSankey = <Id extends SankeyId>({
163
164
const getColor = useOrdinalColorScale ( colors , 'id' )
164
165
const getNodeBorderColor = useInheritedColor ( nodeBorderColor , theme )
165
166
166
- const getLabel = usePropertyAccessor <
167
- Omit < SankeyNodeDatum < Id > , 'color' | 'label' > ,
168
- string | number
169
- > ( label )
167
+ const getLabel = usePropertyAccessor < Omit < SankeyNodeDatum < N , L > , 'color' | 'label' > , string > (
168
+ label
169
+ )
170
170
const getLabelTextColor = useInheritedColor ( labelTextColor , theme )
171
171
const formatValue = useValueFormatter < number > ( valueFormat )
172
172
173
173
const { nodes, links } = useMemo (
174
174
( ) =>
175
- computeNodeAndLinks < Id > ( {
175
+ computeNodeAndLinks < N , L > ( {
176
176
data,
177
177
formatValue,
178
178
layout,
0 commit comments