Add a ref to ensure invalid ref returns the latest value (#8816)

This commit is contained in:
Kent Wang
2025-06-30 10:01:18 +08:00
committed by GitHub
parent 0bef406035
commit d476b25515

View File

@@ -24,6 +24,7 @@ export interface EnvironmentEditorHandle {
export const EnvironmentEditor = forwardRef<EnvironmentEditorHandle, Props>(
({ environmentInfo, onBlur, onChange }, ref) => {
const editorRef = useRef<CodeEditorHandle>(null);
const editorErrorRef = useRef('');
const [error, setError] = useState('');
const getValue = useCallback(() => {
// @ts-expect-error -- current can be null
@@ -41,12 +42,17 @@ export const EnvironmentEditor = forwardRef<EnvironmentEditorHandle, Props>(
useImperativeHandle(
ref,
() => ({
isValid: () => !error,
isValid: () => !editorErrorRef.current,
getValue,
}),
[error, getValue],
[getValue],
);
const updateEditorError = (message: string) => {
editorErrorRef.current = message;
setError(message);
};
const defaultValue = orderedJSON.stringify(
environmentInfo.object,
environmentInfo.propertyOrder || null,
@@ -60,7 +66,7 @@ export const EnvironmentEditor = forwardRef<EnvironmentEditorHandle, Props>(
autoPrettify
enableNunjucks
onChange={() => {
setError('');
updateEditorError('');
try {
const value = getValue();
// Check for invalid key names
@@ -68,13 +74,13 @@ export const EnvironmentEditor = forwardRef<EnvironmentEditorHandle, Props>(
// Check root and nested properties
const err = checkNestedKeys(value.object);
if (err) {
setError(err);
updateEditorError(err);
} else {
onChange?.(value);
}
}
} catch (err) {
setError(err.message);
updateEditorError(err.message);
}
}}
defaultValue={defaultValue}