Arreglado orden de renderizado de campos
This commit is contained in:
@@ -50,36 +50,59 @@ function DatosGeneralesPage() {
|
|||||||
|
|
||||||
// Efecto para transformar data?.datos en el arreglo de campos
|
// Efecto para transformar data?.datos en el arreglo de campos
|
||||||
useEffect(() => {
|
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<string> | undefined
|
||||||
|
|
||||||
const valores = data?.datos as Record<string, unknown>
|
const valores = (data?.datos as Record<string, unknown>) || {}
|
||||||
|
|
||||||
if (properties && typeof properties === 'object') {
|
if (properties && typeof properties === 'object') {
|
||||||
const datosTransformados: Array<DatosGeneralesField> = Object.entries(
|
let keys = Object.keys(properties)
|
||||||
properties,
|
|
||||||
).map(([key, schema], index) => {
|
|
||||||
const rawValue = valores[key]
|
|
||||||
|
|
||||||
return {
|
// Ordenar llaves basado en la lista "required" si existe
|
||||||
id: (index + 1).toString(),
|
if (Array.isArray(requiredOrder)) {
|
||||||
label: schema?.title || formatLabel(key),
|
keys = keys.sort((a, b) => {
|
||||||
helperText: schema?.description || '',
|
const indexA = requiredOrder.indexOf(a)
|
||||||
holder: schema?.examples || '',
|
const indexB = requiredOrder.indexOf(b)
|
||||||
value:
|
// Si 'a' está en la lista y 'b' no -> 'a' primero (-1)
|
||||||
rawValue !== undefined && rawValue !== null ? String(rawValue) : '',
|
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<DatosGeneralesField> = keys.map(
|
||||||
|
(key, index) => {
|
||||||
|
const schema = properties[key]
|
||||||
|
const rawValue = valores[key]
|
||||||
|
|
||||||
// 👇 TIPO DE CAMPO
|
return {
|
||||||
tipo: Array.isArray(schema?.enum)
|
id: (index + 1).toString(),
|
||||||
? 'select'
|
label: schema?.title || formatLabel(key),
|
||||||
: schema?.type === 'number'
|
helperText: schema?.description || '',
|
||||||
? 'number'
|
holder: schema?.examples || '',
|
||||||
: 'texto',
|
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)
|
setCampos(datosTransformados)
|
||||||
}
|
}
|
||||||
@@ -205,7 +228,7 @@ function DatosGeneralesPage() {
|
|||||||
value={editValue}
|
value={editValue}
|
||||||
onChange={(e) => setEditValue(e.target.value)}
|
onChange={(e) => setEditValue(e.target.value)}
|
||||||
className="placeholder:text-muted-foreground/70 min-h-30 not-italic placeholder:italic"
|
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]}`}
|
||||||
/>
|
/>
|
||||||
<div className="flex justify-end gap-2">
|
<div className="flex justify-end gap-2">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Reference in New Issue
Block a user