|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + GoabButton, |
| 4 | + GoabDropdown, |
| 5 | + GoabDropdownItem, |
| 6 | + GoabText, |
| 7 | +} from "@abgov/react-components"; |
| 8 | + |
| 9 | +export function Bug2333Route() { |
| 10 | + const [colors, setColors] = useState<string[]>(["red", "blue", "green", "yellow", "purple"]); |
| 11 | + const [selectedColor, setSelectedColor] = useState<string>(""); |
| 12 | + |
| 13 | + const reduceToOne = () => { |
| 14 | + setColors(["blue"]); |
| 15 | + }; |
| 16 | + |
| 17 | + const reduceToTwo = () => { |
| 18 | + setColors(["green", "yellow"]); |
| 19 | + }; |
| 20 | + |
| 21 | + const resetToAll = () => { |
| 22 | + setColors(["red", "blue", "green", "yellow", "purple"]); |
| 23 | + }; |
| 24 | + |
| 25 | + const onChange = (name: string, value: string | string[]) => { |
| 26 | + console.log("Dropdown changed:", name, value); |
| 27 | + setSelectedColor(Array.isArray(value) ? value[0] : value); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div style={{ width: "1024px", margin: "0 auto", padding: "2rem" }}> |
| 32 | + <GoabText size="heading-l" mb="xl"> |
| 33 | + Bug #2333: Dropdown Reset Test |
| 34 | + </GoabText> |
| 35 | + |
| 36 | + <GoabText size="body-m" mb="2xl"> |
| 37 | + This test demonstrates the dropdown reset issue. When dropdown items are dynamically |
| 38 | + removed, the dropdown should properly sync its internal state to reflect the updated |
| 39 | + list of options. |
| 40 | + </GoabText> |
| 41 | + |
| 42 | + <GoabText size="heading-m" mb="l"> |
| 43 | + Test Scenario |
| 44 | + </GoabText> |
| 45 | + |
| 46 | + <GoabText size="body-s" mb="m"> |
| 47 | + 1. Select a color from the dropdown below |
| 48 | + </GoabText> |
| 49 | + <GoabText size="body-s" mb="m"> |
| 50 | + 2. Click one of the buttons to reduce the number of available options |
| 51 | + </GoabText> |
| 52 | + <GoabText size="body-s" mb="m"> |
| 53 | + 3. Open the dropdown again - it should only show the remaining options |
| 54 | + </GoabText> |
| 55 | + <GoabText size="body-s" mb="2xl"> |
| 56 | + 4. The bug occurred when the filtered options weren't synced after items were destroyed |
| 57 | + </GoabText> |
| 58 | + |
| 59 | + <GoabText size="body-m" mb="m"> |
| 60 | + Currently showing {colors.length} color(s): {colors.join(", ")} |
| 61 | + </GoabText> |
| 62 | + |
| 63 | + <GoabText size="body-m" mb="m"> |
| 64 | + Selected value: {selectedColor || "None"} |
| 65 | + </GoabText> |
| 66 | + |
| 67 | + <GoabDropdown |
| 68 | + name="favcolor" |
| 69 | + placeholder="Select a color" |
| 70 | + value={selectedColor} |
| 71 | + onChange={onChange} |
| 72 | + mb="xl" |
| 73 | + > |
| 74 | + {colors.map((color) => ( |
| 75 | + <GoabDropdownItem key={color} label={color} value={color} /> |
| 76 | + ))} |
| 77 | + </GoabDropdown> |
| 78 | + |
| 79 | + <div style={{ display: "flex", gap: "1rem", marginBottom: "1rem" }}> |
| 80 | + <GoabButton type="secondary" onClick={reduceToOne}> |
| 81 | + Reduce to 1 item (blue) |
| 82 | + </GoabButton> |
| 83 | + <GoabButton type="secondary" onClick={reduceToTwo}> |
| 84 | + Reduce to 2 items (green, yellow) |
| 85 | + </GoabButton> |
| 86 | + <GoabButton type="primary" onClick={resetToAll}> |
| 87 | + Reset to all items |
| 88 | + </GoabButton> |
| 89 | + </div> |
| 90 | + |
| 91 | + <GoabText size="body-s" mb="m" mt="2xl"> |
| 92 | + <strong>Expected behavior:</strong> After clicking a reduction button, opening the |
| 93 | + dropdown should only display the items that remain in the list. |
| 94 | + </GoabText> |
| 95 | + <GoabText size="body-s"> |
| 96 | + <strong>Bug behavior (before fix):</strong> The dropdown would still show all original |
| 97 | + items even after they were removed, because syncFilteredOptions() wasn't called when |
| 98 | + child items were destroyed. |
| 99 | + </GoabText> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
0 commit comments