Skip to content

Commit f966e16

Browse files
author
Roman Snapko
committed
Refactor connection-select event handlers for reuse and readability
1 parent b9d2965 commit f966e16

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

packages/react-ui/src/app/features/builder/step-settings/block-settings/connection-select.tsx

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '@openops/shared';
2020
import { t } from 'i18next';
2121
import { PencilLine, Plus, X } from 'lucide-react';
22+
import type React from 'react';
2223
import { memo, useCallback, useState } from 'react';
2324
import { ControllerRenderProps, useFormContext } from 'react-hook-form';
2425

@@ -68,7 +69,7 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
6869
);
6970

7071
const handleReconnectClick = useCallback(
71-
(e?: React.MouseEvent<HTMLElement>) => {
72+
(e?: React.SyntheticEvent<HTMLElement>) => {
7273
e?.preventDefault?.();
7374
e?.stopPropagation?.();
7475
const currentValue = form.getValues(params.name);
@@ -85,6 +86,26 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
8586
[connectionsPage?.data, form, params.name],
8687
);
8788

89+
const suppressPointerOrMouseDown = useCallback(
90+
(e: React.PointerEvent<HTMLElement> | React.MouseEvent<HTMLElement>) => {
91+
e.stopPropagation();
92+
e.preventDefault();
93+
},
94+
[],
95+
);
96+
97+
const makeActivationKeysHandler = (
98+
action: (e: React.KeyboardEvent<HTMLElement>) => void,
99+
) => {
100+
return (e: React.KeyboardEvent<HTMLElement>) => {
101+
if (e.key === ' ' || e.key === 'Enter') {
102+
e.stopPropagation();
103+
e.preventDefault();
104+
action(e);
105+
}
106+
};
107+
};
108+
88109
return (
89110
<FormField
90111
control={form.control}
@@ -165,20 +186,11 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
165186
variant="ghost"
166187
size="xs"
167188
className="text-primary-700 text-base font-medium"
168-
onPointerDown={(e) => {
169-
e.stopPropagation();
170-
e.preventDefault();
171-
}}
172-
onMouseDown={(e) => {
173-
e.stopPropagation();
174-
e.preventDefault();
175-
}}
176-
onKeyDown={(e) => {
177-
if (e.key === ' ' || e.key === 'Enter') {
178-
e.stopPropagation();
179-
e.preventDefault();
180-
}
181-
}}
189+
onPointerDown={suppressPointerOrMouseDown}
190+
onMouseDown={suppressPointerOrMouseDown}
191+
onKeyDown={makeActivationKeysHandler((e) =>
192+
handleReconnectClick(e),
193+
)}
182194
onClick={(e) => {
183195
handleReconnectClick(e);
184196
}}
@@ -192,20 +204,11 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
192204
variant="ghost"
193205
size="xs"
194206
className="text-primary-700 text-base font-medium"
195-
onPointerDown={(e) => {
196-
e.stopPropagation();
197-
e.preventDefault();
198-
}}
199-
onMouseDown={(e) => {
200-
e.stopPropagation();
201-
e.preventDefault();
202-
}}
203-
onKeyDown={(e) => {
204-
if (e.key === ' ' || e.key === 'Enter') {
205-
e.stopPropagation();
206-
e.preventDefault();
207-
}
208-
}}
207+
onPointerDown={suppressPointerOrMouseDown}
208+
onMouseDown={suppressPointerOrMouseDown}
209+
onKeyDown={makeActivationKeysHandler(() => {
210+
field.onChange('');
211+
})}
209212
onClick={() => {
210213
field.onChange('');
211214
}}

0 commit comments

Comments
 (0)