diff --git a/api/charts.api.md b/api/charts.api.md index d9ec00faf2..0e2ab07c47 100644 --- a/api/charts.api.md +++ b/api/charts.api.md @@ -1048,7 +1048,7 @@ export type LegendColorPicker = ComponentType; export interface LegendColorPickerProps { anchor: HTMLElement; color: Color; - onChange: (color: Color) => void; + onChange: (color: Color | null) => void; onClose: () => void; seriesIdentifier: SeriesIdentifier; } diff --git a/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png b/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png index c82f0d782b..13941a8a6d 100644 Binary files a/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png and b/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png differ diff --git a/src/chart_types/xy_chart/utils/series.ts b/src/chart_types/xy_chart/utils/series.ts index 67bc014396..aa96e36b48 100644 --- a/src/chart_types/xy_chart/utils/series.ts +++ b/src/chart_types/xy_chart/utils/series.ts @@ -694,16 +694,21 @@ function getHighestOverride( customColors: Map, overrides: ColorOverrides, ): Color | undefined { - let color: Color | undefined = overrides.temporary[key]; + const tempColor: Color | undefined | null = overrides.temporary[key]; - if (color) { - return color; + if (tempColor) { + return tempColor; } - color = customColors.get(key); + const customColor: Color | undefined | null = customColors.get(key); - if (color) { - return color; + if (customColor) { + return customColor; + } + + if (tempColor === null) { + // Use default color when temporary and custom colors are null + return; } return overrides.persisted[key]; diff --git a/src/components/legend/legend_item.tsx b/src/components/legend/legend_item.tsx index c296b6056b..0219d0d399 100644 --- a/src/components/legend/legend_item.tsx +++ b/src/components/legend/legend_item.tsx @@ -110,6 +110,7 @@ interface LegendItemState { /** @internal */ export class LegendListItem extends Component { static displayName = 'LegendItem'; + shouldClearPersistedColor = false; colorRef = createRef(); state: LegendItemState = { @@ -183,17 +184,21 @@ export class LegendListItem extends Component const { seriesIdentifier, color } = item; const handleClose = () => { - setPersistedColorAction(seriesIdentifier.key, color); + setPersistedColorAction(seriesIdentifier.key, this.shouldClearPersistedColor ? null : color); clearTemporaryColorsAction(); this.toggleIsOpen(); }; + const handleChange = (c: Color | null) => { + this.shouldClearPersistedColor = c === null; + setTemporaryColorAction(seriesIdentifier.key, c); + }; if (ColorPicker && this.state.isOpen && this.colorRef.current) { return ( setTemporaryColorAction(seriesIdentifier.key, color)} + onChange={handleChange} seriesIdentifier={seriesIdentifier} /> ); diff --git a/src/specs/settings.tsx b/src/specs/settings.tsx index 3adfd2b4a2..62c85f1a48 100644 --- a/src/specs/settings.tsx +++ b/src/specs/settings.tsx @@ -248,7 +248,7 @@ export interface LegendColorPickerProps { /** * Callback to update temporary color state */ - onChange: (color: Color) => void; + onChange: (color: Color | null) => void; /** * Series id for the active series */ diff --git a/src/state/actions/colors.ts b/src/state/actions/colors.ts index 07be8d29cd..4a60e7361a 100644 --- a/src/state/actions/colors.ts +++ b/src/state/actions/colors.ts @@ -36,13 +36,13 @@ interface ClearTemporaryColors { interface SetTemporaryColor { type: typeof SET_TEMPORARY_COLOR; key: SeriesKey; - color: Color; + color: Color | null; } interface SetPersistedColor { type: typeof SET_PERSISTED_COLOR; key: SeriesKey; - color: Color; + color: Color | null; } /** @internal */ @@ -51,12 +51,12 @@ export function clearTemporaryColors(): ClearTemporaryColors { } /** @internal */ -export function setTemporaryColor(key: SeriesKey, color: Color): SetTemporaryColor { +export function setTemporaryColor(key: SeriesKey, color: Color | null): SetTemporaryColor { return { type: SET_TEMPORARY_COLOR, key, color }; } /** @internal */ -export function setPersistedColor(key: SeriesKey, color: Color): SetPersistedColor { +export function setPersistedColor(key: SeriesKey, color: Color | null): SetPersistedColor { return { type: SET_PERSISTED_COLOR, key, color }; } diff --git a/src/state/chart_state.ts b/src/state/chart_state.ts index 2a2cdda8c8..f115e4082c 100644 --- a/src/state/chart_state.ts +++ b/src/state/chart_state.ts @@ -191,7 +191,7 @@ export interface ExternalEventsState { /** @internal */ export interface ColorOverrides { - temporary: Record; + temporary: Record; persisted: Record; } @@ -399,10 +399,16 @@ export const chartStoreReducer = (chartId: string) => { ...state, colors: { ...state.colors, - persisted: { - ...state.colors.persisted, - [action.key]: action.color, - }, + persisted: + action.color !== null + ? { + ...state.colors.persisted, + [action.key]: action.color, + } + : (() => { + const { [action.key]: removed, ...others } = state.colors.persisted; + return others; + })(), }, }; default: diff --git a/stories/legend/9_color_picker.tsx b/stories/legend/9_color_picker.tsx index dd8edcbe82..aa350d6a63 100644 --- a/stories/legend/9_color_picker.tsx +++ b/stories/legend/9_color_picker.tsx @@ -17,7 +17,7 @@ * under the License. */ -import { EuiColorPicker, EuiWrappingPopover, EuiButton, EuiSpacer } from '@elastic/eui'; +import { EuiColorPicker, EuiWrappingPopover, EuiButton, EuiSpacer, EuiFlexItem, EuiButtonEmpty } from '@elastic/eui'; import { action } from '@storybook/addon-actions'; import React, { useState } from 'react'; @@ -41,14 +41,19 @@ export const Example = () => { [seriesIdentifier.key]: color, }); }; - const handleChange = (color: Color) => { - onChange(color); - onChangeAction(color); + const handleChange = (c: Color | null) => { + onChange(c); + onChangeAction(c); }; return ( + + handleChange(null)}> + Clear color + + Done