Skip to content

Commit e007ce8

Browse files
Merge pull request #998 from KCSAbeywickrama/bi-dm-expr-focus-fix
[BI Data Mapper] Fix expression bar focussing issues
2 parents 0a51b50 + 6b150a7 commit e007ce8

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

workspaces/ballerina/data-mapper/src/components/DataMapper/Header/ExpressionBar.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,26 @@ export default function ExpressionBarWrapper({ views }: ExpressionBarProps) {
110110
if (inputPort) {
111111
// Keep the text field focused when an input port is selected
112112
if (textFieldRef.current) {
113+
113114
if (focusedPort || focusedFilter) {
114-
textFieldRef.current.focus(true);
115+
// Update the expression text when an input port is selected
116+
const cursorPosition = textFieldRef.current.inputElement.selectionStart;
117+
118+
const inputAccessExpr = buildInputAccessExpr(inputPort.attributes.fieldFQN);
119+
const updatedText =
120+
textFieldValue.substring(0, cursorPosition) +
121+
inputAccessExpr +
122+
textFieldValue.substring(cursorPosition);
123+
await handleChange(updatedText);
124+
125+
textFieldRef.current.setCursor(updatedText, cursorPosition + inputAccessExpr.length);
126+
115127
} else {
116128
textFieldRef.current.blur();
117129
}
118-
119-
// Update the expression text when an input port is selected
120-
const cursorPosition = textFieldRef.current.shadowRoot.querySelector('input').selectionStart;
121-
const inputAccessExpr = buildInputAccessExpr(inputPort.attributes.fieldFQN);
122-
const updatedText =
123-
textFieldValue.substring(0, cursorPosition) +
124-
inputAccessExpr +
125-
textFieldValue.substring(cursorPosition);
126-
await handleChange(updatedText);
130+
127131
resetInputPort();
132+
128133
}
129134
}
130135
})();
@@ -161,7 +166,7 @@ export default function ExpressionBarWrapper({ views }: ExpressionBarProps) {
161166

162167
return disabled;
163168
// eslint-disable-next-line react-hooks/exhaustive-deps
164-
}, [textFieldRef.current, focusedPort, focusedFilter, views]);
169+
}, [focusedPort, focusedFilter, views]);
165170

166171
const handleChange = async (text: string, cursorPosition?: number) => {
167172
if (textFieldValue === text) {

workspaces/common-libs/ui-toolkit/src/components/ExpressionEditor/components/Header/ExpressionEditor.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ export const ExpressionEditor = forwardRef<HeaderExpressionEditorRef, HeaderExpr
337337

338338
};
339339

340+
const handleRefSetCursor = (value: string, cursorPosition: number) => {
341+
setCursor(inputRef, 'input', value, cursorPosition, manualFocusTrigger);
342+
};
343+
340344
const handleRefFocus = (manualTrigger?: boolean) => {
341345
if (document.activeElement !== elementRef.current) {
342346
manualFocusTrigger.current = manualTrigger ?? false;
@@ -375,6 +379,7 @@ export const ExpressionEditor = forwardRef<HeaderExpressionEditorRef, HeaderExpr
375379
inputElement: inputRef.current?.shadowRoot?.querySelector('input'),
376380
focus: handleRefFocus,
377381
blur: handleRefBlur,
382+
setCursor: handleRefSetCursor,
378383
saveExpression: async (value?: string, ref?: React.MutableRefObject<string>) => {
379384
await handleExpressionSaveMutation(value, ref);
380385
}
@@ -419,22 +424,24 @@ export const ExpressionEditor = forwardRef<HeaderExpressionEditorRef, HeaderExpr
419424
createPortal(
420425
<DropdownContainer ref={dropdownContainerRef} sx={{ ...dropdownElPosition }}>
421426
<Transition show={showCompletions} {...ANIMATION}>
422-
<Codicon
423-
id='expression-editor-close'
424-
sx={{
425-
position: 'absolute',
426-
top: '0',
427-
right: '0',
428-
width: '16px',
429-
margin: '-4px',
430-
borderRadius: '50%',
431-
backgroundColor: 'var(--vscode-activityBar-background)',
432-
zIndex: '5',
433-
}}
434-
iconSx={{ color: 'var(--vscode-activityBar-foreground)' }}
435-
name="close"
436-
onClick={handleClose}
437-
/>
427+
<div onMouseDown={e => { e.preventDefault(); }}>
428+
<Codicon
429+
id='expression-editor-close'
430+
sx={{
431+
position: 'absolute',
432+
top: '0',
433+
right: '0',
434+
width: '16px',
435+
margin: '-4px',
436+
borderRadius: '50%',
437+
backgroundColor: 'var(--vscode-activityBar-background)',
438+
zIndex: '5',
439+
}}
440+
iconSx={{ color: 'var(--vscode-activityBar-foreground)' }}
441+
name="close"
442+
onClick={handleClose}
443+
/>
444+
</div>
438445
<Dropdown
439446
ref={dropdownRef}
440447
isSavable={!!onSave}

workspaces/common-libs/ui-toolkit/src/components/ExpressionEditor/types/header.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ export type HeaderExpressionEditorProps = ExpressionEditorProps;
2222

2323
export type HeaderExpressionEditorRef = ExpressionEditorRef & {
2424
inputElement: HTMLInputElement;
25+
setCursor: (value: string, cursorPosition: number) => void;
2526
};

0 commit comments

Comments
 (0)