ToolJet/frontend/src/Editor/CodeBuilder/CodeHinter.jsx

44 lines
1.2 KiB
React
Raw Normal View History

import React, { useEffect, useMemo, useState } from 'react';
import CodeMirror from '@uiw/react-codemirror';
2021-05-03 11:26:31 +00:00
import 'codemirror/mode/handlebars/handlebars';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/search/match-highlighter';
2021-05-03 14:27:32 +00:00
import 'codemirror/addon/hint/show-hint.css';
2021-05-03 16:04:54 +00:00
import { getSuggestionKeys, onBeforeChange, handleChange } from './utils';
2021-05-03 14:27:32 +00:00
export function CodeHinter({
initialValue, onChange, currentState
}) {
2021-05-03 14:27:32 +00:00
const options = {
lineNumbers: false,
singleLine: true,
mode: 'handlebars',
tabSize: 2,
readOnly: false,
highlightSelectionMatches: true
};
const [realState, setRealState] = useState(currentState);
useEffect(() => {
setRealState(currentState);
}, [currentState.components]);
let suggestions = useMemo(() => {
return getSuggestionKeys(currentState);
}, [currentState.components, currentState.queries]);
2021-05-03 16:04:54 +00:00
2021-05-03 14:27:32 +00:00
return (
<div className="code-hinter form-control">
<CodeMirror
value={initialValue}
realState={realState}
2021-05-03 14:27:32 +00:00
scrollbarStyle={null}
2021-05-03 16:04:54 +00:00
onChange={(editor) => handleChange(editor, onChange, suggestions)}
2021-05-03 14:27:32 +00:00
onBeforeChange={(editor, change) => onBeforeChange(editor, change)}
options={options}
/>
</div>
);
}