Skip to content

Commit c57ce48

Browse files
huaxiabuluoian.lxl
andauthored
chore: add selected in treemap record item (#2084)
Co-authored-by: ian.lxl <ian.lxl@antgroup.com>
1 parent daddc7f commit c57ce48

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

packages/f2/src/components/treemap/withTreemap.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,14 @@ const withTreemap = <IProps extends TreemapProps = TreemapProps>(View: Component
8989
return false;
9090
}
9191

92-
getSelectionStyle(record) {
93-
const { state, props } = this;
94-
const { selected } = state;
95-
if (!selected || !selected.length) {
92+
generateSelectionStyle(record: Record<string, unknown>, isSelected: boolean) {
93+
if (!this.state.selected?.length) {
9694
return null;
9795
}
98-
const { selection } = props;
99-
const { selectedStyle, unSelectedStyle } = selection;
10096

101-
const isSelected = this.isSelected(record);
102-
103-
if (isSelected) {
104-
return isFunction(selectedStyle) ? selectedStyle(record) : selectedStyle;
105-
}
106-
return isFunction(unSelectedStyle) ? unSelectedStyle(record) : unSelectedStyle;
97+
const { selectedStyle, unSelectedStyle } = this.props.selection || {};
98+
const style = isSelected ? selectedStyle : unSelectedStyle;
99+
return isFunction(style) ? style(record) : style;
107100
}
108101

109102
willMount() {
@@ -162,14 +155,16 @@ const withTreemap = <IProps extends TreemapProps = TreemapProps>(View: Component
162155
yMax: y1,
163156
};
164157

165-
const style = this.getSelectionStyle(data);
158+
const selected = this.isSelected(data);
159+
const style = this.generateSelectionStyle(data, selected);
166160

167161
return {
168162
key: data.key,
169163
origin: data,
170164
color,
171165
...rect,
172166
style,
167+
selected,
173168
};
174169
});
175170
}

packages/f2/test/components/treemap/index.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ describe('Treemap', () => {
112112
canvas.destroy();
113113
});
114114
it('select ', async () => {
115+
const treemapRef = { current: null };
115116
const { props } = (
116117
<Canvas context={context} pixelRatio={1}>
117118
<Treemap
119+
ref={treemapRef}
118120
data={data}
119121
color={{
120122
field: 'name',
@@ -147,10 +149,12 @@ describe('Treemap', () => {
147149

148150
await delay(1000);
149151
expect(context).toMatchImageSnapshot();
152+
expect(treemapRef.current?.records[0]?.selected).toBe(true);
150153

151154
const { props: nextProps } = (
152155
<Canvas context={context} pixelRatio={1}>
153156
<Treemap
157+
ref={treemapRef}
154158
data={data}
155159
color={{
156160
field: 'name',
@@ -181,6 +185,7 @@ describe('Treemap', () => {
181185
canvas.update(nextProps);
182186
await delay(1000);
183187
expect(context).toMatchImageSnapshot();
188+
expect(treemapRef.current?.records[2]?.selected).toBe(true);
184189
await delay(100);
185190
canvas.destroy();
186191
});

0 commit comments

Comments
 (0)