@iyulab/flex-table 0.19.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,9 +105,11 @@ interface ColumnDefinition {
|
|
|
105
105
|
sortable?: boolean; // Enable sorting (default: true)
|
|
106
106
|
editable?: boolean; // Per-column edit control (follows global editable)
|
|
107
107
|
pinned?: 'left' | 'right'; // Freeze column during horizontal scroll
|
|
108
|
+
format?: string | ((value, row, col) => string); // Display format, see "format vs renderer" below
|
|
108
109
|
renderer?: CellRenderer; // Custom cell render: (value, row, col) => TemplateResult | string
|
|
109
110
|
editor?: CellEditor; // Custom cell editor: (value, row, col) => TemplateResult
|
|
110
111
|
validator?: CellValidator; // Validate before commit: (value, row, col) => string | null
|
|
112
|
+
conditionalRules?: ConditionalRule[]; // Per-cell style rules, see below
|
|
111
113
|
}
|
|
112
114
|
```
|
|
113
115
|
|
|
@@ -115,6 +117,42 @@ The `editor` callback must return a Lit `TemplateResult` containing an input ele
|
|
|
115
117
|
|
|
116
118
|
The `validator` callback returns `null` if valid, or an error message string. On failure, the cell shows a red border for 3 seconds and a `validation-error` event is dispatched.
|
|
117
119
|
|
|
120
|
+
### `format` vs `renderer`
|
|
121
|
+
|
|
122
|
+
Both control how a cell's raw value is displayed, but they differ in what they replace:
|
|
123
|
+
|
|
124
|
+
- **`format`**: a plain string pattern (Excel-style, e.g. `'#,##0.00'`, `'0.00%'`, `'$#,##0'`, `'yyyy-MM-dd'`) or a `(value) => string` function. Only the *displayed text* changes — editing, sorting, filtering, and export all keep operating on the raw underlying value. Use this for number/date/currency display formatting.
|
|
125
|
+
- **`renderer`**: a `(value, row, col) => TemplateResult | string` function that replaces the cell's rendered content entirely — badges, links, icons, multi-field composites. Sorting/filtering still use the raw value, but the visual output is fully custom.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
const columns: ColumnDefinition<Order>[] = [
|
|
129
|
+
{ key: 'total', header: 'Total', format: '#,##0.00' }, // "1,234.50"
|
|
130
|
+
{ key: 'placedAt', header: 'Placed', format: 'yyyy-MM-dd' }, // date pattern
|
|
131
|
+
{ key: 'status', header: 'Status', renderer: (v) => html`<span class="badge badge-${v}">${v}</span>` },
|
|
132
|
+
];
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
If both are set on the same column, `renderer` takes precedence — `format` has no effect once a custom `renderer` fully controls the cell's output.
|
|
136
|
+
|
|
137
|
+
### Conditional Formatting
|
|
138
|
+
|
|
139
|
+
`conditionalRules` applies a style to a cell when its `when` predicate matches — a declarative alternative to writing a `renderer` just to color-code status/threshold values:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const columns: ColumnDefinition<Order>[] = [
|
|
143
|
+
{
|
|
144
|
+
key: 'status',
|
|
145
|
+
header: 'Status',
|
|
146
|
+
conditionalRules: [
|
|
147
|
+
{ when: (v) => v === 'overdue', style: { color: '#dc2626', fontWeight: 'bold' } },
|
|
148
|
+
{ when: (v) => v === 'paid', style: { color: '#16a34a' } },
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Rules are evaluated in order and combined; later matching rules override earlier ones for overlapping style properties.
|
|
155
|
+
|
|
118
156
|
## Methods
|
|
119
157
|
|
|
120
158
|
### Row Operations
|
|
@@ -312,6 +350,27 @@ function App() {
|
|
|
312
350
|
|
|
313
351
|
All `<flex-table>` properties are available as React props, and all custom events are mapped to `on*` callbacks (e.g., `cell-edit-commit` → `onCellEditCommit`).
|
|
314
352
|
|
|
353
|
+
#### Imperative API via `ref`
|
|
354
|
+
|
|
355
|
+
`FlexTableReact` forwards `ref` to the underlying `FlexTable` custom element instance, so all [Methods](#methods) (`addRow`, `deleteRows`, `selectAll`, `setFilter`, etc.) are reachable without re-rendering the whole table:
|
|
356
|
+
|
|
357
|
+
```tsx
|
|
358
|
+
import { useRef } from 'react';
|
|
359
|
+
import { FlexTableReact, type FlexTable } from '@iyulab/flex-table/react';
|
|
360
|
+
|
|
361
|
+
function App() {
|
|
362
|
+
const tableRef = useRef<FlexTable>(null);
|
|
363
|
+
|
|
364
|
+
return (
|
|
365
|
+
<>
|
|
366
|
+
<button onClick={() => tableRef.current?.addRow({ name: '', age: 0 })}>Add row</button>
|
|
367
|
+
<button onClick={() => tableRef.current?.deleteRows()}>Delete selected</button>
|
|
368
|
+
<FlexTableReact ref={tableRef} columns={columns} data={data} selectable />
|
|
369
|
+
</>
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
315
374
|
#### Typed rows (generics)
|
|
316
375
|
|
|
317
376
|
`FlexTableReact` and `ColumnDefinition` are generic over your row type — no `as unknown as` casts needed in `data`, `columns`, or callbacks:
|