import InputText from "~/components/ui/input/InputText"; import { JsonPropertyDto } from "../dtos/JsonPropertyTypeDto"; import { JsonPropertiesValuesDto, JsonValue } from "../dtos/JsonPropertiesValuesDto"; import InputNumber from "~/components/ui/input/InputNumber"; import InputCheckbox from "~/components/ui/input/InputCheckbox"; import InputDate from "~/components/ui/input/InputDate"; import InputImage from "~/components/ui/input/InputImage"; import InputSelect from "~/components/ui/input/InputSelect"; import InputCombobox from "~/components/ui/input/InputCombobox"; import { useTranslation } from "react-i18next"; import { Fragment, lazy, Suspense, useState } from "react"; import clsx from "clsx"; import InputGroup from "~/components/ui/forms/InputGroup"; const NovelEditor = lazy(() => import("~/modules/novel/ui/editor")); export default function JsonPropertyValuesInput({ prefix = "attributes", properties, attributes, }: { prefix?: string; properties: JsonPropertyDto[] | JsonValue | null; attributes: JsonPropertiesValuesDto | null; }) { let propertiesObj = properties as JsonPropertyDto[] | null; if (!propertiesObj) { return null; } const groups: { name: string; properties: JsonPropertyDto[] }[] = []; propertiesObj.forEach((property) => { let group = groups.find((g) => g.name === property.group); if (!group) { group = { name: property.group || "", properties: [] }; groups.push(group); } group.properties.push(property); }); return ( {groups.map((group, idx) => { return ( {group.name ? ( ) : ( )} ); })} ); } function GroupInputs({ prefix, properties, attributes }: { prefix: string; properties: JsonPropertyDto[]; attributes: JsonPropertiesValuesDto | null }) { let propertiesObj = properties as JsonPropertyDto[] | null; return ( {propertiesObj?.map((property) => { return (
); })}
); } function JsonPropertyInput({ prefix, property, attributes }: { prefix: string; property: JsonPropertyDto; attributes: JsonPropertiesValuesDto | null }) { const { t } = useTranslation(); let value: JsonValue | undefined = attributes ? attributes[property.name] : undefined; switch (property.type) { case "string": let stringValue = value === undefined ? undefined : (value as string); const defaultStringValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (stringValue === undefined && defaultStringValue !== undefined) { stringValue = defaultStringValue; } return (
); case "number": { let numberValue = value === undefined ? undefined : (value as number); const defaultNumberValue = property.defaultValue === undefined ? undefined : (property.defaultValue as number); if (numberValue === undefined && defaultNumberValue !== undefined) { numberValue = defaultNumberValue; } return (
); } case "boolean": { let booleanValue = value === undefined ? undefined : value === "true" || value === true || value === 1 || value === "1"; const defaultBooleanValue = property.defaultValue === undefined ? undefined : property.defaultValue === "true" || property.defaultValue === true || property.defaultValue === 1 || property.defaultValue === "1"; if (booleanValue === undefined && defaultBooleanValue !== undefined) { booleanValue = defaultBooleanValue; } return (
); } case "date": { let dateValue = !value ? undefined : new Date(value as string); const defaultDateValue = property.defaultValue === undefined ? undefined : new Date(property.defaultValue as string); if (dateValue === undefined && defaultDateValue !== undefined) { dateValue = defaultDateValue; } return (
); } case "image": { let imageValue = value === undefined ? undefined : (value as string); const defaultImageValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (imageValue === undefined && defaultImageValue !== undefined) { imageValue = defaultImageValue; } return (
); } case "select": { let stringValue = value === undefined ? undefined : (value as string); const defaultStringValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (stringValue === undefined && defaultStringValue !== undefined) { stringValue = defaultStringValue; } return (
f.value) || []} placeholder={`${t("shared.select")}...`} />
); } case "multiselect": { let arrValue = value === undefined ? [] : (value as Array); const defaultArrValue = property.defaultValue === undefined ? [] : (property.defaultValue as Array); if (!value && defaultArrValue.length > 0) { arrValue = defaultArrValue; } return ; } case "wysiwyg": { let stringValue = value === undefined ? undefined : (value as string); const defaultStringValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (stringValue === undefined && defaultStringValue !== undefined) { stringValue = defaultStringValue; } return (
); } case "monaco": { let stringValue = value === undefined ? undefined : (value as string); const defaultStringValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (stringValue === undefined && defaultStringValue !== undefined) { stringValue = defaultStringValue; } return (
); } case "content": { let stringValue = value === undefined ? undefined : (value as string); const defaultStringValue = property.defaultValue === undefined ? undefined : (property.defaultValue as string); if (stringValue === undefined && defaultStringValue !== undefined) { stringValue = defaultStringValue; } return ; } default: return null; } } function JsonMultiSelectInput({ prefix, property, initial }: { prefix: string; property: JsonPropertyDto; initial: string[] }) { const { t } = useTranslation(); const [actualValue, setActualValue] = useState<(string | number)[]>(initial); return (
{actualValue?.map((item, idx) => { return ; })} f.value) || []} withSearch={false} />
); } function ContentForm({ prefix, property, value }: { prefix: string; property: JsonPropertyDto; value: string | undefined }) { const { t } = useTranslation(); const [content, setContent] = useState(value); const [contentType, setContentType] = useState("wysiwyg"); return (
{/* */}
•
{contentType === "wysiwyg" ? (
setContent(e.html ?? "")} usingLocalStorage={false} />
) : contentType === "markdown" ? ( setContent(e.toString())} /> ) : null}
); }