diff --git a/packages/osd-charts/api/charts.api.md b/packages/osd-charts/api/charts.api.md index d9ec00faf2bc..0e2ab07c47ca 100644 --- a/packages/osd-charts/api/charts.api.md +++ b/packages/osd-charts/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/packages/osd-charts/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png b/packages/osd-charts/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png index c82f0d782be7..13941a8a6dfd 100644 Binary files a/packages/osd-charts/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png and b/packages/osd-charts/integration/tests/__image_snapshots__/legend-stories-test-ts-legend-stories-should-render-color-picker-on-mouse-click-1-snap.png differ diff --git a/packages/osd-charts/src/chart_types/xy_chart/utils/series.ts b/packages/osd-charts/src/chart_types/xy_chart/utils/series.ts index 67bc014396ee..aa96e36b48b7 100644 --- a/packages/osd-charts/src/chart_types/xy_chart/utils/series.ts +++ b/packages/osd-charts/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/packages/osd-charts/src/components/legend/legend_item.tsx b/packages/osd-charts/src/components/legend/legend_item.tsx index c296b6056b77..0219d0d39963 100644 --- a/packages/osd-charts/src/components/legend/legend_item.tsx +++ b/packages/osd-charts/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/packages/osd-charts/src/specs/settings.tsx b/packages/osd-charts/src/specs/settings.tsx index 3adfd2b4a2ef..62c85f1a48d9 100644 --- a/packages/osd-charts/src/specs/settings.tsx +++ b/packages/osd-charts/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/packages/osd-charts/src/state/actions/colors.ts b/packages/osd-charts/src/state/actions/colors.ts index 07be8d29cda8..4a60e7361a09 100644 --- a/packages/osd-charts/src/state/actions/colors.ts +++ b/packages/osd-charts/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/packages/osd-charts/src/state/chart_state.ts b/packages/osd-charts/src/state/chart_state.ts index 2a2cdda8c87f..f115e4082cc5 100644 --- a/packages/osd-charts/src/state/chart_state.ts +++ b/packages/osd-charts/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/packages/osd-charts/stories/legend/9_color_picker.tsx b/packages/osd-charts/stories/legend/9_color_picker.tsx index dd8edcbe82bd..aa350d6a6352 100644 --- a/packages/osd-charts/stories/legend/9_color_picker.tsx +++ b/packages/osd-charts/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