import clsx from "clsx"; import { forwardRef, Fragment, ReactNode, Ref, RefObject, useEffect, useImperativeHandle, useRef, useState } from "react"; import EntityIcon from "../../layouts/icons/EntityIcon"; import HintTooltip from "../tooltips/HintTooltip"; import Editor from "@monaco-editor/react"; import NovelEditor from "../../../modules/novel/ui/editor"; import { useApp } from "../../../hooks"; export interface RefInputText { input: RefObject | RefObject; } type EditorSize = "sm" | "md" | "lg" | "auto" | "full" | "screen"; export interface InputTextProps { id?: string; name?: string; title?: string; withLabel?: boolean; value?: string; setValue?: React.Dispatch>; className?: string; classNameBg?: string; minLength?: number; maxLength?: number; readOnly?: boolean; disabled?: boolean; required?: boolean; autoComplete?: string; withTranslation?: boolean; translationParams?: string[]; placeholder?: string; pattern?: string; rows?: number; button?: ReactNode; lowercase?: boolean; uppercase?: boolean; type?: string; darkMode?: boolean; hint?: ReactNode; help?: string; icon?: string; editor?: string; // monaco editorLanguage?: string; // "javascript" | "typescript" | "html" | "css" | "json"; editorHideLineNumbers?: boolean; editorTheme?: "vs-dark" | "light"; editorFontSize?: number; onBlur?: () => void; borderless?: boolean; editorSize?: EditorSize; autoFocus?: boolean; isError?: boolean; isSuccess?: boolean; } const InputText = ( { id, name, title, withLabel = true, value, setValue, className, classNameBg, help, disabled = false, readOnly = false, required = false, minLength, maxLength, autoComplete, withTranslation = false, translationParams = [], placeholder, pattern, hint, rows, button, lowercase, uppercase, type = "text", darkMode, icon, editor, editorLanguage, editorHideLineNumbers, editorTheme = "vs-dark", editorFontSize, onBlur, borderless, editorSize = "sm", autoFocus, isError, isSuccess, }: InputTextProps, ref: Ref ) => { const { t } = useApp(); const [actualValue, setActualValue] = useState(value ?? ""); const [actualEditorSize] = useState(editorSize ?? ("sm" as EditorSize)); useEffect(() => { setActualValue(value ?? ""); }, [value]); useEffect(() => { if (onChange) { onChange(actualValue); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualValue]); useImperativeHandle(ref, () => ({ input })); const input = useRef(null); const textArea = useRef(null); function getTranslation(value: string) { /* if (!i18n.exists(value)) { return null; } */ return t(value); } function onChange(value: string) { if (setValue) { if (lowercase) { setValue(value.toLowerCase()); } else if (uppercase) { setValue(value.toUpperCase()); } else { setValue(value); } } } return (
{withLabel && ( )}
{editor === "monaco" && editorLanguage ? ( <>