Skip to content

Commit 25a0689

Browse files
authored
fix: poptip render (#360)
1 parent 3991a66 commit 25a0689

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/ui/legend/category/item.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ function styleOfMarker(group: Group): BaseStyleProps {
103103
}
104104

105105
export class CategoryItem extends Component<CategoryItemStyleProps> {
106-
constructor(options: CategoryItemOptions) {
106+
// Key fields, used to pass extra info like id, index for poptip rendering
107+
public keyFields: object = {};
108+
109+
constructor(options: CategoryItemOptions, keyFields?: Omit<CategoryItemOptions, 'style'>) {
107110
super(options, {
108111
span: [1, 1],
109112
marker: () => new Circle({ style: { r: 6 } }),
@@ -115,6 +118,7 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
115118
labelTextBaseline: 'middle',
116119
valueTextBaseline: 'middle',
117120
});
121+
this.keyFields = keyFields || {};
118122
}
119123

120124
private poptipGroup!: Poptip;
@@ -313,7 +317,7 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
313317
const label = typeof labelText === 'string' ? labelText : (labelText as DisplayObject)?.attr('text');
314318
const value = typeof valueText === 'string' ? valueText : (valueText as DisplayObject)?.attr('text');
315319
if (typeof poptip.render === 'function') {
316-
return { html: poptip.render({ label, value, color: markerFill as string }) };
320+
return { html: poptip.render({ ...this.keyFields, label, value, color: markerFill as string }) };
317321
}
318322
let html = '';
319323
if (typeof label === 'string' || typeof label === 'number') {

src/ui/legend/category/items.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ type CallableItemStyle = CallableStyleProps<
3434
>;
3535

3636
export type PoptipRender = {
37-
render?: (params: { label: string | number; value: string | number; color?: string }) => string;
37+
render?: (params: {
38+
label: string | number;
39+
value: string | number;
40+
color?: string;
41+
[key: string]: unknown;
42+
}) => string;
3843
};
3944

4045
export type CategoryItemsStyleProps = GroupStyleProps &
@@ -264,7 +269,7 @@ export class CategoryItems extends Component<CategoryItemsStyleProps> {
264269
.join(
265270
(enter) =>
266271
enter
267-
.append(({ style }) => new CategoryItem({ style }))
272+
.append(({ style, ...rest }) => new CategoryItem({ style }, rest))
268273
.attr('className', itemClassName)
269274
.on('click', function () {
270275
click?.(this);

0 commit comments

Comments
 (0)