import * as React from 'react'; import ClickAwayListener from '@mui/base/ClickAwayListener'; import Chip from '@mui/material/Chip'; import Popper from '@mui/material/Popper'; import InputLabel from '@mui/material/InputLabel'; import FormHelperText from '@mui/material/FormHelperText'; import { Controller, useFormContext } from 'react-hook-form'; import { createEditor } from 'slate'; import { Slate, Editable, useSelected, useFocused } from 'slate-react'; import { serialize, deserialize, insertVariable, customizeEditor, } from './utils'; import Suggestions from './Suggestions'; import { StepExecutionsContext } from 'contexts/StepExecutions'; import { FakeInput, InputLabelWrapper, ChildrenWrapper } from './style'; import { VariableElement } from './types'; import { processStepWithExecutions } from './data'; type PowerInputProps = { onChange?: (value: string) => void; onBlur?: (value: string) => void; defaultValue?: string; name: string; label?: string; type?: string; required?: boolean; readOnly?: boolean; description?: string; docUrl?: string; clickToCopy?: boolean; disabled?: boolean; shouldUnregister?: boolean; }; const PowerInput = (props: PowerInputProps) => { const { control } = useFormContext(); const { defaultValue = '', onBlur, name, label, required, description, disabled, shouldUnregister, } = props; const priorStepsWithExecutions = React.useContext(StepExecutionsContext); const editorRef = React.useRef(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); }, [priorStepsWithExecutions]); const handleBlur = React.useCallback( (value) => { onBlur?.(value); }, [onBlur] ); const handleVariableSuggestionClick = React.useCallback( (variable: Pick) => { insertVariable(editor, variable, stepsWithVariables); }, [stepsWithVariables] ); return ( ( { controllerOnChange(serialize(value)); }} > { setShowVariableSuggestions(false); }} > {/* ref-able single child for ClickAwayListener */} {label} { setShowVariableSuggestions(true); }} onBlur={() => { controllerOnBlur(); handleBlur(value); }} /> {/* ghost placer for the variables popover */}
{description} )} /> ); }; const SuggestionsPopper = (props: any) => { const { open, anchorEl, data, onSuggestionClick } = props; return ( ); }; const Element = (props: any) => { const { attributes, children, element } = props; switch (element.type) { case 'variable': return ; default: return

{children}

; } }; const Variable = ({ attributes, children, element }: any) => { const selected = useSelected(); const focused = useFocused(); const label = ( <> {element.name} {children} ); return ( ); }; export default PowerInput;