From 8786aaae251dbd5717306ddb6b5968b65b9992a6 Mon Sep 17 00:00:00 2001 From: Guillermo Arrieta Medina Date: Wed, 28 Jan 2026 18:08:00 -0600 Subject: [PATCH] Arreglado orden de renderizado de campos --- src/routes/planes/$planId/_detalle/index.tsx | 71 +++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/src/routes/planes/$planId/_detalle/index.tsx b/src/routes/planes/$planId/_detalle/index.tsx index e92286b..1d89976 100644 --- a/src/routes/planes/$planId/_detalle/index.tsx +++ b/src/routes/planes/$planId/_detalle/index.tsx @@ -50,36 +50,59 @@ function DatosGeneralesPage() { // Efecto para transformar data?.datos en el arreglo de campos useEffect(() => { - const properties = data?.estructuras_plan?.definicion?.properties + const definicion = data?.estructuras_plan?.definicion as any + const properties = definicion?.properties + const requiredOrder = definicion?.required as Array | undefined - const valores = data?.datos as Record + const valores = (data?.datos as Record) || {} if (properties && typeof properties === 'object') { - const datosTransformados: Array = Object.entries( - properties, - ).map(([key, schema], index) => { - const rawValue = valores[key] + let keys = Object.keys(properties) - return { - id: (index + 1).toString(), - label: schema?.title || formatLabel(key), - helperText: schema?.description || '', - holder: schema?.examples || '', - value: - rawValue !== undefined && rawValue !== null ? String(rawValue) : '', + // Ordenar llaves basado en la lista "required" si existe + if (Array.isArray(requiredOrder)) { + keys = keys.sort((a, b) => { + const indexA = requiredOrder.indexOf(a) + const indexB = requiredOrder.indexOf(b) + // Si 'a' está en la lista y 'b' no -> 'a' primero (-1) + if (indexA !== -1 && indexB === -1) return -1 + // Si 'b' está en la lista y 'a' no -> 'b' primero (1) + if (indexA === -1 && indexB !== -1) return 1 + // Si ambos están, comparar índices + if (indexA !== -1 && indexB !== -1) return indexA - indexB + // Ninguno en la lista, mantener orden relativo + return 0 + }) + } - requerido: true, + const datosTransformados: Array = keys.map( + (key, index) => { + const schema = properties[key] + const rawValue = valores[key] - // 👇 TIPO DE CAMPO - tipo: Array.isArray(schema?.enum) - ? 'select' - : schema?.type === 'number' - ? 'number' - : 'texto', + return { + id: (index + 1).toString(), + label: schema?.title || formatLabel(key), + helperText: schema?.description || '', + holder: schema?.examples || '', + value: + rawValue !== undefined && rawValue !== null + ? String(rawValue) + : '', - opciones: schema?.enum || [], - } - }) + requerido: true, + + // 👇 TIPO DE CAMPO + tipo: Array.isArray(schema?.enum) + ? 'select' + : schema?.type === 'number' + ? 'number' + : 'texto', + + opciones: schema?.enum || [], + } + }, + ) setCampos(datosTransformados) } @@ -205,7 +228,7 @@ function DatosGeneralesPage() { value={editValue} onChange={(e) => setEditValue(e.target.value)} className="placeholder:text-muted-foreground/70 min-h-30 not-italic placeholder:italic" - placeholder={`Ej. ${campo.holder[0] as string}`} + placeholder={`Ej. ${campo.holder[0]}`} />