Ejemplos
Ejemplos prácticos. Demos en vivo.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Las líneas de una factura viven en un array que se pinta con list(). Cada fila se valida sola, el total cuenta solo las válidas y los inputs se mantienen intactos al agregar o quitar otras filas.
import 'nuclo' interface Line { id: number; item: string; qty: string; price: string } let nextId = 3 let lines: Line[] = [ { id: 1, item: 'Design', qty: '2', price: '120' }, { id: 2, item: '', qty: '1', price: '' }, ] const touched = new Set<number>() // line ids that were blurred let submitted: string | null = null // Validation per row, and for the whole form. const lineErrors = (l: Line) => ({ item: l.item.trim() ? null : 'Name the item', qty: Number.isInteger(+l.qty) && +l.qty > 0 ? null : 'Whole number above 0', price: l.price !== '' && +l.price >= 0 ? null : 'A price', }) const lineValid = (l: Line) => Object.values(lineErrors(l)).every((e) => e === null) const valid = () => lines.length > 0 && lines.every(lineValid) const total = () => lines.filter(lineValid).reduce((sum, l) => sum + +l.qty * +l.price, 0) const rowStyle = { display: 'grid', gridTemplateColumns: 'minmax(0, 1fr) 64px 84px 36px', gap: '6px', } // One input per cell. The row object is the state, so an input writes // straight into it and update() refreshes the errors and the total. const cell = (l: Line, key: 'item' | 'qty' | 'price', placeholder: string, type = 'text') => input({ type, placeholder, value: () => l[key], style: () => ({ borderColor: touched.has(l.id) && lineErrors(l)[key] ? '#ef4444' : '' }), 'aria-label': `${key} of line ${l.id}`, onInput: (e) => { l[key] = e.currentTarget.value; update() }, onBlur: () => { touched.add(l.id); update() }, }) // list() keeps a row's inputs while the row is in the array: adding or // removing other rows does not touch them, and their focus survives. export const Invoice = () => form( div({ style: rowStyle }, small('Item'), small('Qty'), small('Price'), span()), list(() => lines, (l) => div( div( { style: rowStyle }, cell(l, 'item', 'Item'), cell(l, 'qty', '1', 'number'), cell(l, 'price', '0.00', 'number'), button('✕', { type: 'button', title: 'Remove', 'aria-label': 'Remove line', onClick: () => { lines = lines.filter((x) => x !== l); update() }, }), ), p({ role: 'alert' }, () => touched.has(l.id) ? Object.values(lineErrors(l)).find((e) => e) ?? '' : ''), ), ), div( { style: { display: 'flex', justifyContent: 'space-between', fontWeight: '600' } }, span('Total'), span(() => total().toFixed(2)), ), div( button('Add line', { type: 'button', onClick: () => { lines.push({ id: nextId++, item: '', qty: '1', price: '' }) update() }, }), button('Send invoice', { type: 'button', disabled: () => !valid(), onClick: () => { submitted = `${lines.length} lines, total ${total().toFixed(2)}` update() }, }), ), when(() => submitted !== null, p(() => submitted ?? '')), )