@trama-ui/react-native 0.0.0-stage → 0.7.0

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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +198 -2
  3. package/dist/accordion.d.ts +29 -0
  4. package/dist/accordion.js +137 -0
  5. package/dist/alert.d.ts +30 -0
  6. package/dist/alert.js +95 -0
  7. package/dist/avatar.d.ts +36 -0
  8. package/dist/avatar.js +91 -0
  9. package/dist/bars.d.ts +44 -0
  10. package/dist/bars.js +74 -0
  11. package/dist/button.d.ts +30 -0
  12. package/dist/button.js +90 -0
  13. package/dist/calendar.d.ts +71 -0
  14. package/dist/calendar.js +276 -0
  15. package/dist/card.d.ts +41 -0
  16. package/dist/card.js +139 -0
  17. package/dist/choices.d.ts +87 -0
  18. package/dist/choices.js +228 -0
  19. package/dist/collections.d.ts +131 -0
  20. package/dist/collections.js +230 -0
  21. package/dist/color-code.d.ts +37 -0
  22. package/dist/color-code.js +158 -0
  23. package/dist/compositions.d.ts +110 -0
  24. package/dist/compositions.js +190 -0
  25. package/dist/controls.d.ts +85 -0
  26. package/dist/controls.js +221 -0
  27. package/dist/drag-drop.d.ts +65 -0
  28. package/dist/drag-drop.js +482 -0
  29. package/dist/feedback.d.ts +30 -0
  30. package/dist/feedback.js +81 -0
  31. package/dist/file-upload.d.ts +67 -0
  32. package/dist/file-upload.js +251 -0
  33. package/dist/generated/icons.d.ts +1037 -0
  34. package/dist/generated/icons.js +3 -0
  35. package/dist/generated/tokens.d.ts +64 -0
  36. package/dist/generated/tokens.js +65 -0
  37. package/dist/icon.d.ts +31 -0
  38. package/dist/icon.js +28 -0
  39. package/dist/index.d.ts +43 -0
  40. package/dist/index.js +43 -0
  41. package/dist/input.d.ts +43 -0
  42. package/dist/input.js +115 -0
  43. package/dist/interactive-layout.d.ts +26 -0
  44. package/dist/interactive-layout.js +117 -0
  45. package/dist/internal/content.d.ts +17 -0
  46. package/dist/internal/content.js +24 -0
  47. package/dist/internal/drag-geometry.d.ts +16 -0
  48. package/dist/internal/drag-geometry.js +25 -0
  49. package/dist/internal/keyboard.d.ts +20 -0
  50. package/dist/internal/keyboard.js +101 -0
  51. package/dist/internal/motion.d.ts +1 -0
  52. package/dist/internal/motion.js +24 -0
  53. package/dist/internal/number-format.d.ts +4 -0
  54. package/dist/internal/number-format.js +25 -0
  55. package/dist/internal/pointer-capture.d.ts +1 -0
  56. package/dist/internal/pointer-capture.js +16 -0
  57. package/dist/internal/state.d.ts +6 -0
  58. package/dist/internal/state.js +28 -0
  59. package/dist/internal/system-drag.d.ts +48 -0
  60. package/dist/internal/system-drag.js +93 -0
  61. package/dist/internal/table-drag.d.ts +56 -0
  62. package/dist/internal/table-drag.js +237 -0
  63. package/dist/internal/table-editing.d.ts +49 -0
  64. package/dist/internal/table-editing.js +278 -0
  65. package/dist/internal/table-editor.d.ts +11 -0
  66. package/dist/internal/table-editor.js +31 -0
  67. package/dist/internal/table-grid.d.ts +44 -0
  68. package/dist/internal/table-grid.js +153 -0
  69. package/dist/internal/table-model.d.ts +132 -0
  70. package/dist/internal/table-model.js +231 -0
  71. package/dist/internal/table-source.d.ts +9 -0
  72. package/dist/internal/table-source.js +80 -0
  73. package/dist/internal/tour-target.d.ts +23 -0
  74. package/dist/internal/tour-target.js +81 -0
  75. package/dist/keyboard.d.ts +14 -0
  76. package/dist/keyboard.js +27 -0
  77. package/dist/layout.d.ts +35 -0
  78. package/dist/layout.js +77 -0
  79. package/dist/loader.d.ts +18 -0
  80. package/dist/loader.js +79 -0
  81. package/dist/localization.d.ts +106 -0
  82. package/dist/localization.js +310 -0
  83. package/dist/media.d.ts +35 -0
  84. package/dist/media.js +96 -0
  85. package/dist/menus.d.ts +103 -0
  86. package/dist/menus.js +422 -0
  87. package/dist/navigation.d.ts +111 -0
  88. package/dist/navigation.js +255 -0
  89. package/dist/overlays.d.ts +97 -0
  90. package/dist/overlays.js +364 -0
  91. package/dist/pill.d.ts +33 -0
  92. package/dist/pill.js +90 -0
  93. package/dist/progress.d.ts +27 -0
  94. package/dist/progress.js +154 -0
  95. package/dist/responsive.d.ts +15 -0
  96. package/dist/responsive.js +27 -0
  97. package/dist/select.d.ts +74 -0
  98. package/dist/select.js +181 -0
  99. package/dist/separator.d.ts +13 -0
  100. package/dist/separator.js +34 -0
  101. package/dist/switch.d.ts +26 -0
  102. package/dist/switch.js +69 -0
  103. package/dist/table.d.ts +122 -0
  104. package/dist/table.js +928 -0
  105. package/dist/text-fields.d.ts +39 -0
  106. package/dist/text-fields.js +134 -0
  107. package/dist/theme.d.ts +47 -0
  108. package/dist/theme.js +71 -0
  109. package/dist/transient.d.ts +80 -0
  110. package/dist/transient.js +273 -0
  111. package/package.json +50 -4
