2026-03-26 21:47:24 +03:00
|
|
|
// Widget that displays an array as editable text.
|
|
|
|
|
// Single-line mode (default): space-separated in an Input.
|
|
|
|
|
// Multiline mode (options.multiline): one item per line in a Textarea.
|
2026-02-27 18:55:36 +03:00
|
|
|
import type { WidgetProps } from "@rjsf/utils";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
2026-03-26 21:47:24 +03:00
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { getSizedFieldClassName } from "../utils";
|
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2026-02-27 18:55:36 +03:00
|
|
|
|
2026-03-26 21:47:24 +03:00
|
|
|
function arrayToText(value: unknown, multiline: boolean): string {
|
|
|
|
|
const sep = multiline ? "\n" : " ";
|
|
|
|
|
if (Array.isArray(value) && value.length > 0) {
|
|
|
|
|
return value.join(sep);
|
|
|
|
|
}
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function textToArray(text: string, multiline: boolean): string[] {
|
|
|
|
|
if (text.trim() === "") {
|
|
|
|
|
return [];
|
2026-02-27 18:55:36 +03:00
|
|
|
}
|
2026-03-26 21:47:24 +03:00
|
|
|
return multiline
|
|
|
|
|
? text.split("\n").filter((line) => line.trim() !== "")
|
|
|
|
|
: text.trim().split(/\s+/);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ArrayAsTextWidget(props: WidgetProps) {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
value,
|
|
|
|
|
disabled,
|
|
|
|
|
readonly,
|
|
|
|
|
onChange,
|
|
|
|
|
onBlur,
|
|
|
|
|
onFocus,
|
|
|
|
|
placeholder,
|
|
|
|
|
schema,
|
|
|
|
|
options,
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
const multiline = !!(options.multiline as boolean);
|
2026-02-27 18:55:36 +03:00
|
|
|
|
2026-03-26 21:47:24 +03:00
|
|
|
// Local state keeps raw text so newlines aren't stripped mid-typing
|
|
|
|
|
const [text, setText] = useState(() => arrayToText(value, multiline));
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setText(arrayToText(value, multiline));
|
|
|
|
|
}, [value, multiline]);
|
|
|
|
|
|
|
|
|
|
const fieldClassName = multiline
|
|
|
|
|
? getSizedFieldClassName(options, "md")
|
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
const handleInputChange = useCallback(
|
|
|
|
|
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
|
|
|
const raw = e.target.value;
|
|
|
|
|
setText(raw);
|
|
|
|
|
onChange(textToArray(raw, multiline));
|
|
|
|
|
},
|
|
|
|
|
[onChange, multiline],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleBlur = useCallback(
|
|
|
|
|
(e: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
|
|
|
// Clean up: strip empty entries and sync
|
|
|
|
|
const cleaned = textToArray(e.target.value, multiline);
|
|
|
|
|
onChange(cleaned);
|
|
|
|
|
setText(arrayToText(cleaned, multiline));
|
|
|
|
|
onBlur?.(id, e.target.value);
|
2026-02-27 18:55:36 +03:00
|
|
|
},
|
2026-03-26 21:47:24 +03:00
|
|
|
[id, onChange, onBlur, multiline],
|
2026-02-27 18:55:36 +03:00
|
|
|
);
|
|
|
|
|
|
2026-03-26 21:47:24 +03:00
|
|
|
if (multiline) {
|
|
|
|
|
return (
|
|
|
|
|
<Textarea
|
|
|
|
|
id={id}
|
2026-05-30 01:00:30 +03:00
|
|
|
className={cn(fieldClassName)}
|
2026-03-26 21:47:24 +03:00
|
|
|
value={text}
|
|
|
|
|
disabled={disabled || readonly}
|
|
|
|
|
rows={(options.rows as number) || 3}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
onBlur={handleBlur}
|
|
|
|
|
onFocus={(e) => onFocus?.(id, e.target.value)}
|
|
|
|
|
aria-label={schema.title}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 18:55:36 +03:00
|
|
|
return (
|
|
|
|
|
<Input
|
2026-03-26 21:47:24 +03:00
|
|
|
value={text}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
onBlur={handleBlur}
|
2026-02-27 18:55:36 +03:00
|
|
|
disabled={disabled}
|
|
|
|
|
readOnly={readonly}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|