fix(captura): inputs de text/fecha se borraban al teclear (valor desde dirty)

This commit is contained in:
2026-09-12 07:36:37 +00:00
parent 1791dd69ba
commit 1c81331614
2 changed files with 40 additions and 2 deletions

View File

@@ -245,10 +245,36 @@ describe('TablaCaptura', () => {
// Cambiar la fecha marca dirty en el campo _fecha y se guarda en el bulk
fireEvent.change(fechas[1], { target: { value: '2026-12-01' } })
// El input muestra el valor recién elegido (no se resetea al guardado)
expect((fechas[1] as HTMLInputElement).value).toBe('2026-12-01')
fireEvent.click(screen.getByText('Guardar cambios'))
expect(onGuardar).toHaveBeenCalledTimes(1)
const dirty = onGuardar.mock.calls[0][0]
expect(dirty['409-2']).toEqual({ certificacion_gas_fecha: '2026-12-01' })
})
it('regresión: escribir en un campo text no se borra (valor viene de dirty)', () => {
const onGuardar = vi.fn()
const { container } = render(
<TablaCaptura
unidades={UNIDADES}
campos={CAMPOS_OBRA}
datos={DATOS}
onGuardar={onGuardar}
guardando={false}
/>,
)
// Teclear en comentarios de la primera unidad (original: 'Listo')
const textos = container.querySelectorAll('input[type="text"]')
expect(textos.length).toBe(2) // 1 campo text * 2 unidades
fireEvent.change(textos[0], { target: { value: 'Nota X' } })
expect((textos[0] as HTMLInputElement).value).toBe('Nota X')
// Guardar persiste el texto tecleado
fireEvent.click(screen.getByText('Guardar cambios'))
expect(onGuardar).toHaveBeenCalledTimes(1)
expect(onGuardar.mock.calls[0][0]['409-1']).toEqual({ comentarios_obra: 'Nota X' })
})
})

View File

@@ -24,6 +24,18 @@ function valorOriginal(datos: DatosCaptura, unidad: string, campo: string): stri
return datos[unidad]?.[campo] ?? null
}
/** Valor efectivo: el editado (dirty) si existe, si no el guardado.
* De lo contrario el input controlado se resetea al escribir. */
function valorEfectivo(
dirty: DirtyMap,
datos: DatosCaptura,
unidad: string,
campo: string,
): string | null {
const d = dirty[unidad]?.[campo]
return d !== undefined ? d : valorOriginal(datos, unidad, campo)
}
/** Fecha de hoy en formato ISO local (YYYY-MM-DD) */
function hoyISO(): string {
const d = new Date()
@@ -283,7 +295,7 @@ export function TablaCaptura({
{unidad}
</td>
{camposVisibles.map((c) => {
const val = valorOriginal(datos, unidad, c.campo)
const val = valorEfectivo(dirty, datos, unidad, c.campo)
const isDirty = dirty[unidad]?.[c.campo] !== undefined
const campoFecha = c.conFecha
return (
@@ -299,7 +311,7 @@ export function TablaCaptura({
dirty={isDirty}
conFecha={campoFecha}
valorFecha={
campoFecha ? valorOriginal(datos, unidad, campoFecha) : undefined
campoFecha ? valorEfectivo(dirty, datos, unidad, campoFecha) : undefined
}
dirtyFecha={campoFecha ? dirty[unidad]?.[campoFecha] !== undefined : false}
onFechaChange={