@@ -0,0 +1,237 @@
1
+ import { useEffect, useMemo, useRef, useState } from 'react';
2
+ import { PanResponder, Platform } from 'react-native';
3
+ import { edgeScrollSpeed } from './drag-geometry.js';
4
+ import { gridColumnAt } from './table-grid.js';
5
+ import { capturePointer } from './pointer-capture.js';
6
+ import { sameTableCell } from './table-model.js';
7
+ export function useTableDrag(options) {
8
+ const latest = useRef(options);
9
+ latest.current = options;
10
+ const viewportRef = useRef(null);
11
+ const bodyRef = useRef(null);
12
+ const rowHosts = useRef(new Map());
13
+ const active = useRef(null);
14
+ const frame = useRef(null);
15
+ const lastRelease = useRef(0);
16
+ const [armed, setArmed] = useState(false);
17
+ const [preview, setPreview] = useState(null);
18
+ function cancelFrame() {
19
+ if (frame.current !== null)
20
+ cancelAnimationFrame(frame.current);
21
+ frame.current = null;
22
+ }
23
+ function stop(commit) {
24
+ const drag = active.current;
25
+ if (!drag)
26
+ return false;
27
+ if (commit && drag.started)
28
+ updateFocus(drag);
29
+ active.current = null;
30
+ if (!drag.started)
31
+ return false;
32
+ lastRelease.current = Date.now();
33
+ cancelFrame();
34
+ drag.release?.();
35
+ if (commit && latest.current.enabled && drag.layout === latest.current.layout)
36
+ latest.current.onSelect({ anchor: drag.anchor, focus: drag.focus });
37
+ setPreview(null);
38
+ setArmed(false);
39
+ return true;
40
+ }
41
+ useEffect(() => {
42
+ stop(false);
43
+ if (!options.enabled)
44
+ setArmed(false);
45
+ }, [options.layout, options.enabled]);
46
+ useEffect(() => () => {
47
+ active.current?.release?.();
48
+ active.current = null;
49
+ cancelFrame();
50
+ }, []);
51
+ function measure(drag, offset, now) {
52
+ drag.sample = now;
53
+ function sample(view, store) {
54
+ view?.measureInWindow((x, y, width, height) => {
55
+ if (active.current === drag && drag.sample === now && width > 0 && height > 0)
56
+ store({ x, y, width, height });
57
+ });
58
+ }
59
+ sample(viewportRef.current, (bounds) => {
60
+ drag.viewport = bounds;
61
+ });
62
+ sample(bodyRef.current, (bounds) => {
63
+ drag.body = bounds;
64
+ });
65
+ for (const [row, view] of rowHosts.current) {
66
+ sample(view, (bounds) => drag.rows.set(row, { bounds, offset }));
67
+ }
68
+ for (const row of drag.rows.keys())
69
+ if (!rowHosts.current.has(row))
70
+ drag.rows.delete(row);
71
+ }
72
+ function updateFocus(drag) {
73
+ if (!drag.viewport)
74
+ return;
75
+ const current = latest.current;
76
+ const { x, y, width, height } = drag.viewport;
77
+ const offset = current.position();
78
+ const body = drag.body ?? drag.viewport;
79
+ const targetY = Math.max(y, Math.min(y + height - 0.01, drag.point.y));
80
+ let target;
81
+ let nearest = Infinity;
82
+ for (const [row, sample] of drag.rows) {
83
+ const pinned = row < current.pinnedRowsStart || row >= current.rowCount - current.pinnedRowsEnd;
84
+ const shifted = sample.bounds.y - (pinned ? 0 : offset.y - sample.offset);
85
+ const top = Math.max(shifted, pinned ? y : body.y, y);
86
+ const bottom = Math.min(shifted + sample.bounds.height, pinned ? y + height : body.y + body.height, y + height);
87
+ if (bottom <= top)
88
+ continue;
89
+ const distance = Math.max(top - targetY, targetY - (bottom - 0.01), 0);
90
+ if (distance < nearest) {
91
+ nearest = distance;
92
+ target = row;
93
+ }
94
+ }
95
+ if (target === undefined || !current.columns)
96
+ return;
97
+ const index = gridColumnAt(drag.point.x - x, width, offset.x, current.offsets, current.pinnedColumnsStart, current.pinnedColumnsEnd, current.rtl);
98
+ const focus = current.cell(target, Math.max(0, Math.min(current.columns - 1, index - current.utility)));
99
+ if (!sameTableCell(focus, drag.focus)) {
100
+ drag.focus = focus;
101
+ setPreview({ anchor: drag.anchor, focus });
102
+ }
103
+ }
104
+ function tick(now) {
105
+ const drag = active.current;
106
+ if (!drag?.started)
107
+ return;
108
+ const current = latest.current;
109
+ if (now - drag.sample >= 40)
110
+ measure(drag, current.position().y, now);
111
+ updateFocus(drag);
112
+ if (drag.viewport && drag.body) {
113
+ const { x, width } = drag.viewport;
114
+ const logicalX = current.rtl ? x + width - drag.point.x : drag.point.x - x;
115
+ const { y, height } = drag.body;
116
+ const deltaX = edgeScrollSpeed(logicalX, current.pinnedColumnsStart, width - current.pinnedColumnsEnd);
117
+ const deltaY = edgeScrollSpeed(drag.point.y, y, y + height);
118
+ if (deltaX || deltaY)
119
+ current.scroll({ x: deltaX, y: deltaY });
120
+ }
121
+ frame.current = requestAnimationFrame(tick);
122
+ }
123
+ function start(anchor, point, pointer) {
124
+ if (!latest.current.enabled || active.current)
125
+ return;
126
+ lastRelease.current = 0;
127
+ active.current = {
128
+ anchor,
129
+ focus: anchor,
130
+ origin: point,
131
+ point,
132
+ started: false,
133
+ pointer,
134
+ rows: new Map(),
135
+ sample: -Infinity,
136
+ layout: latest.current.layout,
137
+ };
138
+ }
139
+ function move(point) {
140
+ const drag = active.current;
141
+ if (!drag)
142
+ return false;
143
+ drag.point = point;
144
+ if (!drag.started) {
145
+ if (Math.hypot(point.x - drag.origin.x, point.y - drag.origin.y) < 6)
146
+ return false;
147
+ drag.started = true;
148
+ setPreview({ anchor: drag.anchor, focus: drag.anchor });
149
+ frame.current = requestAnimationFrame(tick);
150
+ }
151
+ return true;
152
+ }
153
+ const touch = useMemo(() => PanResponder.create({
154
+ onMoveShouldSetPanResponderCapture: (_, gesture) => Boolean(active.current && Math.hypot(gesture.dx, gesture.dy) >= 6),
155
+ onPanResponderGrant: ({ nativeEvent }) => move({ x: nativeEvent.pageX, y: nativeEvent.pageY }),
156
+ onPanResponderMove: ({ nativeEvent }) => move({ x: nativeEvent.pageX, y: nativeEvent.pageY }),
157
+ onPanResponderRelease: ({ nativeEvent }) => {
158
+ if (active.current)
159
+ active.current.point = { x: nativeEvent.pageX, y: nativeEvent.pageY };
160
+ stop(true);
161
+ },
162
+ onPanResponderTerminate: () => stop(false),
163
+ onPanResponderTerminationRequest: () => !active.current?.started,
164
+ }), []);
165
+ const webEvents = {
166
+ onPointerMove(event) {
167
+ const drag = active.current;
168
+ const { pointerId, clientX, clientY } = event.nativeEvent;
169
+ if (!drag || drag.pointer !== pointerId || !move({ x: clientX, y: clientY }))
170
+ return;
171
+ drag.release ??= capturePointer(event.currentTarget, pointerId);
172
+ event.preventDefault();
173
+ },
174
+ onPointerUp(event) {
175
+ if (active.current?.pointer !== event.nativeEvent.pointerId)
176
+ return;
177
+ active.current.point = { x: event.nativeEvent.clientX, y: event.nativeEvent.clientY };
178
+ if (stop(true))
179
+ event.preventDefault();
180
+ },
181
+ onPointerCancel: () => stop(false),
182
+ onPointerLeave: () => {
183
+ if (!active.current?.started)
184
+ stop(false);
185
+ },
186
+ };
187
+ return {
188
+ armed: options.enabled && armed,
189
+ preview,
190
+ viewportRef,
191
+ bodyRef,
192
+ row(index, view) {
193
+ if (view)
194
+ rowHosts.current.set(index, view);
195
+ else
196
+ rowHosts.current.delete(index);
197
+ },
198
+ toggle() {
199
+ stop(false);
200
+ setArmed(!armed);
201
+ },
202
+ cancel() {
203
+ const interrupted = stop(false);
204
+ if (armed)
205
+ setArmed(false);
206
+ return interrupted || armed;
207
+ },
208
+ suppressPress: () => Boolean(active.current?.started || Date.now() - lastRelease.current < 220),
209
+ events: !options.enabled ? {} : Platform.OS === 'web' ? webEvents : touch.panHandlers,
210
+ cellEvents(cell) {
211
+ if (!options.enabled)
212
+ return {};
213
+ if (Platform.OS === 'web')
214
+ return {
215
+ onPointerDown({ nativeEvent }) {
216
+ if (!active.current)
217
+ lastRelease.current = 0;
218
+ if (nativeEvent.button === 0 && (nativeEvent.pointerType === 'mouse' || armed))
219
+ start(cell, { x: nativeEvent.clientX, y: nativeEvent.clientY }, nativeEvent.pointerId);
220
+ },
221
+ };
222
+ return {
223
+ onTouchStart(event) {
224
+ if (!active.current)
225
+ lastRelease.current = 0;
226
+ if (armed)
227
+ start(cell, { x: event.nativeEvent.pageX, y: event.nativeEvent.pageY });
228
+ },
229
+ onTouchEnd: () => {
230
+ if (!active.current?.started)
231
+ stop(false);
232
+ },
233
+ onTouchCancel: () => stop(false),
234
+ };
235
+ },
236
+ };
237
+ }
@@ -0,0 +1,49 @@
1
+ import type { TableGridBounds } from './table-grid.js';
2
+ import { type TableCellAddress, type TableCellRange, type TableClipboard, type TableColumnDefinition, type TableRowId, type TableRowsChange, type TableValue } from './table-model.js';
3
+ type Draft = {
4
+ cell: TableCellAddress;
5
+ original: TableValue;
6
+ value: TableValue;
7
+ error?: string;
8
+ };
9
+ type CellSelection = 'none' | 'single' | 'range';
10
+ interface EditingOptions<Row> {
11
+ rows: readonly Row[];
12
+ writeRows: (rows: readonly Row[], detail: TableRowsChange) => boolean;
13
+ rowId: (row: Row) => TableRowId;
14
+ columns: readonly TableColumnDefinition<Row>[];
15
+ displayedIds: readonly TableRowId[];
16
+ cellSelection: CellSelection;
17
+ cellRange?: TableCellRange | null;
18
+ defaultCellRange?: TableCellRange | null;
19
+ onCellRangeChange?: (range: TableCellRange | null) => void | false;
20
+ clipboard?: TableClipboard;
21
+ isCovered?: (row: number, column: number) => boolean;
22
+ owner: (cell: TableCellAddress) => TableCellAddress;
23
+ expandBounds?: (bounds: TableGridBounds) => TableGridBounds;
24
+ spanModel?: object;
25
+ }
26
+ export declare function useTableEditing<Row>({ rows, writeRows, rowId, columns, displayedIds, cellSelection, cellRange, defaultCellRange, onCellRangeChange, clipboard, isCovered, owner, expandBounds, spanModel, }: EditingOptions<Row>): {
27
+ bounds: TableGridBounds | null;
28
+ boundsFor: (next: TableCellRange | null) => TableGridBounds | null;
29
+ range: TableCellRange | null;
30
+ error: string;
31
+ draft: Draft | null;
32
+ extending: boolean;
33
+ setExtending: import("react").Dispatch<import("react").SetStateAction<boolean>>;
34
+ choose: (cell: TableCellAddress) => void;
35
+ select: (next: TableCellRange | null) => boolean;
36
+ begin: (requested: TableCellAddress) => boolean;
37
+ editable: (cell: TableCellAddress | undefined) => boolean;
38
+ updateDraft: (value: TableValue) => void;
39
+ cancel: () => void;
40
+ commit: () => boolean;
41
+ copySelection: () => Promise<string>;
42
+ pasteSelection: (text?: string) => Promise<boolean>;
43
+ fillSelection: () => boolean;
44
+ canUndo: boolean;
45
+ canRedo: boolean;
46
+ undo: () => boolean;
47
+ redo: () => boolean;
48
+ };
49
+ export {};
@@ -0,0 +1,278 @@
1
+ import { useMemo, useRef, useState } from 'react';
2
+ import { useTramaLocale, useTramaMessages } from '../localization.js';
3
+ import { numberInputFormat } from './number-format.js';
4
+ import { useControllableValue } from './state.js';
5
+ import { canEditTableCell, formatTableFields, getTableValue, parseTableFields, sameTableCell, tableRangeBounds, validateTableCell, } from './table-model.js';
6
+ export function useTableEditing({ rows, writeRows, rowId, columns, displayedIds, cellSelection, cellRange, defaultCellRange, onCellRangeChange, clipboard, isCovered, owner, expandBounds, spanModel, }) {
7
+ const messages = useTramaMessages();
8
+ const locale = useTramaLocale();
9
+ const numeric = useMemo(() => numberInputFormat(locale), [locale]);
10
+ const [range, setRange] = useControllableValue(cellRange, defaultCellRange ?? null);
11
+ const [draft, setDraft] = useState(null);
12
+ const [error, setError] = useState('');
13
+ const [extending, setExtending] = useState(false);
14
+ const [history, setHistory] = useState({ entries: [], index: 0 });
15
+ const byId = useMemo(() => new Map(rows.map((row) => [rowId(row), row])), [rowId, rows]);
16
+ const byColumn = useMemo(() => new Map(columns.map((column) => [column.id, column])), [columns]);
17
+ const bounds = boundsFor(range);
18
+ const mutation = useRef({ rows, range, columns, displayedIds, draft, spanModel });
19
+ mutation.current = { rows, range, columns, displayedIds, draft, spanModel };
20
+ function boundsFor(next) {
21
+ const raw = cellSelection === 'none' ? null : tableRangeBounds(next, displayedIds, columns);
22
+ return raw && (expandBounds?.(raw) ?? raw);
23
+ }
24
+ function locate(cell) {
25
+ const row = byId.get(cell.rowId);
26
+ const column = byColumn.get(cell.columnId);
27
+ return row === undefined || !column ? null : { row, column };
28
+ }
29
+ function editable(cell) {
30
+ const target = cell && locate(owner(cell));
31
+ return Boolean(target && canEditTableCell(target.row, target.column));
32
+ }
33
+ function select(next) {
34
+ if (cellSelection === 'none' || (next && !tableRangeBounds(next, displayedIds, columns)))
35
+ return false;
36
+ const effective = next && cellSelection === 'single' ? { anchor: next.focus, focus: next.focus } : next;
37
+ if (onCellRangeChange?.(effective) === false)
38
+ return false;
39
+ setRange(effective);
40
+ setError('');
41
+ return true;
42
+ }
43
+ function begin(requested) {
44
+ const cell = owner(requested);
45
+ if (draft && !sameTableCell(draft.cell, cell))
46
+ return false;
47
+ const target = locate(cell);
48
+ if (!target || !canEditTableCell(target.row, target.column))
49
+ return false;
50
+ const value = getTableValue(target.row, target.column);
51
+ setError('');
52
+ setDraft({ cell, value, original: value });
53
+ return true;
54
+ }
55
+ function choose(cell) {
56
+ if (draft)
57
+ return;
58
+ if (cellSelection === 'none')
59
+ begin(cell);
60
+ else {
61
+ const current = cellSelection === 'range' && extending && bounds && range;
62
+ if (select({ anchor: current ? current.anchor : cell, focus: cell }))
63
+ setExtending(false);
64
+ }
65
+ }
66
+ function validateAndWrite(proposals, reason, record) {
67
+ const updates = new Map();
68
+ const changes = [];
69
+ for (const { cell, value: raw, expected } of proposals) {
70
+ const original = locate(cell);
71
+ if (!original || !sameTableCell(cell, owner(cell)))
72
+ return messages.tableCellUnavailable;
73
+ const { column } = original;
74
+ const row = updates.get(cell.rowId) ?? original.row;
75
+ if (!canEditTableCell(row, column))
76
+ return messages.tableCellUnavailable;
77
+ const previous = getTableValue(row, column);
78
+ if (expected && !Object.is(previous, expected.value))
79
+ return messages.tableCellChanged;
80
+ if (Object.is(raw, previous))
81
+ continue;
82
+ const validated = expected && (reason === 'undo' || reason === 'redo')
83
+ ? { value: raw }
84
+ : validateTableCell(raw, column, row, messages, numeric);
85
+ if (!('value' in validated))
86
+ return validated.error;
87
+ const value = validated.value;
88
+ if (Object.is(previous, value))
89
+ continue;
90
+ const next = column.setValue ? column.setValue(row, value) : { ...row, [column.id]: value };
91
+ if (!Object.is(rowId(next), cell.rowId) || !Object.is(getTableValue(next, column), value))
92
+ return messages.tableEditRejected;
93
+ updates.set(cell.rowId, next);
94
+ changes.push({ ...cell, previous, value });
95
+ }
96
+ if (!changes.length)
97
+ return '';
98
+ const next = rows.map((row) => updates.get(rowId(row)) ?? row);
99
+ if (!writeRows(next, { reason, changes }))
100
+ return messages.tableEditRejected;
101
+ if (record) {
102
+ const entries = [...history.entries.slice(0, history.index), changes].slice(-50);
103
+ setHistory({ entries, index: entries.length });
104
+ }
105
+ return '';
106
+ }
107
+ function updateDraft(value) {
108
+ setDraft((current) => current && { ...current, value, error: undefined });
109
+ }
110
+ function commit() {
111
+ if (!draft)
112
+ return false;
113
+ const failure = validateAndWrite([{ cell: draft.cell, value: draft.value, expected: { value: draft.original } }], 'edit', true);
114
+ if (failure) {
115
+ setDraft({ ...draft, error: failure });
116
+ return false;
117
+ }
118
+ setDraft(null);
119
+ return true;
120
+ }
121
+ function valuesInRange() {
122
+ if (!bounds)
123
+ return [];
124
+ return displayedIds.slice(bounds.top, bounds.bottom + 1).map((id, rowIndex) => {
125
+ const row = byId.get(id);
126
+ return columns
127
+ .slice(bounds.start, bounds.end + 1)
128
+ .map((column, columnIndex) => row === undefined || isCovered?.(bounds.top + rowIndex, bounds.start + columnIndex)
129
+ ? undefined
130
+ : getTableValue(row, column));
131
+ });
132
+ }
133
+ async function copySelection() {
134
+ const values = valuesInRange();
135
+ if (!values.length)
136
+ return '';
137
+ const content = formatTableFields(values);
138
+ try {
139
+ await clipboard?.writeText?.(content);
140
+ setError('');
141
+ return content;
142
+ }
143
+ catch {
144
+ setError(messages.tableClipboardError);
145
+ return '';
146
+ }
147
+ }
148
+ function execute(proposals, reason, record = true) {
149
+ if (draft)
150
+ return false;
151
+ const failure = validateAndWrite(proposals, reason, record);
152
+ setError(failure);
153
+ return !failure;
154
+ }
155
+ async function pasteSelection(text) {
156
+ if (!bounds || draft)
157
+ return false;
158
+ const snapshot = mutation.current;
159
+ let input = text;
160
+ if (input === undefined) {
161
+ try {
162
+ input = await clipboard?.readText?.();
163
+ }
164
+ catch {
165
+ setError(messages.tableClipboardError);
166
+ return false;
167
+ }
168
+ const latest = mutation.current;
169
+ if (latest.draft !== snapshot.draft ||
170
+ latest.spanModel !== snapshot.spanModel ||
171
+ latest.rows !== snapshot.rows ||
172
+ latest.range !== snapshot.range ||
173
+ latest.columns.length !== snapshot.columns.length ||
174
+ latest.columns.some((column, i) => column.id !== snapshot.columns[i].id) ||
175
+ latest.displayedIds.length !== snapshot.displayedIds.length ||
176
+ latest.displayedIds.some((id, i) => id !== snapshot.displayedIds[i]))
177
+ return false;
178
+ }
179
+ if (input === undefined)
180
+ return false;
181
+ const matrix = parseTableFields(input);
182
+ if (!matrix ||
183
+ bounds.top + matrix.length > displayedIds.length ||
184
+ matrix.some((line) => bounds.start + line.length > columns.length)) {
185
+ setError(messages.tablePasteDoesNotFit);
186
+ return false;
187
+ }
188
+ const proposals = [];
189
+ for (const [r, line] of matrix.entries()) {
190
+ for (const [c, value] of line.entries()) {
191
+ const row = bounds.top + r;
192
+ const column = bounds.start + c;
193
+ if (isCovered?.(row, column)) {
194
+ if (!value)
195
+ continue;
196
+ setError(messages.tablePasteCoveredCell);
197
+ return false;
198
+ }
199
+ proposals.push({ cell: { rowId: displayedIds[row], columnId: columns[column].id }, value });
200
+ }
201
+ }
202
+ return execute(proposals, 'paste');
203
+ }
204
+ function fillSelection() {
205
+ if (!range || !bounds || (bounds.bottom === bounds.top && bounds.start === bounds.end))
206
+ return false;
207
+ const anchor = owner(range.anchor);
208
+ const source = locate(anchor);
209
+ if (!source)
210
+ return false;
211
+ const value = getTableValue(source.row, source.column);
212
+ const proposals = [];
213
+ for (let r = bounds.top; r <= bounds.bottom; r++) {
214
+ for (let c = bounds.start; c <= bounds.end; c++) {
215
+ if (isCovered?.(r, c))
216
+ continue;
217
+ const cell = { rowId: displayedIds[r], columnId: columns[c].id };
218
+ if (!sameTableCell(cell, anchor))
219
+ proposals.push({ cell, value });
220
+ }
221
+ }
222
+ return execute(proposals, 'fill');
223
+ }
224
+ function historyUsable(changes, undoing) {
225
+ return Boolean(changes?.every((change) => {
226
+ const target = locate(change);
227
+ return (target &&
228
+ sameTableCell(change, owner(change)) &&
229
+ canEditTableCell(target.row, target.column) &&
230
+ Object.is(getTableValue(target.row, target.column), undoing ? change.value : change.previous));
231
+ }));
232
+ }
233
+ function travel(undoing) {
234
+ const changes = history.entries[undoing ? history.index - 1 : history.index];
235
+ if (!changes || !historyUsable(changes, undoing))
236
+ return false;
237
+ const accept = execute(changes.map((change) => ({
238
+ cell: change,
239
+ value: undoing ? change.previous : change.value,
240
+ expected: { value: undoing ? change.value : change.previous },
241
+ })), undoing ? 'undo' : 'redo', false);
242
+ if (accept)
243
+ setHistory({ ...history, index: history.index + (undoing ? -1 : 1) });
244
+ return accept;
245
+ }
246
+ const cancel = () => setDraft(null);
247
+ const canUndo = !draft && historyUsable(history.entries[history.index - 1], true);
248
+ const canRedo = !draft && historyUsable(history.entries[history.index], false);
249
+ const activeCell = draft &&
250
+ (!displayedIds.includes(draft.cell.rowId) || !sameTableCell(draft.cell, owner(draft.cell)) || !editable(draft.cell))
251
+ ? null
252
+ : draft;
253
+ if (draft && !activeCell)
254
+ setDraft(null);
255
+ return {
256
+ bounds,
257
+ boundsFor,
258
+ range,
259
+ error,
260
+ draft: activeCell,
261
+ extending,
262
+ setExtending,
263
+ choose,
264
+ select,
265
+ begin,
266
+ editable,
267
+ updateDraft,
268
+ cancel,
269
+ commit,
270
+ copySelection,
271
+ pasteSelection,
272
+ fillSelection,
273
+ canUndo,
274
+ canRedo,
275
+ undo: () => travel(true),
276
+ redo: () => travel(false),
277
+ };
278
+ }
@@ -0,0 +1,11 @@
1
+ import type { TableColumnDefinition, TableValue } from './table-model.js';
2
+ export declare function TableEditor<Row>({ column, row, value, label, error, onChange, onSubmit, onCancel, }: {
3
+ column: TableColumnDefinition<Row>;
4
+ row: Row;
5
+ value: TableValue;
6
+ label: string;
7
+ error?: string;
8
+ onChange: (value: TableValue) => void;
9
+ onSubmit: () => void;
10
+ onCancel?: () => void;
11
+ }): string | number | bigint | boolean | Iterable<import("react").ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<import("react").ReactNode> | null | undefined> | import("react").JSX.Element | null | undefined;
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { DatePicker } from '../calendar.js';
3
+ import { Checkbox } from '../choices.js';
4
+ import { Input } from '../input.js';
5
+ import { Select } from '../select.js';
6
+ export function TableEditor({ column, row, value, label, error, onChange, onSubmit, onCancel, }) {
7
+ const editor = column.editor;
8
+ if (editor?.render)
9
+ return editor.render({ row, value, label, error, setValue: onChange });
10
+ if (editor?.type === 'boolean')
11
+ return _jsx(Checkbox, { checked: value === true, onCheckedChange: onChange, label: label, error: error });
12
+ if (editor?.type === 'select')
13
+ return (_jsx(Select, { options: editor.options ?? [], value: String(value ?? ''), onValueChange: (next) => {
14
+ if (typeof next === 'string')
15
+ onChange(next);
16
+ }, label: label, clearable: !editor.required, required: editor.required, error: error }));
17
+ if (editor?.type === 'date')
18
+ return (_jsx(DatePicker, { value: String(value ?? ''), onValueChange: (next) => {
19
+ if (typeof next === 'string')
20
+ onChange(next);
21
+ }, label: label, required: editor.required, error: error }));
22
+ return (_jsx(Input, { value: String(value ?? ''), accessibilityLabel: label, autoFocus: true, onChangeText: onChange, error: error, required: editor?.required, maxLength: editor?.maxLength, inputMode: editor?.type === 'number' ? 'decimal' : 'text', placeholder: editor?.type === 'time' ? 'HH:MM' : undefined, onSubmitEditing: onSubmit, onKeyPress: onCancel
23
+ ? (event) => {
24
+ if (event.nativeEvent.key === 'Escape') {
25
+ event.preventDefault();
26
+ event.stopPropagation();
27
+ onCancel();
28
+ }
29
+ }
30
+ : undefined }));
31
+ }
@@ -0,0 +1,44 @@
1
+ import type { KeyboardCommandEvent } from './keyboard.js';
2
+ export interface TableGridPoint {
3
+ row: number;
4
+ column: number;
5
+ }
6
+ export interface TableGridSpan extends TableGridPoint {
7
+ rows: number;
8
+ columns: number;
9
+ }
10
+ export interface TableGridBounds {
11
+ top: number;
12
+ bottom: number;
13
+ start: number;
14
+ end: number;
15
+ }
16
+ export type TableKeyboardEvent = KeyboardCommandEvent;
17
+ export declare function tableGridSpans(rowCount: number, columnPins: readonly (string | undefined)[], rowEnds: readonly number[], requested: (point: TableGridPoint) => {
18
+ rows?: number;
19
+ columns?: number;
20
+ }): {
21
+ close(bounds: TableGridBounds): {
22
+ top: number;
23
+ bottom: number;
24
+ start: number;
25
+ end: number;
26
+ };
27
+ owner(point: TableGridPoint): TableGridSpan;
28
+ row(index: number): Map<number, TableGridSpan> | undefined;
29
+ rowEnd(index: number): number;
30
+ };
31
+ export declare function closeGridBounds(bounds: TableGridBounds, anchors: readonly TableGridSpan[]): {
32
+ top: number;
33
+ bottom: number;
34
+ start: number;
35
+ end: number;
36
+ };
37
+ export declare function gridTravel(event: TableKeyboardEvent, point: TableGridPoint, current: TableGridSpan, bounds: {
38
+ rows: number;
39
+ columns: number;
40
+ pageRows: number;
41
+ rtl: boolean;
42
+ }): TableGridPoint | null;
43
+ export declare function gridSpanIntersects(span: TableGridSpan, bounds: TableGridBounds): boolean;
44
+ export declare function gridColumnAt(x: number, width: number, scroll: number, offsets: readonly number[], pinnedStart: number, pinnedEnd: number, rtl: boolean): number;