Skip to content

Commit

Permalink
[Advanced Settings] Fix code editor field (elastic#177772)
Browse files Browse the repository at this point in the history
Fixes elastic#177600

## Summary

This PR fixes the incorrect behaviour described in
elastic#177600, which seems to be
caused because the `onChange` handler of the code editor component is
redundantly called with the current value when the "Reset to default"
link or the "Save" button is clicked. This fix adds a check for whether
the value passed to the `onChange` is different from the current value
and only then it would update the field.
  • Loading branch information
ElenaStoeva authored Mar 5, 2024
1 parent 6dd9a88 commit f4bb26d
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ export const CodeEditorInput = ({
);

const debouncedUpdateValue = useMemo(() => {
// Trigger update 1000 ms after the user stopped typing to reduce validation requests to the server
return debounce(updateValue, 1000);
// Trigger update 500 ms after the user stopped typing to reduce validation requests to the server
return debounce(updateValue, 500);
}, [updateValue]);

const onChange: CodeEditorProps['onChange'] = async (newValue) => {
// @ts-expect-error
setValue(newValue);
await debouncedUpdateValue(newValue, onUpdate);
// Only update the value when the onChange handler is called with a different value from the current one
if (newValue !== value) {
// @ts-expect-error
setValue(newValue);
await debouncedUpdateValue(newValue, onUpdate);
}
};

useEffect(() => {
Expand Down

0 comments on commit f4bb26d

Please sign in to comment.