|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useMemo, useState } from "react"; |
| 5 | + |
| 6 | +import Alert from "@cloudscape-design/components/alert"; |
| 7 | +import Badge from "@cloudscape-design/components/badge"; |
| 8 | +import Box from "@cloudscape-design/components/box"; |
| 9 | +import SpaceBetween from "@cloudscape-design/components/space-between"; |
| 10 | + |
| 11 | +import { CoreLegend } from "../../lib/components/internal-do-not-use/core-legend"; |
| 12 | +import { Page } from "../common/templates"; |
| 13 | + |
| 14 | +// Base legend items data |
| 15 | +const baseLegendItems = [ |
| 16 | + { |
| 17 | + id: "series-a", |
| 18 | + name: "Series A", |
| 19 | + marker: <div style={{ width: 12, height: 12, backgroundColor: "#1f77b4", borderRadius: 2 }} />, |
| 20 | + visible: true, |
| 21 | + }, |
| 22 | + { |
| 23 | + id: "series-b", |
| 24 | + name: "Series B", |
| 25 | + marker: <div style={{ width: 12, height: 12, backgroundColor: "#ff7f0e", borderRadius: 2 }} />, |
| 26 | + visible: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + id: "series-c", |
| 30 | + name: "Series C", |
| 31 | + marker: <div style={{ width: 12, height: 12, backgroundColor: "#2ca02c", borderRadius: 2 }} />, |
| 32 | + visible: true, |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +export default function LegendEventsDemo() { |
| 37 | + // State for tracking which item is highlighted in each legend |
| 38 | + const [highlightedItemWithExit, setHighlightedItemWithExit] = useState<string | null>(null); |
| 39 | + const [highlightedItemWithoutExit, setHighlightedItemWithoutExit] = useState<string | null>(null); |
| 40 | + |
| 41 | + // Create legend items with dynamic highlighted state for the legend WITH exit handler |
| 42 | + const legendItemsWithExit = useMemo( |
| 43 | + () => |
| 44 | + baseLegendItems.map((item) => ({ |
| 45 | + ...item, |
| 46 | + highlighted: item.name === highlightedItemWithExit, |
| 47 | + })), |
| 48 | + [highlightedItemWithExit], |
| 49 | + ); |
| 50 | + |
| 51 | + // Create legend items with dynamic highlighted state for the legend WITHOUT exit handler |
| 52 | + const legendItemsWithoutExit = useMemo( |
| 53 | + () => |
| 54 | + baseLegendItems.map((item) => ({ |
| 55 | + ...item, |
| 56 | + highlighted: item.name === highlightedItemWithoutExit, |
| 57 | + })), |
| 58 | + [highlightedItemWithoutExit], |
| 59 | + ); |
| 60 | + |
| 61 | + // Handler for legend WITH exit handler - controls both legends |
| 62 | + const handleLegendItemHighlightWithExit = (event: { detail: { item: { name: string } } }) => { |
| 63 | + const itemName = event.detail.item.name; |
| 64 | + setHighlightedItemWithExit(itemName); |
| 65 | + // Cross-control: also highlight the corresponding item in the other legend |
| 66 | + setHighlightedItemWithoutExit(itemName); |
| 67 | + }; |
| 68 | + |
| 69 | + // Handler for exit event - only clears the legend WITH exit handler |
| 70 | + const handleLegendItemHighlightExitWithExit = () => { |
| 71 | + setHighlightedItemWithExit(null); |
| 72 | + // Cross-control: also clear the other legend (this shows proper bidirectional control) |
| 73 | + setHighlightedItemWithoutExit(null); |
| 74 | + }; |
| 75 | + |
| 76 | + // Handler for legend WITHOUT exit handler - controls both legends |
| 77 | + const handleLegendItemHighlightWithoutExit = (event: { detail: { item: { name: string } } }) => { |
| 78 | + const itemName = event.detail.item.name; |
| 79 | + setHighlightedItemWithoutExit(itemName); |
| 80 | + // Cross-control: also highlight the corresponding item in the other legend |
| 81 | + setHighlightedItemWithExit(itemName); |
| 82 | + }; |
| 83 | + |
| 84 | + // Note: No exit handler for the second legend - this demonstrates the issue |
| 85 | + |
| 86 | + return ( |
| 87 | + <Page |
| 88 | + title="Legend Events" |
| 89 | + subtitle="Demonstrates the difference between having onLegendItemHighlightExit and not having it" |
| 90 | + > |
| 91 | + <SpaceBetween direction="vertical" size="l"> |
| 92 | + {/* Legend WITH exit handler */} |
| 93 | + <Box> |
| 94 | + <SpaceBetween direction="vertical" size="m"> |
| 95 | + <SpaceBetween direction="horizontal" size="s" alignItems="center"> |
| 96 | + <Box variant="h2">Legend WITH onClearHighlight (Controls Both)</Box> |
| 97 | + <Badge color={highlightedItemWithExit ? "blue" : "grey"}> |
| 98 | + {highlightedItemWithExit ? `Controlling: ${highlightedItemWithExit}` : "Not Controlling"} |
| 99 | + </Badge> |
| 100 | + </SpaceBetween> |
| 101 | + |
| 102 | + <CoreLegend |
| 103 | + items={legendItemsWithExit} |
| 104 | + title="Master Legend (Has Exit Handler)" |
| 105 | + ariaLabel="Legend with exit handler that controls both legends" |
| 106 | + onItemHighlight={handleLegendItemHighlightWithExit} |
| 107 | + onClearHighlight={handleLegendItemHighlightExitWithExit} |
| 108 | + /> |
| 109 | + </SpaceBetween> |
| 110 | + </Box> |
| 111 | + |
| 112 | + {/* Legend WITHOUT exit handler */} |
| 113 | + <Box> |
| 114 | + <SpaceBetween direction="vertical" size="m"> |
| 115 | + <SpaceBetween direction="horizontal" size="s" alignItems="center"> |
| 116 | + <Box variant="h2">Legend WITHOUT onClearHighlight (Controls Both)</Box> |
| 117 | + <Badge color={highlightedItemWithoutExit ? "red" : "grey"}> |
| 118 | + {highlightedItemWithoutExit ? `Stuck Controlling: ${highlightedItemWithoutExit}` : "Not Controlling"} |
| 119 | + </Badge> |
| 120 | + </SpaceBetween> |
| 121 | + |
| 122 | + <CoreLegend |
| 123 | + items={legendItemsWithoutExit} |
| 124 | + title="Broken Legend (No Exit Handler)" |
| 125 | + ariaLabel="Legend without exit handler that controls both legends" |
| 126 | + onItemHighlight={handleLegendItemHighlightWithoutExit} |
| 127 | + // Note: No onClearHighlight prop - this demonstrates the issue |
| 128 | + /> |
| 129 | + </SpaceBetween> |
| 130 | + </Box> |
| 131 | + |
| 132 | + <Alert type="success"> |
| 133 | + <strong>Expected Behavior:</strong> |
| 134 | + <br />• <strong>Top Legend (With Exit Handler):</strong> When you hover and then move away, both legends clear |
| 135 | + their highlights properly - demonstrating proper bidirectional control. |
| 136 | + <br />• <strong>Bottom Legend (Without Exit Handler):</strong> When you hover and then move away, both legends |
| 137 | + remain stuck in the highlighted state - demonstrating the broken one-way control that the exit handler fixes. |
| 138 | + <br /> |
| 139 | + <br /> |
| 140 | + This shows how the exit handler is essential for proper cross-component communication and state management. |
| 141 | + </Alert> |
| 142 | + </SpaceBetween> |
| 143 | + </Page> |
| 144 | + ); |
| 145 | +} |
0 commit comments