All files / core/blong-browser/src/widgets TableWidget.tsx

81.06% Statements 197/243
81.72% Branches 237/290
73.33% Functions 44/60
83.33% Lines 175/210

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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909                                                                                                                                            124x 292x 124x 124x         124x 124x 124x 124x 124x 124x 124x         124x 124x   124x 124x     124x       124x 124x                 124x                   5x           124x         124x     124x 124x     124x 124x 124x               124x 72x 1x 1x                 124x                       124x       124x 124x 71x 1x 71x 1x 1x   1x       124x 124x 69x             124x       124x             292x   124x   124x   292x 29x                                 124x                           124x 124x 124x 124x 124x 124x 124x 124x       292x             124x 124x 124x 124x 124x   124x   7x       7x   1x 1x 1x   6x 10x 6x         124x   316x 23x 316x         124x 124x 91x   6x 91x 4x 4x   2x 2x     2x 2x                         124x 73x   2x   2x     124x       10x 10x                   124x   6x 6x 3x 5x 3x 2x   6x   3x 3x         124x   3x 3x                 124x   3x       124x   2x 2x 2x         2x 2x 2x         124x                                 124x 124x 75x                     124x 124x   124x                                               124x                                                                 124x   124x                                                       124x                       124x             124x   124x     119x   124x                                                                                   5x 5x 5x 5x                                           1x 1x                           277x 277x 277x     277x     277x     277x   277x       277x         277x                                                                                                                                       572x 572x             572x         6x             2x                       6x                           566x 152x 117x 35x                         414x   1x   572x                             572x 572x           413x   1x                                                                       63x 63x 63x 63x           65x                       3x               3x 3x                                                                      
/**
 * The table widget: rows, paging, selection, editing mode and the toolbar around them.
 *
 * What a cell reads and how it edits lives in `./table/cells.tsx`; the pure
 * derivations in `./table/model.ts`; the state a table owns in `./table/useTableList`,
 * `./table/useTableRows`, `./table/useTableDropdowns` and `./table/useRowDrafts`; the
 * toolbar template and the paginator have their own files. What is left here is the
 * decisions only a table can make — which mode it is in, which rows are editable, and
 * where an edit goes. `@@D-620`
 */
import {useFormatValue} from '../hooks/useFormat.js';
import {
    Column,
    DataTable,
    type DataTableSelectionChangeParams,
    Dropdown,
    InputText,
    Toolbar,
} from '../primereact/index.js';
 
import type {IEnrichedFieldSchema, IWidgetProps, IWidgetToolbarButton} from '@feasibleone/blong';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {Button} from '../components/Button/Button.js';
import {useBlongFormState} from '../components/Form/FormContext.js';
import {Text} from '../components/Text/Text.js';
import {useBlong} from '../context/BlongContext.js';
import {appendSegment} from '../feedback/ids.js';
import {
    FeedbackPath,
    FeedbackRegion,
    useFeedbackPath,
    useFeedbackTargetFactory,
} from '../feedback/useFeedbackTarget.js';
import {
    InlineCellEditor,
    RowCellEditor,
    RowDraftContext,
    renderBody,
    renderEditor,
} from './table/cells.jsx';
import {
    KEY,
    type Row,
    getColumnOptions,
    getParentKeyValue,
    isCycleCell,
    newRow,
    pickPatch,
    resolveColumns,
    resolveParentField,
    resolveWidgetType,
} from './table/model.js';
import {TablePaginator} from './table/paginator.jsx';
import {evalToolbarEnabled, resolveToolbarParams} from './table/toolbar.js';
import {useRowDrafts} from './table/useRowDrafts.js';
import {useTableDropdowns} from './table/useTableDropdowns.js';
import {type ITableListOptions, useTableList} from './table/useTableList.js';
import {useTableRows} from './table/useTableRows.js';
 
