import type { EditorView } from "@codemirror/view";
import { generateId } from "@yaakapp-internal/lib/generateId";
import type { Color } from "@yaakapp-internal/plugins";
import classNames from "classnames";
import type { ReactNode } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useStateWithDeps } from "../hooks/useStateWithDeps";
import type { CodeEditorProps } from "./editor/CodeEditor";
import { LazyCodeEditor } from "./editor/LazyCodeEditor";
import { IconButton } from "./IconButton";
import { Label } from "./Label";
import { HStack } from "./Stacks";
export type InputProps = Pick<
CodeEditorProps,
| "language"
| "forceUpdateKey"
| "disabled"
| "autoFocus"
| "autoSelect"
| "extensions"
| "onKeyDown"
| "readOnly"
> & {
className?: string;
containerClassName?: string;
inputWrapperClassName?: string;
defaultValue?: string | null;
disableObscureToggle?: boolean;
fullHeight?: boolean;
hideLabel?: boolean;
help?: ReactNode;
label: ReactNode;
labelClassName?: string;
labelPosition?: "top" | "left";
leftSlot?: ReactNode;
multiLine?: boolean;
name?: string;
onBlur?: () => void;
onChange?: (value: string) => void;
onFocus?: () => void;
onPaste?: (value: string) => void;
onPasteOverwrite?: CodeEditorProps["onPasteOverwrite"];
placeholder?: string;
/** Renders the editor, for a host whose editor builds on CodeEditor */
renderEditor?: (props: CodeEditorProps) => ReactNode;
/** Rendered beside the right slot, given a way to replace the whole value */
renderOptionsPicker?: (insert: (value: string) => void) => ReactNode;
required?: boolean;
rightSlot?: ReactNode;
size?: "2xs" | "xs" | "sm" | "md" | "auto";
stateKey: CodeEditorProps["stateKey"];
tint?: Color;
type?: "text" | "password";
validate?: boolean | ((v: string) => boolean);
wrapLines?: boolean;
setRef?: (h: InputHandle | null) => void;
};
export interface InputHandle {
focus: () => void;
isFocused: () => boolean;
value: () => string;
selectAll: () => void;
dispatch: EditorView["dispatch"];
}
function renderCodeEditor(props: CodeEditorProps) {
return ;
}
export function Input({
className,
containerClassName,
defaultValue,
disableObscureToggle,
disabled,
forceUpdateKey,
fullHeight,
help,
hideLabel,
inputWrapperClassName,
label,
labelClassName,
labelPosition = "top",
leftSlot,
multiLine,
onBlur,
onChange,
onFocus,
onPaste,
onPasteOverwrite,
placeholder,
readOnly,
required,
renderEditor,
renderOptionsPicker,
rightSlot,
size = "md",
stateKey,
tint,
type = "text",
validate,
wrapLines,
setRef,
...props
}: InputProps) {
const [focused, setFocused] = useState(false);
const [obscured, setObscured] = useStateWithDeps(type === "password", [type]);
const [hasChanged, setHasChanged] = useStateWithDeps(false, [forceUpdateKey]);
const editorRef = useRef(null);
const skipNextFocus = useRef(false);
const handle = useMemo(
() => ({
focus: () => {
if (editorRef.current == null) return;
const anchor = editorRef.current.state.doc.length;
skipNextFocus.current = true;
editorRef.current.focus();
editorRef.current.dispatch({ selection: { anchor, head: anchor }, scrollIntoView: true });
},
isFocused: () => editorRef.current?.hasFocus ?? false,
value: () => editorRef.current?.state.doc.toString() ?? "",
dispatch: (...args) => {
// oxlint-disable-next-line no-explicit-any
editorRef.current?.dispatch(...(args as any));
},
selectAll() {
if (editorRef.current == null) return;
editorRef.current.focus();
editorRef.current.dispatch({
selection: { anchor: 0, head: editorRef.current.state.doc.length },
});
},
}),
[],
);
// Replace the whole value with a picked option. Dispatching runs it through the editor's
// onChange, same as typing, so callers see one consistent change path.
const insertOption = useCallback((value: string) => {
const view = editorRef.current;
if (view == null) return;
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } });
view.focus();
}, []);
const setEditorRef = useCallback(
(h: EditorView | null) => {
editorRef.current = h;
setRef?.(handle);
},
[handle, setRef],
);
useEffect(() => {
const fn = () => {
skipNextFocus.current = true;
};
window.addEventListener("focus", fn);
return () => {
window.removeEventListener("focus", fn);
};
}, []);
const handleFocus = useCallback(() => {
if (readOnly) return;
if (!skipNextFocus.current) {
editorRef.current?.dispatch({
selection: { anchor: 0, head: editorRef.current.state.doc.length },
});
}
setFocused(true);
onFocus?.();
skipNextFocus.current = false;
}, [onFocus, readOnly]);
const handleBlur = useCallback(async () => {
setFocused(false);
// Move selection to the end on blur
const anchor = editorRef.current?.state.doc.length ?? 0;
editorRef.current?.dispatch({
selection: { anchor, head: anchor },
});
onBlur?.();
}, [onBlur]);
const id = useRef(`input-${generateId()}`);
const editorClassName = classNames(
className,
"bg-transparent! min-w-0 h-auto w-full focus:outline-hidden placeholder:text-placeholder",
);
const isValid = useMemo(() => {
if (required && !validateRequire(defaultValue ?? "")) return false;
if (typeof validate === "boolean") return validate;
if (typeof validate === "function" && !validate(defaultValue ?? "")) return false;
return true;
}, [required, defaultValue, validate]);
const handleChange = useCallback(
(value: string) => {
onChange?.(value);
setHasChanged(true);
},
[onChange, setHasChanged],
);
const wrapperRef = useRef(null);
// Submit the nearest form on Enter key press
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key !== "Enter") return;
const form = wrapperRef.current?.closest("form");
if (!isValid || form == null) return;
form?.dispatchEvent(new Event("submit", { cancelable: true, bubbles: true }));
},
[isValid],
);
return (
{tint != null && (
)}
{leftSlot}
{(renderEditor ?? renderCodeEditor)({
setRef: setEditorRef,
id: id.current,
hideGutter: true,
singleLine: !multiLine,
containerOnly: true,
stateKey,
wrapLines,
heightMode: "auto",
onKeyDown: handleKeyDown,
type: type === "password" && !obscured ? "text" : type,
defaultValue,
forceUpdateKey,
placeholder,
onChange: handleChange,
onPaste,
onPasteOverwrite,
disabled,
className: classNames(
editorClassName,
multiLine && size === "md" && "py-1.5",
multiLine && size === "sm" && "py-1",
multiLine && (size === "xs" || size === "2xs") && "py-0.5",
),
onFocus: handleFocus,
onBlur: handleBlur,
readOnly,
...props,
})}
{type === "password" && !disableObscureToggle && (
setObscured((o) => !o)}
/>
)}
{renderOptionsPicker?.(insertOption)}
{rightSlot}
);
}
function validateRequire(v: string) {
return v.length > 0;
}