Skip to content

Commit 692eb20

Browse files
hokolomoporrahir
authored andcommitted
[FIX] selection input: highlight color not sync with range color
After re-ordering the selection inputs of a chart, the highlight colors would not be the same as the range colors. This was because the store mutator `updateColors` would not update the store range colors. And `store.highlights` is based on `ranges.colors` while `store.selectionInputs` ignores the colors of `store.ranges`. The test `update of colors are taken into account` was also a bit sketchy. We would modify the `props.colors` array in place, thus the condition `nextProps.colors !== props.colors` would never be satisfied, and `store.updateColors` would never be called. Task: 4577803
1 parent 0ffa187 commit 692eb20

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/components/selection_input/selection_input_store.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class SelectionInputStore extends SpreadsheetStore {
3131
"confirm",
3232
"updateColors",
3333
] as const;
34-
ranges: RangeInputValue[] = [];
34+
private ranges: RangeInputValue[] = [];
3535
focusedRangeIndex: number | null = null;
3636
private inputSheetId: UID;
3737
private focusStore = this.get(FocusStore);
@@ -166,6 +166,11 @@ export class SelectionInputStore extends SpreadsheetStore {
166166

167167
updateColors(colors: Color[]) {
168168
this.colors = colors;
169+
const colorGenerator = new ColorGenerator(this.ranges.length, this.colors);
170+
this.ranges = this.ranges.map((range) => ({
171+
...range,
172+
color: colorGenerator.next(),
173+
}));
169174
}
170175

171176
confirm() {
@@ -206,14 +211,13 @@ export class SelectionInputStore extends SpreadsheetStore {
206211
* e.g. ["A1", "Sheet2!B3", "E12"]
207212
*/
208213
get selectionInputs(): (RangeInputValue & { isFocused: boolean; isValidRange: boolean })[] {
209-
const generator = new ColorGenerator(this.ranges.length, this.colors);
210214
return this.ranges.map((input, index) =>
211215
Object.assign({}, input, {
212216
color:
213217
this.hasMainFocus &&
214218
this.focusedRangeIndex !== null &&
215219
this.getters.isRangeValid(input.xc)
216-
? generator.next()
220+
? input.color
217221
: null,
218222
isFocused: this.hasMainFocus && this.focusedRangeIndex === index,
219223
isValidRange: input.xc === "" || this.getters.isRangeValid(input.xc),

tests/selection_input/selection_input_component.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SelectionInput } from "../../src/components/selection_input/selection_i
55
import { ColorGenerator, toCartesian, toZone } from "../../src/helpers";
66
import { useStoreProvider } from "../../src/store_engine";
77
import { ModelStore } from "../../src/stores";
8+
import { HighlightStore } from "../../src/stores/highlight_store";
89
import { Color, SpreadsheetChildEnv } from "../../src/types";
910
import {
1011
activateSheet,
@@ -234,20 +235,26 @@ describe("Selection Input", () => {
234235
// data series of a chart, as the color of the removed range won't be passed anymore to
235236
// the colors function in the chart's side panel's props.
236237
let colors = ["#FF0000", "#00FF00"];
237-
await createSelectionInput({
238+
const { parent, env } = await createSelectionInput({
238239
initialRanges: ["A1", "B1"],
239240
colors: colors,
240241
});
241-
simulateClick(fixture.querySelectorAll("input")[0]);
242-
await nextTick();
242+
const highlightStore = env.getStore(HighlightStore);
243+
await simulateClick(fixture.querySelectorAll("input")[0]);
243244
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("color:#FF0000; ");
244245
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe("color:#00FF00; ");
245-
colors[0] = "#0000FF";
246-
colors[1] = "#FF00FF";
247-
simulateClick(fixture.querySelectorAll("input")[1]);
248-
await nextTick();
246+
expect(highlightStore.highlights).toMatchObject([
247+
{ color: "#FF0000", zone: toZone("A1") },
248+
{ color: "#00FF00", zone: toZone("B1") },
249+
]);
250+
parent.colors = ["#0000FF", "#FF00FF"];
251+
await simulateClick(fixture.querySelectorAll("input")[1]);
249252
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("color:#0000FF; ");
250253
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe("color:#FF00FF; ");
254+
expect(highlightStore.highlights).toMatchObject([
255+
{ color: "#0000FF", zone: toZone("A1") },
256+
{ color: "#FF00FF", zone: toZone("B1") },
257+
]);
251258
});
252259

253260
test("can select full column as unbounded zone by clicking on header", async () => {

0 commit comments

Comments
 (0)