Skip to content

Commit 3738448

Browse files
committed
Fix: Code editor sets initial value on every code editor instance re-init
1 parent 0d7243c commit 3738448

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- `CodeAutocompleteField`:
12+
- Code editor resets to initial value on every code editor instance re-init
13+
914
## [24.0.1] - 2025-02-06
1015

1116
### Changed

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ const AutoSuggestion = ({
219219
CodeAutocompleteFieldSuggestionWithReplacementInfo | undefined
220220
>(undefined);
221221
const [cm, setCM] = React.useState<EditorView>();
222+
const currentCm = React.useRef<EditorView>()
223+
currentCm.current = cm
222224
const isFocused = React.useRef(false);
223225
const autoSuggestionDivRef = React.useRef<HTMLDivElement>(null);
224226
/** Mutable editor state, since this needs to be current in scope of the SingleLineEditorComponent. */
@@ -235,12 +237,12 @@ const AutoSuggestion = ({
235237
const pathIsValid = validationResponse?.valid ?? true;
236238

237239
React.useEffect(() => {
238-
if (reInitOnInitialValueChange && initialValue != null && cm) {
240+
if (reInitOnInitialValueChange && initialValue != null && currentCm.current) {
239241
dispatch({
240-
changes: { from: 0, to: cm?.state?.doc.length, insert: initialValue },
242+
changes: { from: 0, to: currentCm.current.state?.doc.length, insert: initialValue },
241243
});
242244
}
243-
}, [initialValue, cm, reInitOnInitialValueChange]);
245+
}, [initialValue, reInitOnInitialValueChange]);
244246

245247
const setCurrentIndex = (newIndex: number) => {
246248
editorState.index = newIndex;

0 commit comments

Comments
 (0)