import React from 'react'; import type { IFormikFormFieldProps } from './FormikFormField'; import { FormikFormField } from './FormikFormField'; import type { IExpressionChange, ISpelError } from '../inputs'; import { ExpressionError, ExpressionInput, ExpressionPreview } from '../inputs'; import type { ICommonFormFieldProps } from './interface'; import { firstDefined } from '../../../utils'; import { useValidationData } from '../validation'; export interface IExpressionFieldProps { placeholder?: string; markdown?: boolean; context: object; layout?: ICommonFormFieldProps['layout']; } export type IFormikExpressionFieldProps = IExpressionFieldProps & IFormikFormFieldProps; export interface IFormikExpressionFieldState { spelPreview: string; spelError: ISpelError; } const initialSpelData: IExpressionChange = { value: null, spelError: null, spelPreview: null, }; export function FormikExpressionField(props: IFormikExpressionFieldProps) { const { context, name, label, help, actions, validationMessage: message } = props; const [spelData, setSpelData] = React.useState(initialSpelData); const markdown = firstDefined(props.markdown, false); const validationNode = useValidationData(message, true); const validationMessage = validationNode.messageNode || (spelData.spelError && context && ) || (spelData.spelPreview && ); return ( ( setSpelData(changes)} context={context} {...inputProps} /> )} label={label} help={help} actions={actions} validationMessage={validationMessage} /> ); }