diff --git a/packages/web/src/components/PowerInput/Suggestions.tsx b/packages/web/src/components/PowerInput/Suggestions.tsx index dc3b8205..293aaa4a 100644 --- a/packages/web/src/components/PowerInput/Suggestions.tsx +++ b/packages/web/src/components/PowerInput/Suggestions.tsx @@ -15,25 +15,19 @@ import type { IStep } from '@automatisch/types'; const ListItemText = styled(MuiListItemText)``; type SuggestionsProps = { - query?: string | null; - index: number; - data: any; + data: any[]; onSuggestionClick: (variable: any) => void; }; const SHORT_LIST_LENGTH = 4; const LIST_HEIGHT = 256; -const getPartialFilteredArray = (array: any[], query = '', length = array.length) => { - return array - .filter((suboption: any) => suboption.name.includes(query)) - .slice(0, length); +const getPartialArray = (array: any[], length = array.length) => { + return array.slice(0, length); } const Suggestions = (props: SuggestionsProps) => { const { - query = '', - index: focusIndex, data, onSuggestionClick = () => null, } = props; @@ -79,13 +73,13 @@ const Suggestions = (props: SuggestionsProps) => { - {getPartialFilteredArray(option.output as any || [], query as string, listLength) + {getPartialArray(option.output as any || [], listLength) .map((suboption: any, index: number) => ( onSuggestionClick(suboption)} - selected={focusIndex === index}> + > { const editorRef = React.useRef(null); const [target, setTarget] = React.useState(null); const [index, setIndex] = React.useState(0); - const [search, setSearch] = React.useState(null); const renderElement = React.useCallback(props => , []); const [editor] = React.useState(() => customizeEditor(createEditor())); + const [showVariableSuggestions, setShowVariableSuggestions] = React.useState(false); const stepsWithVariables = React.useMemo(() => { return processStepWithExecutions(priorStepsWithExecutions); @@ -70,46 +70,9 @@ const PowerInput = (props: PowerInputProps) => { const handleVariableSuggestionClick = React.useCallback( (variable: Pick) => { - if (target) { - Transforms.select(editor, target); insertVariable(editor, variable, stepsWithVariables); - setTarget(null); - } }, - [index, target, stepsWithVariables] - ); - - const onKeyDown = React.useCallback( - event => { - if (target) { - switch (event.key) { - case 'ArrowDown': { - event.preventDefault(); - setIndex((currentIndex) => currentIndex + 1); - break - } - case 'ArrowUp': { - event.preventDefault(); - setIndex((currentIndex) => currentIndex - 1 < 0 ? 0 : currentIndex - 1); - break - } - case 'Tab': - case 'Enter': { - event.preventDefault(); - Transforms.select(editor, target); - insertVariable(editor, stepsWithVariables[0].output[index], stepsWithVariables); - setTarget(null); - break - } - case 'Escape': { - event.preventDefault(); - setTarget(null); - break - } - } - } - }, - [index, search, target, stepsWithVariables] + [index, stepsWithVariables] ); return ( @@ -125,42 +88,12 @@ const PowerInput = (props: PowerInputProps) => { value={deserialize(value, stepsWithVariables)} onChange={value => { controllerOnChange(serialize(value)); - const { selection } = editor - - if (selection && Range.isCollapsed(selection)) { - const [start] = Range.edges(selection); - const lineBefore = Editor.before(editor, start, { unit: 'line' }); - const before = lineBefore && Editor.before(editor, lineBefore); - const beforeRange = (before || lineBefore) && Editor.range(editor, before || lineBefore, start); - const beforeText = beforeRange && Editor.string(editor, beforeRange); - const variableMatch = beforeText && beforeText.match(/@([\w.]*?)$/); - - if (variableMatch) { - const beginningOfVariable = Editor.before( - editor, - start, - { - unit: 'offset', - distance: (variableMatch[1].length || 0) + 1 - } - ); - if (beginningOfVariable) { - const newTarget = Editor.range(editor, beginningOfVariable, start); - if (newTarget) { - setTarget(newTarget); - } - } - setIndex(0); - setSearch(variableMatch[1]); - - return; - } - } - - setSearch(null); }} > - setSearch(null)}> + { setShowVariableSuggestions(false); }} + > {/* ref-able single child for ClickAwayListener */}
@@ -179,7 +112,9 @@ const PowerInput = (props: PowerInputProps) => { readOnly={disabled} style={{ width: '100%' }} renderElement={renderElement} - onKeyDown={onKeyDown} + onFocus={() => { + setShowVariableSuggestions(true); + }} onBlur={() => { controllerOnBlur(); handleBlur(value); }} /> @@ -192,18 +127,12 @@ const PowerInput = (props: PowerInputProps) => { {description} - - - + data={stepsWithVariables} + onSuggestionClick={handleVariableSuggestionClick} + />
@@ -212,6 +141,37 @@ const PowerInput = (props: PowerInputProps) => { ) } +const SuggestionsPopper = (props: any) => { + const { + open, + anchorEl, + data, + onSuggestionClick, + } = props; + + return ( + + + + ); +}; + const Element = (props: any) => { const { attributes, children, element } = props; switch (element.type) {