fix: removed setState within an effect, responsibility moved to parent key
This commit is contained in:
@@ -422,6 +422,7 @@ export default function App() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<NodeModal
|
<NodeModal
|
||||||
|
key={addNodeOpen ? 'add-open' : 'add-closed'}
|
||||||
open={addNodeOpen}
|
open={addNodeOpen}
|
||||||
onClose={() => setAddNodeOpen(false)}
|
onClose={() => setAddNodeOpen(false)}
|
||||||
onSubmit={handleAddNode}
|
onSubmit={handleAddNode}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { createElement, useEffect, useState } from 'react'
|
import { createElement, useState } from 'react'
|
||||||
import { RotateCcw, ChevronDown } from 'lucide-react'
|
import { RotateCcw, ChevronDown } from 'lucide-react'
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
@@ -48,14 +48,6 @@ export function NodeModal({ open, onClose, onSubmit, initial, title = 'Add Node'
|
|||||||
const [iconPickerOpen, setIconPickerOpen] = useState(false)
|
const [iconPickerOpen, setIconPickerOpen] = useState(false)
|
||||||
const [labelError, setLabelError] = useState(false)
|
const [labelError, setLabelError] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!open) return
|
|
||||||
setForm({ ...DEFAULT_DATA, ...initial })
|
|
||||||
setIconSearch('')
|
|
||||||
setIconPickerOpen(false)
|
|
||||||
setLabelError(false)
|
|
||||||
}, [open, initial])
|
|
||||||
|
|
||||||
const set = (key: keyof NodeData, value: unknown) =>
|
const set = (key: keyof NodeData, value: unknown) =>
|
||||||
setForm((f) => ({ ...f, [key]: value }))
|
setForm((f) => ({ ...f, [key]: value }))
|
||||||
|
|
||||||
|
|||||||
@@ -134,12 +134,12 @@ describe('NodeModal', () => {
|
|||||||
const onClose = vi.fn()
|
const onClose = vi.fn()
|
||||||
const onSubmit = vi.fn()
|
const onSubmit = vi.fn()
|
||||||
|
|
||||||
const { rerender } = render(<NodeModal open onClose={onClose} onSubmit={onSubmit} />)
|
const { rerender } = render(<NodeModal key="open-1" open onClose={onClose} onSubmit={onSubmit} />)
|
||||||
fireEvent.change(screen.getByPlaceholderText('My Server'), { target: { value: 'Temp Node' } })
|
fireEvent.change(screen.getByPlaceholderText('My Server'), { target: { value: 'Temp Node' } })
|
||||||
fireEvent.change(screen.getByPlaceholderText('server.lan'), { target: { value: 'temp.local' } })
|
fireEvent.change(screen.getByPlaceholderText('server.lan'), { target: { value: 'temp.local' } })
|
||||||
|
|
||||||
rerender(<NodeModal open={false} onClose={onClose} onSubmit={onSubmit} />)
|
rerender(<NodeModal key="closed" open={false} onClose={onClose} onSubmit={onSubmit} />)
|
||||||
rerender(<NodeModal open onClose={onClose} onSubmit={onSubmit} />)
|
rerender(<NodeModal key="open-2" open onClose={onClose} onSubmit={onSubmit} />)
|
||||||
|
|
||||||
expect((screen.getByPlaceholderText('My Server') as HTMLInputElement).value).toBe('')
|
expect((screen.getByPlaceholderText('My Server') as HTMLInputElement).value).toBe('')
|
||||||
expect((screen.getByPlaceholderText('server.lan') as HTMLInputElement).value).toBe('')
|
expect((screen.getByPlaceholderText('server.lan') as HTMLInputElement).value).toBe('')
|
||||||
|
|||||||
Reference in New Issue
Block a user