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 | 9x 415x 415x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 1x 1x 1x 1x 19x 19x 26x 19x 19x 3x 389x 389x 65x 65x 65x 65x 6x 6x 6x 6x 4x 1x 6x 6x 2x 2x 2x 71x 71x 31x 3x 8x 32x 2x | /**
* How a table cell reads and how it edits.
*
* A body is the text a reader sees; an editor is what replaces it while the row is
* being changed. Both are pure functions of a row, so they live here rather than in
* the component that decides when a row is editable: a dropdown cell reads the label
* the backend joined beside its key, and writes back the label and the `lookup`
* fields a pick carries.
*/
import {ActionButton} from '../../components/ActionButton/ActionButton.js';
import {Text} from '../../components/Text/Text.js';
import {translatedLabel} from '../../hooks/useText.js';
import type {FormatFn, FormatType} from '../../lib/format.js';
import {
Calendar,
Checkbox,
Dropdown,
FileUpload,
InputMask,
InputNumber,
InputText,
InputTextarea,
MultiSelect,
Password,
RadioButton,
SelectButton,
} from '../../primereact/index.js';
import type {IEnrichedFieldSchema} from '@feasibleone/blong';
import React, {createContext, use, useEffect, useRef, useState} from 'react';
import {dateIn, dateOut} from '../DateWidget.js';
import {
type DropdownOption,
KEY,
type Row,
currentCycleState,
nextCycleState,
optionsForRow,
resolveCycleStates,
} from './model.js';
/**
* The picks a row has made while it is being edited, by row key.
*
* PrimeReact keeps an editing row's draft to itself and memoizes the body cell, so
* re-rendering the table does not reach a cell whose list depends on a sibling — the
* context does, because a consumer re-renders whatever its memoized ancestors decide.
* See `rowDrafts` for why the table has to remember the picks at all. `@@D-617`
*/
export const RowDraftContext = createContext<Record<string, Row>>({});
export function renderBody(
widgetType: string,
field: string,
rowData: Row,
cellId: string,
options: DropdownOption[],
keyFieldName: string,
fieldSchema: IEnrichedFieldSchema,
onCycle: ((value: unknown) => void) | undefined,
format: FormatFn,
): React.ReactNode {
const value = rowData[field];
switch (widgetType) {
case 'action':
return (
<ActionButton
label={String(value)}
className="p-button-link p-0 text-left"
action={fieldSchema?.action}
params={{
[keyFieldName]: rowData[keyFieldName],
id: rowData[keyFieldName],
current: rowData,
}}
/>
);
case 'cycle': {
// Click-to-cycle cell: no row-edit mode, so each click commits the next
// state straight into the form value. `title` names the current
// state, which is also what the Playwright helper matches on.
const states = resolveCycleStates(fieldSchema);
const state = currentCycleState(states, value);
return (
<span
data-testid={cellId}
className="blong-cycle cursor-pointer"
title={state?.label ?? 'Not set'}
onClick={event => {
event.stopPropagation();
onCycle?.(nextCycleState(states, value).value);
}}
>
{state?.icon ? <i className={state.icon} /> : <span> </span>}
</span>
);
}
case 'password':
return <span data-testid={cellId}>{value ? '*'.repeat(10) : ''}</span>;
case 'file': {
// A stored file is a name, or a record carrying its own url. The link is
// only offered when the column says the value is downloadable: a url
// built from a path that is not there would be a broken link. @@D-615
const raw = rowData[field];
const items = (Array.isArray(raw) ? raw : raw == null ? [] : [raw]) as Array<
string | Record<string, unknown>
>;
Iif (!items.length) return <span data-testid={cellId} />;
const pathField = fieldSchema.widget?.pathField ?? 'hash';
const basePath = fieldSchema.widget?.basePath ?? '';
const rowPath = rowData[pathField];
const rowHref = rowPath ? `${basePath}${String(rowPath)}` : undefined;
return (
<span data-testid={cellId}>
{items.map((item, index) => {
const record = typeof item === 'string' ? undefined : item;
const text = String(
record?.name ?? record?.fileName ?? record?.label ?? item ?? '',
);
const href = record
? ((record.objectURL ?? record.url ?? rowHref) as string | undefined)
: rowHref;
return (
// One cell may hold two files of the same name and url, so
// the position is part of the only identity available.
// eslint-disable-next-line @eslint-react/no-array-index-key
<React.Fragment key={`${text}-${href ?? ''}-${index}`}>
{index > 0 && ', '}
{fieldSchema.widget?.download && href ? (
<a
href={String(href)}
download={text}
className="text-inherit"
>
{text}
</a>
) : (
text
)}
</React.Fragment>
);
})}
</span>
);
}
case 'radio':
return (
<span data-testid={cellId}>
<RadioButton
checked={Boolean(value)}
disabled
readOnly
/>
</span>
);
case 'selectTableRadio':
return (
<span
data-testid={cellId}
onClick={event => {
event.stopPropagation();
onCycle?.(!value);
}}
>
<RadioButton
checked={Boolean(value)}
readOnly
// Without a commit path the cell cannot write, so it should not
// look as if it could: a read-only card still renders the flag.
disabled={onCycle === undefined}
/>
</span>
);
case 'dropdown': {
// The row carries the label the backend joined beside the key, so the
// cell needs no options at all. Options are the fallback for a table
// whose data has no label column. @@D-614
const bodyField = fieldSchema.widget?.body;
const label = bodyField ? rowData[bodyField] : undefined;
const item = options.find(o => o.value === value);
// A joined label is data — the read translated it in the active
// language. Only a static option label is a dictionary key (`D-635`).
const text = label != null ? String(label) : translatedLabel(item?.label ?? '');
return <span data-testid={cellId}>{text || (value != null ? String(value) : '')}</span>;
}
case 'dropdownTree': {
function findLabel(nodes: DropdownOption[], v: unknown): string | undefined {
for (const n of nodes) {
if (n.key === v || n.value === v) return n.label;
if (Array.isArray(n.children)) {
const found = findLabel(n.children as DropdownOption[], v);
if (found != null) return found;
}
}
}
return (
<span data-testid={cellId}>
{findLabel(options, value) ?? (value != null ? String(value) : '')}
</span>
);
}
case 'multiSelect':
case 'multiSelectTree': {
const bodyField = fieldSchema.widget?.body;
const label = bodyField ? rowData[bodyField] : undefined;
if (label != null) {
return (
<span data-testid={cellId}>
{Array.isArray(label) ? label.join(', ') : String(label)}
</span>
);
}
const arr = Array.isArray(value) ? (value as unknown[]) : [];
const labels = arr.map(v =>
translatedLabel(options.find(o => o.value === v)?.label ?? String(v)),
);
return <span data-testid={cellId}>{labels.join(', ')}</span>;
}
case 'select': {
const bodyField = fieldSchema.widget?.body;
const label = bodyField ? rowData[bodyField] : undefined;
const item = options.find(o => o.value === value);
const text = label != null ? String(label) : translatedLabel(item?.label ?? '');
return <span data-testid={cellId}>{text || (value != null ? String(value) : '')}</span>;
}
case 'date':
case 'time':
case 'dateTime': {
if (value == null) return <span data-testid={cellId} />;
return (
<span data-testid={cellId}>
{format(value, widgetType as FormatType, fieldSchema.widget?.formatOptions)}
</span>
);
}
case 'number':
case 'integer':
case 'currency':
case 'percent':
return (
<span
data-testid={cellId}
className="block text-right"
>
{value == null
? ''
: format(
value,
widgetType as FormatType,
fieldSchema.widget?.formatOptions,
fieldSchema.widget?.useGrouping,
)}
</span>
);
default:
Iif (value != null && fieldSchema.widget?.translation) {
return (
<span data-testid={cellId}>
<Text>{String(value)}</Text>
</span>
);
}
return <span data-testid={cellId}>{value != null ? String(value) : ''}</span>;
}
}
/**
* One editing cell, rendered as a consumer of the row's recorded picks.
*
* A memoized cell in PrimeReact does not re-render when the table around it does, so
* the list a cell offers is otherwise fixed at the moment the row entered edit mode —
* whole, for a row that is still empty. Reading the picks from the context brings the
* new list to the cell that depends on them. `@@D-617`
*/
export function RowCellEditor({
rowData,
options,
fieldSchema,
render,
}: {
rowData: Row;
options: DropdownOption[];
fieldSchema: IEnrichedFieldSchema;
render: (editedRow: Row, options: DropdownOption[]) => React.ReactNode;
}) {
const drafts = use(RowDraftContext);
const draft = drafts[String(rowData[KEY])];
const editedRow = draft ? {...rowData, ...draft} : rowData;
return <>{render(editedRow, optionsForRow(options, fieldSchema, editedRow))}</>;
}
/**
* One cell that edits in place.
*
* The value it shows is its own, held here until the commit has round-tripped through
* the form: an input bound straight to the committed value loses keystrokes whenever a
* re-render lands between two of them, and what is on screen must never lag the
* keyboard. Every change still commits as it happens. `@@D-618`
*/
export function InlineCellEditor({
rowData,
field,
options,
fieldSchema,
render,
commit,
}: {
rowData: Row;
field: string;
options: DropdownOption[];
fieldSchema: IEnrichedFieldSchema;
render: (args: {
rowData: Row;
options: DropdownOption[];
onDraft: (value: unknown) => void;
}) => React.ReactNode;
commit: (value: unknown) => void;
}) {
const committed = rowData[field];
const [draft, setDraft] = useState<unknown>(committed);
const focusedRef = useRef(false);
// A value that changes from outside — a load, a reset, a different row in the same
// slot — wins over what was typed, but only while the cell is not being used: the
// committed value arrives a render after the keystroke that caused it, so adopting
// it mid-typing would replace the text with the value it is about to become.
useEffect(() => {
if (focusedRef.current) return;
// eslint-disable-next-line @eslint-react/set-state-in-effect
setDraft(committed);
}, [committed]);
const editedRow = {...rowData, [field]: draft};
return (
<div
onFocus={() => {
focusedRef.current = true;
}}
onBlur={() => {
focusedRef.current = false;
}}
>
{render({
rowData: editedRow,
options: optionsForRow(options, fieldSchema, editedRow),
onDraft: (value: unknown) => {
setDraft(value);
commit(value);
},
})}
</div>
);
}
export function renderEditor(
widgetType: string,
fieldSchema: IEnrichedFieldSchema,
field: string,
rowData: Row,
cellId: string,
cellName: string,
editorCallback: (v: unknown) => void,
options: DropdownOption[],
): React.ReactNode {
const value = rowData[field];
switch (widgetType) {
case 'integer':
return (
<InputNumber
inputId={cellId}
name={cellName}
data-testid={cellId}
value={value == null ? null : Number(value)}
onValueChange={e => editorCallback(e.value)}
className="w-full"
inputClassName="w-full text-right"
showButtons
min={fieldSchema.minimum}
max={fieldSchema.maximum}
/>
);
case 'number':
return (
<InputNumber
inputId={cellId}
name={cellName}
data-testid={cellId}
value={value == null ? null : Number(value)}
onValueChange={e => editorCallback(e.value)}
className="w-full"
inputClassName="w-full text-right"
min={fieldSchema.minimum}
max={fieldSchema.maximum}
/>
);
case 'currency':
case 'percent':
return (
<InputNumber
inputId={cellId}
name={cellName}
data-testid={cellId}
value={value == null ? null : Number(value)}
onValueChange={e => editorCallback(e.value)}
className="w-full"
inputClassName="w-full text-right"
mode="decimal"
minFractionDigits={2}
maxFractionDigits={4}
min={fieldSchema.minimum}
max={fieldSchema.maximum}
/>
);
case 'dropdown':
case 'dropdownTree':
return (
<Dropdown
inputId={cellId}
name={cellName}
data-testid={cellId}
value={value}
options={options}
// The label and the `lookup` fields a pick carries are applied by
// the caller: a row being edited keeps them on PrimeReact's copy,
// a row edited in place writes them with the value. `@@D-614`
onChange={e => editorCallback(e.value)}
className="w-full blong-dropdown"
showClear={!fieldSchema.fieldRequired}
placeholder="Select…"
filter={options.length > 8}
/>
);
case 'multiSelect':
case 'multiSelectTree':
return (
<MultiSelect
inputId={cellId}
name={cellName}
data-testid={cellId}
value={Array.isArray(value) ? value : []}
options={options}
onChange={e => editorCallback(e.value)}
className="w-full blong-multiselect"
display="chip"
placeholder="Select…"
/>
);
case 'select':
return (
<SelectButton
id={cellId}
name={cellName}
data-testid={cellId}
value={value}
options={options}
onChange={e => editorCallback(e.value)}
className="white-space-nowrap"
/>
);
case 'password':
return (
<Password
inputId={cellId}
name={cellName}
data-testid={cellId}
value={value != null ? String(value) : ''}
onInput={e => editorCallback(e.currentTarget.value)}
className="w-full"
inputClassName="w-full"
feedback={false}
/>
);
case 'date':
return (
<Calendar
inputId={cellId}
name={cellName}
data-testid={cellId}
showOnFocus={false}
value={value != null ? dateIn(value as string | Date) : null}
onChange={e =>
editorCallback(e.value instanceof Date ? dateOut(e.value) : e.value)
}
showIcon
className="w-full"
/>
);
case 'time':
return (
<Calendar
inputId={cellId}
name={cellName}
data-testid={cellId}
showOnFocus={false}
value={value != null ? new Date(value as string) : new Date(1970, 0, 1)}
onChange={e => editorCallback(e.value)}
timeOnly
showIcon
className="w-full"
/>
);
case 'dateTime':
return (
<Calendar
inputId={cellId}
name={cellName}
data-testid={cellId}
showOnFocus={false}
value={value != null ? dateIn(value as string | Date) : null}
onChange={e =>
editorCallback(e.value instanceof Date ? dateOut(e.value) : e.value)
}
showTime
showIcon
className="w-full"
/>
);
case 'mask':
return (
<InputMask
id={cellId}
name={cellName}
data-testid={cellId}
value={value != null ? String(value) : ''}
onChange={e => editorCallback(e.value)}
mask={fieldSchema.widget?.mask ?? ''}
className="w-full"
/>
);
case 'boolean':
case 'checkbox':
return (
// data-testid mirrors the display cell (`${tableId}-${row}-${field}`)
// so tests can target the boolean cell editor for check/uncheck.
<span
data-testid={cellId}
onClick={e => e.stopPropagation()}
>
<Checkbox
inputId={cellId}
checked={Boolean(value)}
onChange={e => editorCallback(e.checked)}
/>
</span>
);
case 'radio':
return (
<span
data-testid={cellId}
onClick={e => e.stopPropagation()}
>
<RadioButton
inputId={cellId}
checked={Boolean(value)}
onChange={e => editorCallback(e.checked)}
/>
</span>
);
case 'file':
return (
<FileUpload
mode="basic"
id={cellId}
name={cellName}
data-testid={cellId}
accept={fieldSchema.widget?.accept}
chooseLabel="Choose"
onSelect={e => editorCallback([...(e.files ?? [])])}
/>
);
case 'text':
case 'textArea':
return (
<InputTextarea
id={cellId}
name={cellName}
data-testid={cellId}
value={value != null ? String(value) : ''}
onChange={e => editorCallback(e.target.value)}
autoFocus
className="w-full"
rows={2}
/>
);
default:
// input, chips, autocomplete, dateRange, etc.
return (
<InputText
id={cellId}
name={cellName}
data-testid={cellId}
value={value != null ? String(value) : ''}
onChange={e => editorCallback(e.target.value)}
autoFocus
className="w-full"
/>
);
}
}
|