Skip to content

Commit 522523e

Browse files
Switch to former solution for initialValue changes in CodeAutoCompletefield
1 parent 85a73b1 commit 522523e

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

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: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import AutoSuggestion, { AutoSuggestionProps } from "../AutoSuggestion/AutoSugge
55
import Spinner from "../Spinner/Spinner";
66

77
export interface CodeAutocompleteFieldProps extends AutoSuggestionProps {
8-
/** If the component should re-init when the initial value changes. Used when a value should be set externally.*/
9-
reInitOnInitialValueChange?: boolean
8+
109
}
1110

1211
/**
@@ -15,25 +14,9 @@ export interface CodeAutocompleteFieldProps extends AutoSuggestionProps {
1514
*
1615
* Example usage: input of a path string offering auto-completion for each single part of the path.
1716
*/
18-
export function CodeAutocompleteField({ className, reInitOnInitialValueChange = false, ...otherProps }: CodeAutocompleteFieldProps) {
19-
const [reInit, setReInit] = React.useState<boolean>(false)
20-
21-
React.useEffect(() => {
22-
if(reInitOnInitialValueChange) {
23-
setReInit(true)
24-
}
25-
}, [otherProps.initialValue, reInitOnInitialValueChange])
26-
27-
React.useEffect(() => {
28-
if(reInit) {
29-
setReInit(false)
30-
}
31-
}, [reInit])
32-
17+
export function CodeAutocompleteField({ className, ...otherProps }: CodeAutocompleteFieldProps) {
3318
// Currently this works only as an alias element for `AutoSuggestion`.
34-
return reInit ?
35-
<Spinner size="tiny" position="inline" description="Validating value path"/> :
36-
<AutoSuggestion
19+
return <AutoSuggestion
3720
className={`${eccgui}-codeautocompletefield` + (className ? ` ${className}` : "")}
3821
{...otherProps}
3922
/>

0 commit comments

Comments
 (0)