import React from "react"; import { DialButton, DialCustom } from "./components"; import type { GroupSchema, DialSchema, LabelPositionT } from "./types"; import { DialArrayInput, DialBooleanInput, DialColorInput, DialEulerDegInput, DialEulerRadInput, DialIntInput, DialNumberInput, DialNumberRadInput, DialPresetsInput, DialSelectInput, DialSliderInput, DialTextInput, DialTupleInput, DialVec3Input, DialVectorInput, } from "./wrapped-inputs"; import { DialInterfaceInput } from "./wrapped-inputs/DialInterfaceInput"; interface DialSchemaRendererProps { schemas: DialSchema[]; groups?: GroupSchema[]; labelLayout?: LabelPositionT; } /** * Generate dynamic styles based on GroupSchema * Default layout: grid with 1 column */ const generateGroupStyle = (config?: GroupSchema): React.CSSProperties => { const layout = config?.layout ?? "grid"; if (layout === "grid") { return { display: "grid", gap: "0.5rem", gridAutoFlow: config?.gridFlow, gridTemplateColumns: config?.gridColTemplate ? config.gridColTemplate : config?.gridCols ? `repeat(${config.gridCols}, 1fr)` : "1fr", gridTemplateRows: config?.gridRowTemplate ? config.gridRowTemplate : config?.gridRows ? `repeat(${config.gridRows}, 1fr)` : undefined, }; } // Flex layout return { display: "flex", gap: "0.5rem", flexWrap: config?.flexWrap ?? "wrap", justifyContent: config?.flexJustifyContent, }; }; export const DialPanel: React.FC = ({ schemas, groups: groupConfigs, labelLayout, }) => { // Create a map of group configurations for easy lookup const groupConfigMap: Record = {}; // First, populate from explicit groups prop if provided if (groupConfigs) { groupConfigs.forEach((config) => { groupConfigMap[config.name] = config; }); } // Group schemas by their grouping tag const groupedSchemas: Record = {}; const ungrouped: DialSchema[] = []; schemas.forEach((schema) => { const grouping = schema?.grouping; if (grouping) { if (!groupedSchemas[grouping]) { groupedSchemas[grouping] = []; } groupedSchemas[grouping].push(schema); } else { ungrouped.push(schema); } }); // Render a single schema item as an input component const renderSchemaInput = (schema: DialSchema) => { const { name, dtype, icon, min, max, step, options } = schema; // Use schema's label position if specified, otherwise fall back to panel's labelLayout prop const effectiveLabelPosition = (schema?.labelPosition as LabelPositionT) || labelLayout; const label = schema.label || name.charAt(0).toUpperCase() + name.slice(1); // Choose the right input component based on dtype switch (dtype) { case "tuple": // General tuple with mixed types return ( ); case "array": // Array of primitive types return ( ); case "interface": case "object": return ( ); case "vector3": return ( ); case "euler": case "euler-rad": // Alias for euler (both default to radians) return ( ); case "euler-deg": return ( ); case "euler-pi": return ( ); case "vector": case "vector-6": case "number-group": // For generic vector or number-group, use VectorN input return ; case "boolean": return ( ); case "number-slider": // Integer input return ( ); case "number-int": // Integer input return ( ); case "number-rad": // Radian input displayed as degrees return ( ); case "number": // Use slider if min and max are defined, otherwise use number input if (min !== undefined && max !== undefined && !options) { return ( ); } // Use select if options are provided if (options && options.length > 0) { return ( ); } // Default to number input return ( ); case "color": return ; case "string": case "text": case "select": // If options are provided, use select if (options && options.length > 0) { return ( ); } // Use text input for pure string values return ( ); case "button": return ( ); case "custom": // Either render function or children must be provided if (!schema.render && !schema.children) { console.warn( `DialPanel: dtype="custom" requires either render or children for "${name}"`, ); return null; } return ( ); default: return null; } }; // Wrapper function to apply span styles const wrapWithStyle = (schema: DialSchema, component: React.ReactNode) => { const spanStyle: React.CSSProperties = {}; if (schema.colSpan) { spanStyle.gridColumn = `span ${schema.colSpan}`; } if (schema.rowSpan) { spanStyle.gridRow = `span ${schema.rowSpan}`; } if (Object.keys(spanStyle).length > 0) { return (
{component}
); } return component; }; // Sort groups: Transform first, then alphabetically const sortedGroups = Object.entries(groupedSchemas).sort(([nameA], [nameB]) => { const lowerA = nameA.toLowerCase(); const lowerB = nameB.toLowerCase(); if (lowerA === "transform") return -1; if (lowerB === "transform") return 1; return lowerA.localeCompare(lowerB); }); return (
{/* Render grouped schemas */} {sortedGroups.map(([name, schemaList]) => { // Use group configuration const groupConfig = groupConfigMap[name]; const groupStyle = generateGroupStyle(groupConfig); return (
{name}
{schemaList.map((schema) => wrapWithStyle(schema, renderSchemaInput(schema)))}
); })} {/* Render ungrouped schemas - use grid with 3 columns */} {ungrouped.length > 0 && (
{/*

*/} {/* General*/} {/*

*/}
{ungrouped.map((schema) => wrapWithStyle(schema, renderSchemaInput(schema)))}
)}
); };