Skip to content
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
40 changes: 35 additions & 5 deletions src/ui/legend/category.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '../../core';
import type { Group } from '../../shapes';
import { HTML, type Group } from '../../shapes';
import { BBox, select, Selection, splitStyle, subStyleProps } from '../../util';
import type { TitleStyleProps } from './title';
import { getBBox, Title } from './title';
Expand All @@ -9,6 +9,12 @@ import type { CategoryOptions, CategoryStyleProps } from './types';

export type { CategoryOptions, CategoryStyleProps };

class HtmlLegend extends HTML {
update(options: any) {
this.attr(options);
}
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The options parameter in the update method is typed as any. For better type safety and code clarity, it's recommended to use a more specific type. You can use HTMLStyleProps which is available for HTML elements.

  update(options: Partial<import('../../shapes').HTMLStyleProps>) {
    this.attr(options);
  }

}

export class Category extends Component<CategoryStyleProps> {
constructor(options: CategoryOptions) {
super(options, CATEGORY_DEFAULT_OPTIONS);
Expand All @@ -35,6 +41,26 @@ export class Category extends Component<CategoryStyleProps> {
.update(finalTitleStyle) as Selection<Title>;
}

private renderCustom(container: Selection) {
const { data } = this.attributes;

const style = {
innerHTML: this.attributes.render(data),
pointerEvents: 'auto' as const,
};

container
.maybeAppendByClassName(
CLASS_NAMES.html,
() =>
new HtmlLegend({
className: CLASS_NAMES.html.name,
style,
})
)
.update(style);
}

private renderItems(container: Selection, bbox: DOMRect) {
const { x, y, width, height } = bbox;
const style = subStyleProps(this.attributes, 'title', true);
Expand Down Expand Up @@ -84,7 +110,7 @@ export class Category extends Component<CategoryStyleProps> {
}

render(attributes: Required<CategoryStyleProps>, container: Group) {
const { width, height, x = 0, y = 0, classNamePrefix } = this.attributes;
const { width, height, x = 0, y = 0, classNamePrefix, render } = this.attributes;
const ctn = select(container);

// Set root container className
Expand All @@ -97,10 +123,14 @@ export class Category extends Component<CategoryStyleProps> {

container.style.transform = `translate(${x}, ${y})`;

this.renderTitle(ctn, width!, height!);
if (render as CategoryStyleProps['render']) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type assertion in the if condition is unnecessary for a truthiness check and makes the code more verbose. You can simplify it by just checking for render.

Suggested change
if (render as CategoryStyleProps['render']) {
if (render) {

this.renderCustom(ctn);
} else {
this.renderTitle(ctn, width!, height!);

this.renderItems(ctn, this.availableSpace);
this.renderItems(ctn, this.availableSpace);

this.adjustLayout();
this.adjustLayout();
}
}
}
1 change: 1 addition & 0 deletions src/ui/legend/category/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type CategoryItemsStyleProps = GroupStyleProps &
focus?: boolean;
focusMarkerSize?: number;
classNamePrefix?: string;
render?: (data: CategoryItemsDatum[]) => string | HTMLElement;
};

export type CategoryItemsOptions = ComponentOptions<CategoryItemsStyleProps>;
Expand Down
1 change: 1 addition & 0 deletions src/ui/legend/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const NAME_VALUE_RATIO = 0.5;
export const CLASS_NAMES = classNames(
{
title: 'title',
html: 'html',
titleGroup: 'title-group',
items: 'items',
itemsGroup: 'items-group',
Expand Down
Loading