import React, { useCallback, useMemo } from "react"; import { FormLayout, Label, type LayoutType } from "../../index"; import { DialPanel } from "../DialPanel"; import { DialProvider, useDialSchema } from "../DialProvider"; import { IconRenderer } from "../IconRenderer"; import { LabelPositionT } from "../types"; interface DialTupleInputProps { name: string; label?: string; labelPosition?: LabelPositionT; icon?: string; } /** * General tuple input component that handles mixed-type tuples * Similar to interface/object but displays elements in a more compact layout * Used for tuples containing strings, objects, or other complex types */ export const DialTupleInput: React.FC = ({ name, label, labelPosition = "top", icon, }) => { const { getValue, setValue, schemas } = useDialSchema(); const values = useMemo(() => getValue(name) || [], [getValue, name]); const handleElementChange = useCallback( (elementName: string, value: unknown) => { const newValues = Array.isArray(values) ? [...values] : []; // Extract index from element name (e.g., "[0]" -> 0 or "x" -> find by name) const schema = schemas.find((s) => s.name === name); if (!schema || !schema.typeDefinition?.schemas) { return; } const elementIndex = schema.typeDefinition.schemas.findIndex((s) => s.name === elementName); if (elementIndex >= 0) { newValues[elementIndex] = value; setValue(name, newValues); } }, [name, setValue, values, schemas], ); const schema = schemas.find((s) => s.name === name); if (!schema || !schema.typeDefinition?.schemas) { return null; } // Convert tuple array to object format for DialProvider const valuesObject: Record = {}; schema.typeDefinition.schemas.forEach((elemSchema, index) => { valuesObject[elemSchema.name] = Array.isArray(values) ? values[index] : undefined; }); const tupleContent = (
); if (!label) { return tupleContent; } return (
{schema.helpText && ( ⓘ {schema.helpText} )} {tupleContent}
); };