export function TableWidget({
    id,
    name,
    schema,
    value,
    onChange,
    readOnly,
    disabled,
    onSelect,
    dropdowns,
}: IWidgetProps) {
    const cols = resolveColumns(schema.widget, schema.items ?? schema);
    const colFields = cols.map(c => c.field);
    const tableId = id ?? name;
    const properties = (schema.items?.properties ?? schema.properties) as
        | Record<string, IEnrichedFieldSchema>
        | undefined;
 
    // ── listAction mode: external data loading ────────────────────────────
    const listAction = schema.widget?.listAction ?? '';
    const listParams = schema.widget?.listParams;
    const resultSet = schema.widget?.resultSet ?? 'items';
    const keyFieldName = schema.widget?.keyField ?? 'id';
    const initialPageSize = schema.widget?.pageSize ?? 25;
    const widgetToolbar = schema.widget?.toolbar ?? [];
    const widgetToolbarRight = schema.widget?.toolbarRight ?? [];
    // A table that names a height scrolls its own rows and keeps the header in
    // place, instead of growing until the page has no bottom (`scrollHeight:
    // 'flex'` takes whatever the card leaves it). An unset height keeps the
    // table growing with its rows — what a detail array inside a form wants.
    const scrollHeight = schema.widget?.scrollHeight;
    const isListMode = !!listAction;
 
    const {handler} = useBlong();
    const format = useFormatValue();
    // A column is addressable by its field, so a comment about "the name column"
    // names the schema property rather than a position (`@@T-464`).
    const path = useFeedbackPath();
    // The table is a step and its columns are steps below it, so a header's label
    // reads as `column:legalName/label` and not as a sibling of the table
    // (`@@T-487`). All of it is composed only while the mode is on (`@@D-667`).
    const tablePath = path ? appendSegment(path, 'table', name) : undefined;
    const columnTarget = useFeedbackTargetFactory(tablePath);
 
    // The option lists a cell needs to show a label, and a pivot's slot rows: the
    // caller may pass them, otherwise they are loaded once through the portal.
    const {
        resolvedDropdowns,
        signature: dropdownSignature,
        labelFields,
        pivotOptions,
    } = useTableDropdowns({
        columns: colFields,
        properties: properties as
            | Record<string, {widget?: {dropdown?: string; body?: string}}>
            | undefined,
        dropdowns,
        pivotDropdown: (schema.widget?.pivot as {dropdown?: string} | undefined)?.dropdown,
        load:
            typeof handler.portalDropdownList === 'function'
                ? (names: string[]) =>
                      handler.portalDropdownList({names}, {}) as Promise<Record<string, unknown>>
                : undefined,
    });
 
    // A cascade inside a row that is still being edited: the picks the row has made are
    // kept here so the cell that depends on them can recompute its list. @@D-617
    const {rowDrafts, recordRowDraft, clearRowDrafts} = useRowDrafts(cols);
 
    // Subscribe to table selections from FormStateContext (slow-changing — only updates on
    // row selection events, never on keystrokes).  Falls back gracefully to undefined when
    // TableWidget is used outside a Form (e.g. in standalone stories / tests).
    const formState = useBlongFormState();
 
    // ── Report params (from report "Run" button) ──────────────────────────
    const reportMode = formState?.reportMode ?? false;
    const reportParams = formState?.reportParams;
 
    // ── Cascaded table filtering ───────────────────────────────────────────
    const parentFieldName = resolveParentField(schema.widget?.parent);
    const masterMapping = schema.widget?.master;
    const parentSelection = parentFieldName
        ? (formState?.tableSelections[parentFieldName] as
              | {row: Record<string, unknown>; index: number}
              | null
              | undefined)
        : undefined;
 
    /** The keys the server narrows a cascaded list by, from the selected parent row. */
    const cascadeFilter = useMemo(() => {
        if (!isListMode || !parentFieldName || !masterMapping || !parentSelection) return undefined;
        return Object.fromEntries(
            Object.entries(masterMapping).map(([ownKey, parentKey]) => [
                ownKey,
                parentSelection.row[parentKey],
            ]),
        );
    }, [isListMode, parentFieldName, masterMapping, parentSelection]);
 
    // A list table reads its rows from the server: search, filters, sort and page are
    // one hook, so what is left here is which rows the page shows.
    const list = useTableList({
        listAction,
        listParams,
        resultSet,
        reportParams,
        reportMode,
        labelFields,
        cascadeFilter,
        pageSize: initialPageSize,
        handler: handler as unknown as ITableListOptions['handler'],
    });
 
    const {setFirst: resetPage} = list;
 
    // A new parent row starts at the first page and forgets the selected one: both
    // described the previous parent.
    const prevParentKeyForPagingRef = useRef<unknown>(undefined);
    useEffect(() => {
        if (!isListMode || !parentFieldName) return;
        const curKey = getParentKeyValue(parentSelection?.row ?? null, keyFieldName);
        Iif (prevParentKeyForPagingRef.current === curKey) return;
        prevParentKeyForPagingRef.current = curKey;
        resetPage(0);
        // eslint-disable-next-line @eslint-react/set-state-in-effect
        setSingleSelected(null);
    }, [isListMode, parentFieldName, parentSelection?.row, keyFieldName, resetPage]);
 
    // A new report run starts at the first page too.
    const prevReportParamsRef = useRef<Record<string, unknown> | undefined>(undefined);
    useEffect(() => {
        Eif (!isListMode || !reportMode) return;
        if (prevReportParamsRef.current === reportParams) return;
        prevReportParamsRef.current = reportParams;
        if (reportParams !== undefined) resetPage(0);
    }, [isListMode, reportMode, reportParams, resetPage]);
 
    // ── Pivot and filter columns ──────────────────────────────────────────
    const pivotCfg = schema.widget?.pivot as
        | {examples?: Row[]; dropdown?: string; join?: Record<string, string>; defaults?: Row}
        | undefined;
    /** The slots a pivot overlays stored rows onto: its examples, or a named dropdown. */
    const pivotBaseRows: Row[] | undefined = pivotCfg
        ? (pivotCfg.examples ??
          (pivotCfg.dropdown
              ? ((dropdowns?.[pivotCfg.dropdown] as Row[] | undefined) ?? pivotOptions ?? undefined)
              : undefined))
        : undefined;
    /** One filter input per `filter: true` column, in both modes. */
    const filterFields = useMemo(() => cols.filter(c => c.filter).map(c => c.field), [cols]);
    /** A pick-list column filters on its key, exactly; a text column on a substring. */
    const pickFields = useMemo(
        () =>
            new Set(
                cols
                    .filter(c => c.fieldSchema.widget?.dropdown || c.fieldSchema.widget?.options)
                    .map(c => c.field),
            ),
        [cols],
    );
 
    // Which rows the page shows: the form value in a detail table, the server's page
    // in a list, a fixed slot set in a pivot — then the cascade, the column filters
    // and the view sort. A form table narrows and sorts what it holds; a list table
    // asks the server to.
    const {
        rows,
        sortedRows,
        visibleRows,
        sortField: formSortField,
        sortOrder: formSortOrder,
        onSortChange: onFormSortChange,
        pivotJoinDataFields,
    } = useTableRows({
        value,
        listRows: list.rows,
        isListMode,
        pivotCfg,
        pivotBaseRows,
        parentFieldName,
        masterMapping,
        parentSelection,
        filterFields,
        pickFields,
        committedFilters: list.committedFilters,
    });
 
    const [editingRows, setEditingRows] = useState<Record<string, boolean>>({});
    const [pendingEdit, setPendingEdit] = useState<Record<string, boolean> | null>(null);
    const [selected, setSelected] = useState<Row[]>([]);
    const [singleSelected, setSingleSelected] = useState<Row | null>(null);
    const allowEdit = schema.widget?.actions?.allowEdit !== false;
    const allowAdd = schema.widget?.actions?.allowAdd !== false && !pivotBaseRows;
    const allowDelete = schema.widget?.actions?.allowDelete !== false && !pivotBaseRows;
    const editable = !readOnly && allowEdit;
    // A pivot whose editable columns are all `cycle` cells is toggle-only: each
    // click commits a value, so the rows need no edit button at all.
    const cycleOnly =
        cols.some(c => isCycleCell(c.fieldSchema)) &&
        cols.every(c => isCycleCell(c.fieldSchema) || c.fieldSchema.readOnly === true);
    /**
     * A table whose cells are their own editors: no row-edit state, no pencil column,
     * every change written into the form value as it happens. The column is the unit
     * of refusal — a `readOnly` column still reads. `@@D-618`
     */
    const inlineEdit = !isListMode && editable && schema.widget?.inlineEdit === true;
    const needsRowEditor = !isListMode && editable && !cycleOnly && !inlineEdit;
    const interactionDisabled = disabled || readOnly;
    const isSingleSelect = schema.widget?.selectionMode === 'single';
    const widgetLabel = schema.widget?.label;
 
    const fireSingleSelect = useCallback(
        (row: Row | null) => {
            Iif (!row) {
                onSelect?.(name, null);
                return;
            }
            if (isListMode) {
                // In listAction mode rows have no KEY field; use keyFieldName
                const originalIndex = rows.findIndex(r => r[keyFieldName] === row[keyFieldName]);
                onSelect?.(name, {row: row as Record<string, unknown>, index: originalIndex});
                return;
            }
            const {[KEY]: _k, ...clean} = row;
            const originalIndex = rows.findIndex(r => r[KEY] === row[KEY]);
            onSelect?.(name, {row: clean as Record<string, unknown>, index: originalIndex});
        },
        [rows, name, onSelect, isListMode, keyFieldName],
    );
 
    const rowClass = useCallback(
        (data: Row) => {
            if (!isSingleSelect || !singleSelected) return '';
            const matchKey = isListMode ? keyFieldName : KEY;
            return data[matchKey] === singleSelected[matchKey] ? 'blong-table-current' : '';
        },
        [isSingleSelect, singleSelected, isListMode, keyFieldName],
    );
 
    const prevParentKeyForAutoSelectRef = useRef<unknown>(undefined);
    useEffect(() => {
        if (!schema.widget?.autoSelect || !parentFieldName) return;
        // Use the parent row's key field value for stable identity (not object reference)
        const curKey = getParentKeyValue(parentSelection?.row ?? null, keyFieldName);
        if (prevParentKeyForAutoSelectRef.current === curKey) return;
        prevParentKeyForAutoSelectRef.current = curKey;
        if (visibleRows.length > 0) {
            // eslint-disable-next-line @eslint-react/set-state-in-effect
            setSingleSelected(visibleRows[0]);
            fireSingleSelect(visibleRows[0]);
        } else {
            // eslint-disable-next-line @eslint-react/set-state-in-effect
            setSingleSelected(null);
            onSelect?.(name, null);
        }
    }, [
        name,
        parentSelection,
        parentFieldName,
        schema.widget?.autoSelect,
        visibleRows,
        fireSingleSelect,
        onSelect,
        keyFieldName,
    ]);
 
    useEffect(() => {
        if (!pendingEdit) return;
        // eslint-disable-next-line @eslint-react/set-state-in-effect
        setPendingEdit(null);
        // eslint-disable-next-line @eslint-react/set-state-in-effect
        setEditingRows(prev => ({...prev, ...pendingEdit}));
    }, [pendingEdit]);
 
    const onRowEditChange = useCallback(
        (e: {data: Record<string, boolean>}) => {
            // A row that is no longer being edited leaves no picks behind: a cancelled
            // edit must not narrow the next one. @@D-617
            clearRowDrafts();
            setEditingRows(e.data);
        },
        [clearRowDrafts],
    );
 
    /**
     * Write one row back into the form value.  For a pivot table the row may not
     * exist in the value yet (its slot comes from the dropdown), so the merge is
     * keyed on the pivot's `join` fields — the same rule the row editor uses.
     */
    const mergeRow = useCallback(
        (rowData: Row): Row[] => {
            const {[KEY]: rowKey, ...rest} = rowData;
            if (pivotCfg) {
                const joinEntries = Object.entries(pivotCfg.join ?? {});
                const isMatch = (r: Row) => joinEntries.every(([, jk]) => r[jk] === rest[jk]);
                const updated = rows.some(isMatch)
                    ? rows.map(r => (isMatch(r) ? {...r, ...rest} : r))
                    : [...rows, rest];
                return updated.map(({[KEY]: _k, ...r}) => r);
            }
            const updated = rows.map(r => (r[KEY] === rowKey ? {...rest, [KEY]: rowKey} : r));
            return updated.map(({[KEY]: _k, ...r}) => r);
        },
        [rows, pivotCfg],
    );
 
    const onRowEditComplete = useCallback(
        (e: {newData: Row}) => {
            clearRowDrafts();
            onChange(mergeRow(e.newData));
        },
        [mergeRow, onChange, clearRowDrafts],
    );
 
    /**
     * Commit one cell value straight into the form value — how a cycle cell and an
     * in-place cell write, with the label and `lookup` fields a pick carries. `@@D-618`
     */
    const commitCell = useCallback(
        (rowData: Row, field: string, nextValue: unknown, patch?: Row) =>
            onChange(mergeRow({...rowData, ...patch, [field]: nextValue})),
        [mergeRow, onChange],
    );
 
    const addRow = useCallback(
        (event: React.MouseEvent) => {
            event.preventDefault();
            const row = newRow(colFields, properties);
            Iif (parentSelection && masterMapping) {
                for (const [ownKey, parentKey] of Object.entries(masterMapping)) {
                    row[ownKey] = parentSelection.row[String(parentKey)];
                }
            }
            const updated = [...rows, row];
            onChange(updated.map(({[KEY]: _k, ...r}) => r));
            setPendingEdit({[rows.length]: true});
        },
        [rows, colFields, properties, onChange, parentSelection, masterMapping],
    );
 
    const deleteSelected = useCallback(
        (event: React.MouseEvent) => {
            event.preventDefault();
            const selectedKeys = new Set<unknown>();
            for (const r of selected) selectedKeys.add(r[KEY]);
            if (singleSelected) selectedKeys.add(singleSelected[KEY]);
            if (selectedKeys.size === 0) return;
            const updated = rows.filter(r => !selectedKeys.has(r[KEY]));
            setSelected([]);
            setSingleSelected(null);
            onSelect?.(name, null);
            onChange(updated.map(({[KEY]: _k, ...r}) => r));
        },
        [rows, selected, singleSelected, onChange, onSelect, name],
    );
 
    // ── Template context for custom toolbar params ────────────────────────
    const currentRowKey = singleSelected?.[isListMode ? keyFieldName : KEY];
    const templateContext = useMemo(
        () => ({
            [keyFieldName]: currentRowKey,
            id: currentRowKey,
            current: singleSelected,
            selected: selected.length > 0 ? selected : singleSelected ? [singleSelected] : [],
            ...(singleSelected ?? {}),
        }),
        [singleSelected, selected, keyFieldName, currentRowKey],
    );
 
    // ── Toolbar rendering ─────────────────────────────────────────────────
    const [busyCount, setBusyCount] = useState(0);
    const isBusy = busyCount > 0;
 
    const renderCustomButton = (btn: IWidgetToolbarButton, idx: number) => {
        const isEnabled = evalToolbarEnabled(btn.enabled, selected, singleSelected);
        const resolvedParams = resolveToolbarParams(btn.params, templateContext);
        return (
            <Button
                key={idx}
                label={btn.label}
                icon={btn.icon}
                className="p-button mr-2"
                type="button"
                disabled={isBusy || !isEnabled}
                onClick={e => {
                    e.preventDefault();
                    if (!btn.method) return;
                    setBusyCount(c => c + 1);
                    void (
                        handler[btn.method](resolvedParams ?? {}, {}) as Promise<unknown>
                    ).finally(() => setBusyCount(c => c - 1));
                }}
            />
        );
    };
 
    // listAction mode: search + paginator in toolbar right
    const listModeSearchBar = isListMode ? (
        <span
            style={{
                position: 'relative',
                display: 'inline-flex',
                alignItems: 'center',
                flexShrink: 0,
            }}
        >
            <i
                className="pi pi-search"
                style={{position: 'absolute', left: '0.5rem', pointerEvents: 'none'}}
            />
            <InputText
                value={list.searchInput}
                onChange={e => list.onSearchChange(e.target.value)}
                placeholder="Search…"
                data-testid="browse-search"
                style={{width: '9rem', paddingLeft: '1.75rem'}}
            />
            {list.searchInput && (
                <Button
                    icon="pi pi-times"
                    className="p-button-text p-button-sm"
                    style={{position: 'absolute', right: 0, padding: '0.25rem'}}
                    type="button"
                    onClick={() => list.onSearchChange('')}
                />
            )}
        </span>
    ) : null;
 
    const hasCustomToolbar =
        widgetToolbar.length > 0 || widgetToolbarRight.length > 0 || isListMode;
    const actionButtons =
        !isListMode && editable ? (
            <>
                {allowAdd && (
                    <Button
                        label="Add"
                        icon="pi pi-plus"
                        className="p-button mr-2"
                        onClick={addRow}
                        type="button"
                        disabled={interactionDisabled}
                        data-testid={`${tableId}-addButton`}
                    />
                )}
                {allowDelete && (
                    <Button
                        label="Delete"
                        icon="pi pi-trash"
                        className="p-button"
                        onClick={deleteSelected}
                        type="button"
                        disabled={interactionDisabled || (!selected.length && !singleSelected)}
                        data-testid={`${tableId}-deleteButton`}
                    />
                )}
            </>
        ) : null;
 
    const toolbarLeftContent = (
        <>
            {widgetLabel && (
                <span className="p-card-title mr-2">
                    <Text>{widgetLabel}</Text>
                </span>
            )}
            {widgetToolbar.map(renderCustomButton)}
            {!widgetLabel && actionButtons}
        </>
    );
 
    const toolbarRightContent = (
        <>
            {widgetToolbarRight.map(renderCustomButton)}
            {widgetLabel && actionButtons}
            {listModeSearchBar}
        </>
    );
 
    const showToolbar = editable || widgetLabel || hasCustomToolbar;
 
    if (parentFieldName && masterMapping && !parentSelection) return null;
 
    // listAction mode: compute dataKey and active row key
    const dataKeyField = isListMode ? keyFieldName : KEY;
 
    return (
        <div
            data-testid={`${tableId}`}
            className="blong-table-widget w-full"
            style={isListMode ? {display: 'flex', flexDirection: 'column'} : undefined}
        >
            {showToolbar && (
                <Toolbar
                    left={toolbarLeftContent}
                    right={toolbarRightContent}
                    className="p-0 border-none"
                    style={{background: 'none'}}
                />
            )}
            <RowDraftContext value={rowDrafts}>
                {/* The table is a step in the path its column headers extend, and it
                    is cloned with its own attributes because PrimeReact renders the
                    columns (`@@T-487`). */}
                <FeedbackRegion kind="table" name={name}>
                    <DataTable
                        key={dropdownSignature}
                        value={sortedRows}
                    // An in-place table never enters row editing: the cells are the
                    // editors, so there is no draft to save or cancel. @@D-618
                    editMode={!isListMode && editable && !inlineEdit ? 'row' : undefined}
                    editingRows={!isListMode && editable && !inlineEdit ? editingRows : undefined}
                    onRowEditChange={
                        !isListMode && editable && !inlineEdit ? onRowEditChange : undefined
                    }
                    onRowEditComplete={
                        !isListMode && editable && !inlineEdit ? onRowEditComplete : undefined
                    }
                    dataKey={dataKeyField}
                    size="small"
                    emptyMessage="No rows."
                    loading={isListMode ? list.loading : undefined}
                    lazy={isListMode}
                    scrollable={!!scrollHeight}
                    scrollHeight={scrollHeight}
                    rowClassName={rowClass}
                    selection={isSingleSelect ? singleSelected : selected}
                    onSelectionChange={(e: DataTableSelectionChangeParams) => {
                        if (isSingleSelect) {
                            const row = e.value as Row | null;
                            setSingleSelected(row);
                            fireSingleSelect(row);
                        } else E{
                            setSelected(e.value as Row[]);
                        }
                    }}
                    selectionMode={isSingleSelect ? 'single' : 'multiple'}
                    metaKeySelection={false}
                    removableSort
                    sortField={
                        isListMode ? (list.sortField ?? undefined) : (formSortField ?? undefined)
                    }
                    sortOrder={isListMode ? list.sortOrder : formSortOrder}
                    onSort={
                        isListMode
                            ? (e: {sortField: string; sortOrder: number}) => {
                                  const order = (e.sortOrder ?? 0) as 0 | 1 | -1;
                                  list.onSortChange(
                                      order === 0 ? null : (e.sortField ?? null),
                                      order === 0 ? 1 : order,
                                  );
                              }
                            : (e: {sortField: string; sortOrder: number}) => {
                                  const order = (e.sortOrder ?? 0) as 0 | 1 | -1;
                                  onFormSortChange(
                                      order === 0 ? null : (e.sortField ?? null),
                                      order === 0 ? 1 : order,
                                  );
                              }
                    }
                >
                    {!isSingleSelect && (editable || isListMode) && !pivotBaseRows && (
                        <Column
                            selectionMode="multiple"
                            style={{width: '3rem', flexGrow: 0}}
                        />
                    )}
                    {cols.map(({field, header, filter, sortable, fieldSchema}) => {
                        const widgetType = resolveWidgetType(fieldSchema);
                        const options = getColumnOptions(fieldSchema, resolvedDropdowns);
                        const isPickList = !!(
                            fieldSchema.widget?.dropdown || fieldSchema.widget?.options
                        );
                        const isNumeric = ['number', 'integer', 'currency', 'percent'].includes(
                            widgetType,
                        );
                        const isBoolType = ['boolean', 'checkbox'].includes(widgetType);
                        // One call and one answer, used for both the header's attributes
                        // and the decision whether to wrap the label at all.
                        const columnAttributes = columnTarget?.('column', field, header);
                        const columnPath =
                            columnAttributes && tablePath
                                ? appendSegment(tablePath, 'column', field)
                                : undefined;
                        const headerText = (
                            <FeedbackPath path={columnPath}>
                                <Text>{header}</Text>
                            </FeedbackPath>
                        );
 
                        return (
                            <Column
                                key={field}
                                field={field}
                                header={
                                    filter ? (
                                        <div
                                            style={{
                                                display: 'flex',
                                                flexDirection: 'column',
                                                gap: '0.25rem',
                                            }}
                                            {...columnAttributes}
                                        >
                                            {headerText}
                                            {isPickList ? (
                                                // A key column filters by picking the key,
                                                // the same way the cell is edited — typing a
                                                // substring can never match an id.
                                                <Dropdown
                                                    value={list.columnFilterInputs[field] ?? ''}
                                                    options={options}
                                                    onChange={e =>
                                                        list.onColumnFilterChange(
                                                            field,
                                                            (e.value as string) ?? '',
                                                        )
                                                    }
                                                    showClear
                                                    placeholder="Filter…"
                                                    className="p-inputtext-sm"
                                                    style={{width: '100%', fontSize: '0.8125rem'}}
                                                    onClick={e => e.stopPropagation()}
                                                    data-testid={`${tableId}-filter-${field}`}
                                                />
                                            ) : (
                                                <InputText
                                                    value={list.columnFilterInputs[field] ?? ''}
                                                    onChange={e =>
                                                        list.onColumnFilterChange(
                                                            field,
                                                            e.target.value,
                                                        )
                                                    }
                                                    placeholder="Filter…"
                                                    className="p-inputtext-sm"
                                                    style={{width: '100%', fontSize: '0.8125rem'}}
                                                    onClick={e => e.stopPropagation()}
                                                    data-testid={`${tableId}-filter-${field}`}
                                                />
                                            )}
                                        </div>
                                    ) : (
                                        // The wrapper exists only while the mode is on:
                                        // an always-present span would change every
                                        // table's DOM (and every baseline) for a
                                        // feature the suite may never enable.
                                        columnAttributes ? (
                                            <span {...columnAttributes}>{headerText}</span>
                                        ) : (
                                            headerText
                                        )
                                    )
                                }
                                sortable={sortable || isListMode}
                                alignHeader={isNumeric ? 'right' : undefined}
                                bodyClassName={isNumeric ? 'text-right' : undefined}
                                body={(rowData: Row, colOptions) => {
                                    const cellId = `${tableId}-${colOptions.rowIndex}-${field}`;
                                    const cellName = `${tableId}[${colOptions.rowIndex}].${field}`;
                                    /**
                                     * A cell that edits in place *is* its editor, so it
                                     * commits the moment it changes and there is nothing to
                                     * compare against: the value on screen is the value in
                                     * the form. @@D-618
                                     */
                                    if (
                                        inlineEdit &&
                                        !fieldSchema.readOnly &&
                                        !pivotJoinDataFields.has(field)
                                    ) {
                                        return (
                                            <InlineCellEditor
                                                rowData={rowData}
                                                field={field}
                                                options={options}
                                                fieldSchema={fieldSchema}
                                                commit={(nextValue: unknown) =>
                                                    commitCell(
                                                        rowData,
                                                        field,
                                                        nextValue,
                                                        pickPatch(fieldSchema, options, nextValue),
                                                    )
                                                }
                                                render={({
                                                    rowData: editedRow,
                                                    options: narrowed,
                                                    onDraft,
                                                }) =>
                                                    renderEditor(
                                                        widgetType,
                                                        fieldSchema,
                                                        field,
                                                        editedRow,
                                                        cellId,
                                                        cellName,
                                                        onDraft,
                                                        narrowed,
                                                    )
                                                }
                                            />
                                        );
                                    }
                                    if (isBoolType) {
                                        if (rowData[field] == null)
                                            return <span data-testid={cellId} />;
                                        return (
                                            <span data-testid={cellId}>
                                                <i
                                                    className={`pi ${
                                                        rowData[field]
                                                            ? 'pi-check text-green-500'
                                                            : 'pi-times text-red-500'
                                                    }`}
                                                />
                                            </span>
                                        );
                                    }
                                    const commit =
                                        editable && !fieldSchema.readOnly
                                            ? (nextValue: unknown) =>
                                                  commitCell(rowData, field, nextValue)
                                            : undefined;
                                    const node = renderBody(
                                        widgetType,
                                        field,
                                        rowData,
                                        cellId,
                                        options,
                                        keyFieldName,
                                        fieldSchema,
                                        commit,
                                        format,
                                    );
                                    // A cell that also carries the value it replaced shows
                                    // both, the current one first. The old value goes
                                    // through the same body, so a label, a date and a
                                    // currency read alike in both halves. @@D-615
                                    const compareField = fieldSchema.widget?.compare;
                                    if (
                                        !compareField ||
                                        rowData[compareField] === undefined ||
                                        String(rowData[compareField] ?? '') ===
                                            String(rowData[field] ?? '')
                                    ) {
                                        return node;
                                    }
                                    return (
                                        <span>
                                            <span
                                                data-testid={`${cellId}-new`}
                                                style={{color: 'var(--teal-500)'}}
                                            >
                                                {node}
                                            </span>
                                            <span
                                                data-testid={`${cellId}-old`}
                                                style={{
                                                    color: 'var(--orange-500)',
                                                    display: 'block',
                                                }}
                                            >
                                                {renderBody(
                                                    widgetType,
                                                    field,
                                                    {...rowData, [field]: rowData[compareField]},
                                                    `${cellId}-previous`,
                                                    options,
                                                    keyFieldName,
                                                    fieldSchema,
                                                    undefined,
                                                    format,
                                                )}
                                            </span>
                                        </span>
                                    );
                                }}
                                editor={
                                    !isListMode &&
                                    editable &&
                                    !fieldSchema.readOnly &&
                                    !pivotJoinDataFields.has(field)
                                        ? colOptions => {
                                              const cellId = `${tableId}-${colOptions.rowIndex}-${field}`;
                                              const cellName = `${tableId}[${colOptions.rowIndex}].${field}`;
                                              const rowData = colOptions.rowData as Row;
                                              return (
                                                  <RowCellEditor
                                                      rowData={rowData}
                                                      options={options}
                                                      fieldSchema={fieldSchema}
                                                      render={(editedRow, narrowed) =>
                                                          renderEditor(
                                                              widgetType,
                                                              fieldSchema,
                                                              field,
                                                              rowData,
                                                              cellId,
                                                              cellName,
                                                              (v: unknown) => {
                                                                  // The row being edited is
                                                                  // PrimeReact's copy: mutating it is
                                                                  // how the label and `lookup` fields
                                                                  // reach `onRowEditComplete`. @@D-614
                                                                  Object.assign(
                                                                      rowData,
                                                                      pickPatch(
                                                                          fieldSchema,
                                                                          options,
                                                                          v,
                                                                      ),
                                                                  );
                                                                  recordRowDraft(rowData, field, v);
                                                                  colOptions.editorCallback?.(v);
                                                              },
                                                              narrowed,
                                                          )
                                                      }
                                                  />
                                              );
                                          }
                                        : undefined
                                }
                            />
                        );
                    })}
                    {needsRowEditor && (
                        <Column
                            rowEditor
                            style={{width: '7rem', textAlign: 'center'}}
                        />
                    )}
                    </DataTable>
                </FeedbackRegion>
            </RowDraftContext>
            {isListMode && (list.total ?? list.rows.length) > 0 && (
                <TablePaginator
                    first={list.first}
                    pageSize={list.pageSize}
                    total={list.total}
                    hasMore={list.hasMore}
                    onFirstChange={list.setFirst}
                    onPageSizeChange={list.setPageSize}
                />
            )}
        </div>
    );
}