Skip to content

Commit b973b5b

Browse files
committed
extend EditEntireItemAction with shouldCloseOnOutsideClick
1 parent 82eeec2 commit b973b5b

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/webviews/src/modules/key-details/components/rejson-details/components/edit-entire-item-action/EditEntireItemAction.spec.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,41 @@ describe('EditEntireItemAction', () => {
5252
)
5353
expect(handleUpdateValueFormSubmit).not.toHaveBeenCalled()
5454
})
55+
56+
it('should call onCancel when clicking outside and shouldCloseOnOutsideClick is true', () => {
57+
const handleCancel = vi.fn()
58+
59+
render(
60+
<div data-testid="outside">
61+
<EditEntireItemAction
62+
{...instance(mockedProps)}
63+
initialValue={valueOfEntireItem}
64+
onCancel={handleCancel}
65+
shouldCloseOnOutsideClick
66+
/>
67+
</div>,
68+
)
69+
70+
// Simulate outside click
71+
fireEvent.mouseDown(screen.getByTestId('outside'))
72+
expect(handleCancel).toHaveBeenCalled()
73+
})
74+
75+
it('should NOT call onCancel when clicking outside and shouldCloseOnOutsideClick is false', () => {
76+
const handleCancel = vi.fn()
77+
78+
render(
79+
<div data-testid="outside">
80+
<EditEntireItemAction
81+
{...instance(mockedProps)}
82+
initialValue={valueOfEntireItem}
83+
onCancel={handleCancel}
84+
shouldCloseOnOutsideClick={false}
85+
/>
86+
</div>,
87+
)
88+
89+
fireEvent.mouseDown(screen.getByTestId('outside'))
90+
expect(handleCancel).not.toHaveBeenCalled()
91+
})
5592
})

src/webviews/src/modules/key-details/components/rejson-details/components/edit-entire-item-action/EditEntireItemAction.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ import styles from '../../styles.module.scss'
1717

1818
export interface Props {
1919
initialValue: string
20+
shouldCloseOnOutsideClick?: boolean
2021
onCancel?: () => void
2122
onSubmit: (value: string) => void
2223
}
2324

2425
export const EditEntireItemAction = (props: Props) => {
25-
const { initialValue, onCancel, onSubmit } = props
26+
const {
27+
initialValue,
28+
onCancel,
29+
onSubmit,
30+
shouldCloseOnOutsideClick = true,
31+
} = props
2632
const [value, setValue] = useState<string>(initialValue)
2733
const [error, setError] = useState<Nullable<string>>(null)
2834

29-
const handleClickOutside = () => {
30-
onCancel?.()
31-
}
35+
const handleClickOutside = () => shouldCloseOnOutsideClick && onCancel?.()
3236

3337
const textareaRef = useRef<HTMLTextAreaElement>(null)
3438

src/webviews/src/modules/key-details/components/rejson-details/rejson-object/RejsonObject.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export const RejsonObject = React.memo((props: JSONObjectProps) => {
256256
initialValue={valueOfEntireObject}
257257
onCancel={() => setEditEntireObject(false)}
258258
onSubmit={handleUpdateValueFormSubmit}
259+
shouldCloseOnOutsideClick={false}
259260
/>
260261
) : (
261262
<RejsonDynamicTypes

0 commit comments

Comments
 (0)