import { RowMedia, RowValueMultiple, RowValueRange } from "@toolctrl/db/prisma-browser"; import { Ref, useImperativeHandle, useRef, useState, useEffect, forwardRef } from "react"; import { useTranslation } from "react-i18next"; import { MediaDto } from "~/application/dtos/entities/MediaDto"; import { PropertyType } from "~/application/enums/entities/PropertyType"; import InputDate, { RefInputDate } from "~/components/ui/input/InputDate"; import InputNumber, { RefInputNumber } from "~/components/ui/input/InputNumber"; import InputText, { RefInputText } from "~/components/ui/input/InputText"; import { PropertyWithDetails, EntityWithDetails } from "~/utils/db/entities/entities.db.server"; import InputCheckbox from "~/components/ui/input/InputCheckbox"; import InputMedia from "~/components/ui/input/InputMedia"; import PropertyAttributeHelper from "~/utils/helpers/PropertyAttributeHelper"; import { PropertyAttributeName } from "~/application/enums/entities/PropertyAttributeName"; import { RowValueMultipleDto } from "~/application/dtos/entities/RowValueMultipleDto"; import InputMultiText, { RefInputMultiText } from "~/components/ui/input/InputMultiText"; import PropertyMultiSelector from "../properties/PropertyMultiSelector"; import InputRangeNumber from "~/components/ui/input/ranges/InputRangeNumber"; import { RowValueRangeDto } from "~/application/dtos/entities/RowValueRangeDto"; import InputRangeDate from "~/components/ui/input/ranges/InputRangeDate"; import { getSeparator } from "~/utils/shared/SeparatorUtils"; import InputTextSubtype from "~/components/ui/input/subtypes/InputTextSubtype"; import InputSelect from "~/components/ui/input/InputSelect"; import InputRadioGroupCards from "~/components/ui/input/InputRadioGroupCards"; import { renderSanitizedMarkdown } from "~/utils/html-sanitizer"; import { translatePropertyTitle, translateSelectOptionLabel } from "~/utils/i18n/translateLabel"; export interface RefRowValueInput { focus: () => void; } interface Props { selected: PropertyWithDetails | undefined; entity: EntityWithDetails; textValue: string | undefined; numberValue: number | undefined; dateValue: Date | undefined; booleanValue: boolean | undefined; multiple: RowValueMultiple[] | undefined; range: RowValueRange | undefined; initialMedia?: RowMedia[] | undefined; initialOption?: string | undefined; onChange?: (value: string | number | Date | boolean | undefined | null) => void; onChangeOption?: (option: string | undefined) => void; onChangeMedia?: (option: MediaDto[]) => void; onChangeMultiple?: (option: RowValueMultipleDto[]) => void; onChangeRange?: (option: RowValueRangeDto | undefined) => void; readOnly: boolean; className?: string; autoFocus?: boolean; } const RowValueInput = ( { entity, selected, textValue, numberValue, dateValue, booleanValue, multiple, range, initialMedia, initialOption, onChange, onChangeOption, onChangeMedia, onChangeMultiple, onChangeRange, className, readOnly, autoFocus, }: Props, ref: Ref ) => { useImperativeHandle(ref, () => ({ focus })); const { t, i18n } = useTranslation(); const numberInput = useRef(null); const textInput = useRef(null); const dateInput = useRef(null); const multipleInput = useRef(null); const [media, setMedia] = useState([]); function focus() { if (selected?.type === PropertyType.TEXT) { textInput.current?.input.current?.focus(); } else if (selected?.type === PropertyType.NUMBER) { numberInput.current?.input.current?.focus(); } else if (selected?.type === PropertyType.DATE) { dateInput.current?.input.current?.focus(); } } function getSelectedTitle() { return selected ? translatePropertyTitle(t, i18n, entity.name, selected) : ""; } useEffect(() => { if (selected?.type === PropertyType.MEDIA) { if (onChangeMedia) { onChangeMedia(media); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [media]); return ( <> {selected?.type === PropertyType.TEXT ? ( <> {["monaco", "wysiwyg", "iframe"].includes(PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Editor) ?? "") && readOnly ? ( <> ) : ( <> (onChange ? onChange(e.toString()) : undefined)} required={selected.isRequired} className={className} readOnly={readOnly} disabled={readOnly} pattern={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Pattern)} minLength={PropertyAttributeHelper.getPropertyAttributeValue_Number(selected, PropertyAttributeName.Min)} maxLength={PropertyAttributeHelper.getPropertyAttributeValue_Number(selected, PropertyAttributeName.Max)} rows={PropertyAttributeHelper.getPropertyAttributeValue_Number(selected, PropertyAttributeName.Rows)} placeholder={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Placeholder)} hint={t(PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.HintText) ?? "")} help={t(PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.HelpText) ?? "")} icon={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Icon)} uppercase={PropertyAttributeHelper.getPropertyAttributeValue_Boolean(selected, PropertyAttributeName.Uppercase)} lowercase={PropertyAttributeHelper.getPropertyAttributeValue_Boolean(selected, PropertyAttributeName.Lowercase)} password={PropertyAttributeHelper.getPropertyAttributeValue_Boolean(selected, PropertyAttributeName.Password)} editor={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Editor)} editorLanguage={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.EditorLanguage)} editorSize={PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.EditorSize) as any} autoFocus={autoFocus} /> )} {["iframe"].includes(PropertyAttributeHelper.getPropertyAttributeValue_String(selected, PropertyAttributeName.Editor) ?? "") && readOnly && (
{getSafeIframePreviewUrl(textValue) ? (