Skip to content

Commit 900b0be

Browse files
feat(ui): fix state bug in ToggleButton (#1180)
* add back useEffect fixing bug * add back useEffect fixing bug * add changeset * add test * mock handlers * remove mock handlers
1 parent 3ac7d06 commit 900b0be

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

.changeset/green-trees-cut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudoperators/juno-ui-components": minor
3+
---
4+
5+
Added `ToggleButton` and `SortButton` components.

packages/ui-components/src/components/ToggleButton/ToggleButton.component.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import React, { useState, ReactNode } from "react"
6+
import React, { useState, ReactNode, useEffect } from "react"
77
import { Button, ButtonProps } from "../Button"
88

99
type Option<T> = T | { value: T; label?: ReactNode }
@@ -24,8 +24,13 @@ export const ToggleButton = <T extends string | number>({
2424
}: ToggleButtonProps<T>) => {
2525
const extractValue = (option: Option<T>): T => (typeof option === "object" ? option.value : option)
2626

27-
const initialValue = value !== undefined ? value : extractValue(options[0])
28-
const [currentValue, setCurrentValue] = useState<T>(initialValue)
27+
const [currentValue, setCurrentValue] = useState<T>(value ?? extractValue(options[0]))
28+
29+
useEffect(() => {
30+
if (value !== undefined) {
31+
setCurrentValue(value)
32+
}
33+
}, [value])
2934

3035
const handleButtonClick = () => {
3136
const currentIndex = options.findIndex((opt) => extractValue(opt) === currentValue)

packages/ui-components/src/components/ToggleButton/ToggleButton.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import React from "react"
77
import type { Meta, StoryObj } from "@storybook/react-vite"
8-
98
import { ToggleButton } from "./ToggleButton.component"
109
import { Icon } from "../Icon"
1110
import { KnownIcons } from "../Icon/Icon.component"

packages/ui-components/src/components/ToggleButton/ToggleButton.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,19 @@ describe("ToggleButton Component", () => {
123123
expect(button).toHaveTextContent("option1")
124124
expect(handleChange).toHaveBeenCalledWith("option1")
125125
})
126+
127+
it("updates display when value prop is changed", async () => {
128+
const options = ["option1", "option2"]
129+
const { rerender } = render(<ToggleButton options={options} value="option1" />)
130+
131+
const button = screen.getByRole("button")
132+
expect(button).toHaveTextContent("option1")
133+
134+
rerender(<ToggleButton options={options} value="option2" />)
135+
expect(button).toHaveTextContent("option2")
136+
await userEvent.click(button)
137+
expect(button).toHaveTextContent("option1")
138+
await userEvent.click(button)
139+
expect(button).toHaveTextContent("option2")
140+
})
126141
})

0 commit comments

Comments
 (0)