Skip to content

Commit

Permalink
fix: 默认选中变化时不更新 (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Mar 23, 2022
1 parent c7e5705 commit a6e1276
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/f2/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Geometry<
}

willReceiveProps(nextProps) {
super.willReceiveProps(nextProps);
const { props: lastProps, attrController } = this;
const { data: nextData, adjust: nextAdjust, zoomRange: nextZoomRange } = nextProps;
const { data: lastData, adjust: lastAdjust, zoomRange: lastZoomRange } = lastProps;
Expand Down
14 changes: 14 additions & 0 deletions packages/f2/src/components/geometry/selection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isFunction } from '@antv/util';
import Component from '../../base/component';
import { ShapeAttrs, Point } from '../../types';
import equal from '../../base/equal';

function isEqual(origin1, origin2, fields: string[]) {
if (origin1 === origin2) {
Expand Down Expand Up @@ -117,6 +118,19 @@ class Selection<
});
}

willReceiveProps(nextProps: P): void {
const { selection: nextSelection } = nextProps;
const { selection: lastSelection } = this.props;
if (!nextSelection || !lastSelection) {
return;
}
const { defaultSelected: nextDefaultSelected } = nextSelection;
const { defaultSelected: lastDefaultSelected } = lastSelection;
if (!equal(nextDefaultSelected, lastDefaultSelected)) {
this.state.selected = nextDefaultSelected;
}
}

getSnapRecords(_point: Point) {
return null;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions packages/f2/test/components/interval/selected.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,55 @@ describe('cancelable = false', () => {
expect(context).toMatchImageSnapshot();
});
});

describe('改变默认值', () => {
it('改变默认值', async () => {
const context = createContext();

const getProps = (data, defaultSelected) => {
const { props } = (
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart
data={data}
coord={{
radius: 0.8,
type: 'polar',
transposed: true,
}}
>
<Interval
x="a"
y="sold"
adjust="stack"
color="genre"
selection={{
defaultSelected,
selectedStyle: (record) => {
const { yMax } = record;
return {
r: yMax * 1.1,
};
},
cancelable: false,
}}
/>
</Chart>
</Canvas>
);
return props;
};

const props = getProps(data, [{ a: '1', genre: 'Sports', sold: 275 }]);
const canvas = new Canvas(props);
canvas.render();

await delay(200);
expect(context).toMatchImageSnapshot();

const updateProps = getProps([].concat(data), [{ a: '1', genre: 'Strategy', sold: 115 }]);
canvas.update(updateProps);

await delay(200);
expect(context).toMatchImageSnapshot();
});
});

0 comments on commit a6e1276

Please sign in to comment.