import ClickAwayListener from '@mui/base/ClickAwayListener'; import FormHelperText from '@mui/material/FormHelperText'; import InputLabel from '@mui/material/InputLabel'; import * as React from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import { createEditor } from 'slate'; import { Editable } from 'slate-react'; import Slate from 'components/Slate'; import Element from 'components/Slate/Element'; import { customizeEditor, deserialize, insertVariable, serialize, } from 'components/Slate/utils'; import { StepExecutionsContext } from 'contexts/StepExecutions'; import { VariableElement } from 'components/Slate/types'; import Popper from './Popper'; import { processStepWithExecutions } from './data'; import { ChildrenWrapper, FakeInput, InputLabelWrapper } from './style'; 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 disappearSuggestionsOnShift = ( event: React.KeyboardEvent ) => { if (event.code === 'Tab') { setShowVariableSuggestions(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} )} /> ); }; export default PowerInput;