Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 9x 277x 271x 242x 242x 242x 242x 206x 206x 206x 206x 206x 9x 292x 277x 277x 24x 71x 71x 71x 7x 14x 14x 14x 14x 8x 64x 71x 7x 7x 7x 4x 8x 8x 5x 5x 3x 3x 5x 3x 1x 3x 124x 124x 124x 207x 25x 25x 124x 124x 25x 85x 85x 2x 2x 4x 4x 4x 4x 4x 2x 2x 124x 11x 10x 7x 3x | /**
* The parts of a table that are not React: what a column is, how a cell's value is
* narrowed to its row, and what a pick carries beside its key.
*
* Kept apart from `TableWidget` so a page that only needs one of these — a test, an
* adapter, another widget — reads it without the component's state.
*/
import type {ICycleCellState, IEnrichedFieldSchema, IWidgetProps} from '@feasibleone/blong';
import label from '../../lib/label.js';
import {parentKey} from '../cascade.js';
export type Row = Record<string, unknown>;
export type DropdownOption = {value: unknown; label: string; [k: string]: unknown};
/** The key a form row carries so an edit lands on the row it was made on. */
export const KEY = '__key';
/**
* The widget type a column renders as. A declared `widget.type` wins; otherwise the
* schema is read for the shape the column must show.
*/
export function resolveWidgetType(schema: IEnrichedFieldSchema): string {
if (schema.widget?.type) return schema.widget.type;
// A column that names a dropdown (or a static option list) holds a key, not the
// text a reader wants: render the option's label, never the stored id. @@D-612
if (schema.widget?.dropdown) return 'dropdown';
Iif (schema.widget?.options) return 'select';
Iif (schema.widget?.cycle || schema.widget?.states) return 'cycle';
Iif (schema.action) return 'action';
if (schema.type === 'boolean') return 'boolean';
Iif (schema.type === 'number') return 'number';
Iif (schema.type === 'integer') return 'integer';
Iif (schema.format === 'date-time') return 'dateTime';
Iif (schema.format === 'date') return 'date';
return 'input';
}
/**
* Named state sets for a `cycle` cell — the boolean cases a pivot toggles.
* Clicking the cell advances to the next state, so the column needs no
* row-edit mode. The empty state renders as a blank cell and is named
* `Not set` (also the cell's `title`, which tests read).
*/
export const CYCLE_PRESETS: Record<string, ICycleCellState[]> = {
// check → cross → empty
'tri-state': [
{value: true, icon: 'pi pi-check text-green-500', label: 'Yes'},
{value: false, icon: 'pi pi-times text-red-500', label: 'No'},
{value: null, label: 'Not set'},
],
// check → empty
'check-empty': [
{value: true, icon: 'pi pi-check text-green-500', label: 'Yes'},
{value: null, label: 'Not set'},
],
// check → cross (a value is always set)
'check-cross': [
{value: true, icon: 'pi pi-check text-green-500', label: 'Yes'},
{value: false, icon: 'pi pi-times text-red-500', label: 'No'},
],
};
export function resolveCycleStates(schema: IEnrichedFieldSchema): ICycleCellState[] {
const cfg = schema.widget as {cycle?: string; states?: ICycleCellState[]} | undefined;
if (cfg?.states?.length) return cfg.states;
if (cfg?.cycle && CYCLE_PRESETS[cfg.cycle]) return CYCLE_PRESETS[cfg.cycle];
return CYCLE_PRESETS['tri-state'];
}
export function isCycleCell(schema: IEnrichedFieldSchema): boolean {
return schema.widget?.type === 'cycle' || !!schema.widget?.cycle || !!schema.widget?.states;
}
/** Cycle values compare loosely, so `true`/`1` and `null`/`undefined` are one state. */
export function sameCycleValue(a: unknown, b: unknown): boolean {
if (a == null || b == null) return (a ?? null) === (b ?? null);
if (typeof a === 'boolean' || typeof b === 'boolean') return Boolean(a) === Boolean(b);
return a === b;
}
export function currentCycleState(
states: ICycleCellState[],
value: unknown,
): ICycleCellState | undefined {
return states.find(s => sameCycleValue(s.value, value));
}
/** An unset cell advances to the first state of the cycle. */
export function nextCycleState(states: ICycleCellState[], value: unknown): ICycleCellState {
const index = states.findIndex(s => sameCycleValue(s.value, value));
return states[(index + 1) % states.length];
}
export function getColumnOptions(
schema: IEnrichedFieldSchema,
dropdowns?: Record<string, unknown[]>,
): DropdownOption[] {
const raw =
schema.widget?.options ?? (schema.widget?.dropdown && dropdowns?.[schema.widget.dropdown]);
if (!Array.isArray(raw)) return [];
return raw as DropdownOption[];
}
/**
* Narrow a pick-list's options to the row they are offered for.
*
* `optionsFilter: {optionProperty: rowField}` is the explicit form, and several
* pairs are a conjunction. `parent` is the shorthand for the common case, where the
* option carries the key under the field's own name — or under `parent`, which is
* how the form-level cascade already works, so both conventions are honoured. A row
* field with no value yet leaves the list whole: the first pick must not be blocked
* by the very value it is about to set. @@D-615
*/
export function optionsForRow(
options: DropdownOption[],
schema: IEnrichedFieldSchema,
row: Row | undefined,
): DropdownOption[] {
Iif (!row) return options;
const filter = schema.widget?.optionsFilter;
if (filter) {
return options.filter(option => {
const candidate = option as unknown as Record<string, unknown>;
return Object.entries(filter).every(([optionProperty, rowField]) => {
const want = row[rowField];
if (want === undefined || want === null || want === '') return true;
return String(candidate[optionProperty]) === String(want);
});
});
}
const parent = schema.widget?.parent;
if (!parent) return options;
// A row is flat, so the parent is read at the field's own name — the leaf of the
// path a model declares. @@D-875
const key = parentKey(parent);
const want = row[key];
if (want === undefined || want === null || want === '') return options;
return options.filter(option => {
const candidate = option as unknown as Record<string, unknown>;
return candidate[key] === want || candidate['parent'] === want;
});
}
/**
* The fields a pick writes beside its key: the label the column shows (`widget.body`)
* and whatever `widget.lookup` copies from the option — so a saved row never carries
* a label that disagrees with its key. `@@D-614`
*/
export function pickPatch(
fieldSchema: IEnrichedFieldSchema,
options: DropdownOption[],
picked: unknown,
): Row {
const pick = options.find(option => option.value === picked);
if (!pick) return {};
const patch: Row = {};
const bodyField = fieldSchema.widget?.body;
if (bodyField) patch[bodyField] = pick.label;
for (const [optionKey, rowField] of Object.entries(fieldSchema.widget?.lookup ?? {})) {
patch[rowField] = (pick as Record<string, unknown>)[optionKey];
}
return patch;
}
/** The columns a table renders, from `widget.columns` or from the item schema. */
export function resolveColumns(
widget: IWidgetProps['schema']['widget'],
items: IWidgetProps['schema']['items'],
): {
field: string;
header: string;
filter?: boolean;
sortable?: boolean;
fieldSchema: IEnrichedFieldSchema;
}[] {
const properties = items?.properties as Record<string, IEnrichedFieldSchema> | undefined;
const cols = widget?.columns;
if (cols && Array.isArray(cols)) {
return cols.map(c => ({
field: c,
header: label(properties?.[c]?.title, c)!,
filter: !!(properties?.[c] as Record<string, unknown>)?.filter,
sortable: !!(properties?.[c] as Record<string, unknown>)?.sort,
fieldSchema: properties?.[c] ?? {},
}));
}
Iif (cols && typeof cols === 'object') {
return Object.entries(cols).map(([field, cfg]) => ({
field,
header: label((cfg as Record<string, unknown>)?.title as string, field)!,
fieldSchema: properties?.[field] ?? {},
}));
}
const hidden = new Set(widget?.hidden ?? []);
const show = cols ? (Array.isArray(cols) ? cols : Object.keys(cols)) : null;
if (properties) {
return Object.entries(properties)
.filter(([field]) => !hidden.has(field) && (!show || show.includes(field)))
.map(([field, schema]) => ({
field,
header: label(schema.title, field)!,
filter: !!(schema as Record<string, unknown>).filter,
sortable: !!(schema as Record<string, unknown>).sort,
fieldSchema: schema,
}));
}
return [];
}
/** An empty row for the columns a table declares, seeded with its own defaults. */
export function newRow(
colFields: string[],
properties?: Record<string, IEnrichedFieldSchema>,
): Row {
const row: Row = {};
for (const field of colFields) {
const schema = properties?.[field];
Iif (schema && 'default' in (schema as Record<string, unknown>)) {
row[field] = (schema as Record<string, unknown>).default;
} else Iif (schema?.type === 'boolean' || schema?.widget?.type === 'boolean') {
row[field] = false;
} else Iif (
schema?.type === 'number' ||
schema?.type === 'integer' ||
['number', 'integer', 'currency', 'percent'].includes(schema?.widget?.type ?? '')
) {
row[field] = null;
} else {
row[field] = '';
}
}
row[KEY] = Math.random().toString();
return row;
}
export function resolveParentField(parent?: string): string | undefined {
if (!parent) return undefined;
if (parent.startsWith('$.selected.')) return parent.slice('$.selected.'.length);
return parent;
}
/**
* Extract the stable key value from a parent selection row.
* Uses `keyFieldName` as the primary key field.
* `keyFieldName` defaults to 'id' in the widget config, so this value
* is always valid when the widget is properly configured.
*/
export function getParentKeyValue(
parentRow: Record<string, unknown> | null,
keyFieldName: string,
): unknown {
if (!parentRow) return null;
return parentRow[keyFieldName];
}
|