|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useEffect, useRef, useState } from "react"; |
| 5 | + |
| 6 | +import { useInternalI18n } from "@cloudscape-design/components/internal/do-not-use/i18n"; |
| 7 | + |
| 8 | +import { BaseI18nStrings, CoreChartProps } from "../../core/interfaces"; |
| 9 | +import { getChartLegendItemsFromSeriesOptions, isLegendItemsEqual } from "../../core/utils"; |
| 10 | +import { ChartLegend as ChartLegendComponent } from "../../internal/components/chart-legend"; |
| 11 | +import { ChartSeriesMarker, ChartSeriesMarkerType } from "../../internal/components/series-marker"; |
| 12 | +import { fireNonCancelableEvent } from "../../internal/events"; |
| 13 | +import { isEqualArrays } from "../../internal/utils/utils"; |
| 14 | + |
| 15 | +export interface StandaloneLegendAPI { |
| 16 | + highlightItems(itemIds: readonly string[]): void; |
| 17 | + setItemsVisible(itemIds: readonly string[]): void; |
| 18 | + clearHighlight(): void; |
| 19 | +} |
| 20 | + |
| 21 | +export function StandaloneLegend({ |
| 22 | + series, |
| 23 | + title, |
| 24 | + actions, |
| 25 | + position, |
| 26 | + i18nStrings, |
| 27 | + onItemHighlight, |
| 28 | + onClearHighlight, |
| 29 | + onVisibleItemsChange, |
| 30 | + getLegendTooltipContent, |
| 31 | + callback, |
| 32 | +}: { |
| 33 | + series: Highcharts.SeriesOptionsType[]; |
| 34 | + title?: string; |
| 35 | + actions?: React.ReactNode; |
| 36 | + position?: "bottom" | "side"; |
| 37 | + i18nStrings?: BaseI18nStrings; |
| 38 | + onClearHighlight?: CoreChartProps["onClearHighlight"]; |
| 39 | + onItemHighlight?: CoreChartProps["onLegendItemHighlight"]; |
| 40 | + onVisibleItemsChange?: CoreChartProps["onVisibleItemsChange"]; |
| 41 | + getLegendTooltipContent?: CoreChartProps["getLegendTooltipContent"]; |
| 42 | + callback?: (api: StandaloneLegendAPI) => void; |
| 43 | +}) { |
| 44 | + const i18n = useInternalI18n("[charts]"); |
| 45 | + const ariaLabel = i18n("i18nStrings.legendAriaLabel", i18nStrings?.legendAriaLabel); |
| 46 | + |
| 47 | + const markersCache = useRef(new Map<string, React.ReactNode>()).current; |
| 48 | + function renderMarker(type: ChartSeriesMarkerType, color: string, visible = true): React.ReactNode { |
| 49 | + const key = `${type}:${color}:${visible}`; |
| 50 | + const marker = markersCache.get(key) ?? <ChartSeriesMarker type={type} color={color} visible={visible} />; |
| 51 | + markersCache.set(key, marker); |
| 52 | + return marker; |
| 53 | + } |
| 54 | + |
| 55 | + const [items, setItems] = useState(() => { |
| 56 | + return getChartLegendItemsFromSeriesOptions(series).map(({ id, name, color, markerType, visible }) => { |
| 57 | + const marker = renderMarker(markerType, color, visible); |
| 58 | + return { id, name, marker, visible, highlighted: false }; |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + const internalApi = useRef({ |
| 63 | + clearHighlight(isApiCall: boolean) { |
| 64 | + setItems((prevItems) => { |
| 65 | + const hiddenSeries = prevItems.find((i) => !i.visible) !== undefined; |
| 66 | + const nextItems = prevItems.map(({ ...i }) => ({ ...i, highlighted: hiddenSeries ? i.visible : false })); |
| 67 | + if (!isEqualArrays(prevItems, nextItems, isLegendItemsEqual)) { |
| 68 | + fireNonCancelableEvent(onClearHighlight, { isApiCall }); |
| 69 | + return nextItems; |
| 70 | + } |
| 71 | + return prevItems; |
| 72 | + }); |
| 73 | + }, |
| 74 | + highlightItems(itemIds: readonly string[]) { |
| 75 | + setItems((prevItems) => { |
| 76 | + const nextItems = prevItems.map((i) => ({ |
| 77 | + ...i, |
| 78 | + highlighted: itemIds.includes(i.id), |
| 79 | + })); |
| 80 | + if (!isEqualArrays(prevItems, nextItems, isLegendItemsEqual)) { |
| 81 | + return nextItems; |
| 82 | + } |
| 83 | + return prevItems; |
| 84 | + }); |
| 85 | + }, |
| 86 | + setItemsVisible(itemIds: readonly string[], isApiCall: boolean) { |
| 87 | + setItems((prevItems) => { |
| 88 | + const nextItems = prevItems.map((i) => { |
| 89 | + const visible = itemIds.includes(i.id); |
| 90 | + return { ...i, visible, highlighted: visible }; |
| 91 | + }); |
| 92 | + if (!isEqualArrays(prevItems, nextItems, isLegendItemsEqual)) { |
| 93 | + fireNonCancelableEvent(onVisibleItemsChange, { items: nextItems, isApiCall }); |
| 94 | + return nextItems; |
| 95 | + } |
| 96 | + return prevItems; |
| 97 | + }); |
| 98 | + }, |
| 99 | + }).current; |
| 100 | + |
| 101 | + const api = useRef<StandaloneLegendAPI>({ |
| 102 | + clearHighlight() { |
| 103 | + internalApi.clearHighlight(true); |
| 104 | + }, |
| 105 | + highlightItems(itemIds: readonly string[]) { |
| 106 | + internalApi.highlightItems(itemIds); |
| 107 | + }, |
| 108 | + setItemsVisible(itemIds: readonly string[]) { |
| 109 | + internalApi.setItemsVisible(itemIds, true); |
| 110 | + }, |
| 111 | + }).current; |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + if (callback) { |
| 115 | + callback(api); |
| 116 | + } |
| 117 | + }, [callback, api]); |
| 118 | + |
| 119 | + if (items.length === 0) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + return ( |
| 123 | + <ChartLegendComponent |
| 124 | + items={items} |
| 125 | + ariaLabel={ariaLabel} |
| 126 | + actions={actions} |
| 127 | + legendTitle={title} |
| 128 | + position={position ?? "bottom"} |
| 129 | + getTooltipContent={(props) => getLegendTooltipContent?.(props) ?? null} |
| 130 | + onItemHighlightExit={() => internalApi.clearHighlight(false)} |
| 131 | + onItemVisibilityChange={(itemIds) => internalApi.setItemsVisible(itemIds, false)} |
| 132 | + onItemHighlightEnter={(item) => { |
| 133 | + internalApi.highlightItems([item.id]); |
| 134 | + fireNonCancelableEvent(onItemHighlight, { item }); |
| 135 | + }} |
| 136 | + /> |
| 137 | + ); |
| 138 | +} |
0 commit comments