Skip to content

Commit 04983a7

Browse files
Merge pull request #227 from eccenca/fix/reInitAutoCompleteField
Allow to re-init the CodeAutocompleteField
2 parents 807b216 + 9151ffa commit 04983a7

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ This is a major release, and it might be not compatible with your current usage
8787
- `<OverviewItemDepiction/>`
8888
- improve examples in storybook
8989
- improve display for images that are to large for the available space (fully show them)
90+
- `<CodeAutocompleteField />`:
91+
- Add parameter `reInitOnInitialValueChange`, to allow the field to re-initialize if the initial value changes.
9092

9193
### Deprecated
9294

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ export interface AutoSuggestionProps {
161161
multiline?: boolean;
162162
// The editor theme, e.g. "sparql"
163163
mode?: SupportedCodeEditorModes;
164+
165+
/** If this is enabled the value of the editor is replaced with the initialValue if it changes.
166+
* FIXME: This property is a workaround for some "controlled" usages of the component via the initialValue property. */
167+
reInitOnInitialValueChange?: boolean;
164168
}
165169

166170
// Meta data regarding a request
@@ -192,6 +196,7 @@ const AutoSuggestion = ({
192196
validationRequestDelay = 200,
193197
mode,
194198
multiline = false,
199+
reInitOnInitialValueChange = false,
195200
}: AutoSuggestionProps) => {
196201
const value = React.useRef<string>(initialValue);
197202
const cursorPosition = React.useRef(0);
@@ -229,6 +234,14 @@ const AutoSuggestion = ({
229234

230235
const pathIsValid = validationResponse?.valid ?? true;
231236

237+
React.useEffect(() => {
238+
if (reInitOnInitialValueChange && initialValue != null && cm) {
239+
dispatch({
240+
changes: { from: 0, to: cm?.state?.doc.length, insert: initialValue },
241+
});
242+
}
243+
}, [initialValue, cm, reInitOnInitialValueChange]);
244+
232245
const setCurrentIndex = (newIndex: number) => {
233246
editorState.index = newIndex;
234247
setFocusedIndex(newIndex);

src/components/CodeAutocompleteField/CodeAutocompleteField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from "react";
33
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
44
import AutoSuggestion, { AutoSuggestionProps } from "../AutoSuggestion/AutoSuggestion";
55

6-
export type CodeAutocompleteFieldProps = AutoSuggestionProps;
6+
export interface CodeAutocompleteFieldProps extends AutoSuggestionProps {}
77

88
/**
99
* Input component that allows partial, fine-grained auto-completion, i.e. of sub-strings of the input string.

0 commit comments

Comments
 (0)