@object-ui/fields 11.3.0 → 11.5.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 (50) hide show
  1. package/CHANGELOG.md +155 -0
  2. package/dist/FieldEditWidget.d.ts +8 -0
  3. package/dist/FieldEditWidget.test.d.ts +8 -0
  4. package/dist/__tests__/DateCellRenderer.test.d.ts +0 -0
  5. package/dist/__tests__/NumberCellRenderer.test.d.ts +0 -0
  6. package/dist/__tests__/lookupCellReferenceKey.test.d.ts +0 -0
  7. package/dist/index.cjs +4 -4
  8. package/dist/index.d.ts +17 -2
  9. package/dist/index.js +2015 -1346
  10. package/dist/widgets/LookupField.dependsOn.test.d.ts +8 -0
  11. package/dist/widgets/PeoplePicker.d.ts +63 -0
  12. package/dist/widgets/PeoplePicker.test.d.ts +8 -0
  13. package/dist/widgets/PeoplePickerComponents.test.d.ts +8 -0
  14. package/dist/widgets/PersonRow.d.ts +23 -0
  15. package/dist/widgets/RecordPickerDialog.d.ts +9 -1
  16. package/dist/widgets/SelectionTray.d.ts +27 -0
  17. package/dist/widgets/UserField.d.ts +0 -13
  18. package/dist/widgets/UserFieldPicker.test.d.ts +8 -0
  19. package/dist/widgets/deriveLookupColumns.d.ts +12 -4
  20. package/dist/widgets/personDisplay.d.ts +37 -0
  21. package/dist/widgets/personDisplay.test.d.ts +8 -0
  22. package/dist/widgets/useRecordQuery.d.ts +87 -0
  23. package/dist/widgets/useRecordQuery.test.d.ts +8 -0
  24. package/package.json +7 -7
  25. package/src/FieldEditWidget.test.ts +61 -0
  26. package/src/FieldEditWidget.tsx +56 -1
  27. package/src/__tests__/DateCellRenderer.test.tsx +50 -0
  28. package/src/__tests__/NumberCellRenderer.test.tsx +60 -0
  29. package/src/__tests__/lookupCellReferenceKey.test.tsx +57 -0
  30. package/src/complex-widgets.test.tsx +241 -0
  31. package/src/index.tsx +61 -18
  32. package/src/widgets/GridField.test.tsx +14 -0
  33. package/src/widgets/GridField.tsx +7 -8
  34. package/src/widgets/LookupField.dependsOn.test.tsx +233 -0
  35. package/src/widgets/LookupField.tsx +365 -190
  36. package/src/widgets/NumberField.tsx +4 -2
  37. package/src/widgets/PeoplePicker.test.tsx +334 -0
  38. package/src/widgets/PeoplePicker.tsx +552 -0
  39. package/src/widgets/PeoplePickerComponents.test.tsx +104 -0
  40. package/src/widgets/PersonRow.tsx +117 -0
  41. package/src/widgets/RecordPickerDialog.tsx +58 -129
  42. package/src/widgets/SelectionTray.tsx +110 -0
  43. package/src/widgets/UserField.tsx +20 -1
  44. package/src/widgets/UserFieldPicker.test.tsx +78 -0
  45. package/src/widgets/deriveLookupColumns.test.ts +25 -0
  46. package/src/widgets/deriveLookupColumns.ts +24 -10
  47. package/src/widgets/personDisplay.test.ts +113 -0
  48. package/src/widgets/personDisplay.ts +92 -0
  49. package/src/widgets/useRecordQuery.test.ts +174 -0
  50. package/src/widgets/useRecordQuery.ts +264 -0
@@ -13,7 +13,9 @@ export function NumberField({ value, onChange, field, readonly, ...props }: Fiel
13
13
  }
14
14
 
15
15
  const numberField = (field || (props as any).schema) as NumberFieldMetadata;
16
- const precision = numberField?.precision;
16
+ // Step follows `scale` (decimal places), not `precision` (total digit count):
17
+ // a decimal(10, 0) column has 0 decimal places, so the input should step by 1.
18
+ const scale = numberField?.scale;
17
19
 
18
20
  // Filter out non-DOM props
19
21
  const { inputType, ...domProps } = props as any;
@@ -29,7 +31,7 @@ export function NumberField({ value, onChange, field, readonly, ...props }: Fiel
29
31
  }}
30
32
  placeholder={numberField?.placeholder}
31
33
  disabled={readonly || domProps.disabled}
32
- step={precision ? Math.pow(10, -precision) : 'any'}
34
+ step={scale ? Math.pow(10, -scale) : 'any'}
33
35
  />
34
36
  );
35
37
  }
@@ -0,0 +1,334 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
10
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
11
+ import { PeoplePicker } from './PeoplePicker';
12
+ import { pushRecentLookupId } from './recentLookups';
13
+
14
+ const users = [
15
+ { id: 'u1', name: 'Amy Lin', email: 'amy@x.io', primary_business_unit_id: { name: 'Sales' } },
16
+ { id: 'u2', name: 'Bob Wu', email: 'bob@x.io' },
17
+ { id: 'u3', name: 'Cara Xu', email: 'cara@x.io' },
18
+ ];
19
+
20
+ function makeDataSource() {
21
+ const find = vi.fn(async (_obj: string, params: any) => {
22
+ const idIn = params?.$filter?.id?.$in as any[] | undefined;
23
+ if (idIn) {
24
+ const set = new Set(idIn.map(String));
25
+ const data = users.filter(u => set.has(String(u.id)));
26
+ return { data, total: data.length };
27
+ }
28
+ const search: string | undefined = params?.$search;
29
+ const data = search
30
+ ? users.filter(u => u.name.toLowerCase().includes(search.toLowerCase()))
31
+ : users;
32
+ return { data, total: data.length };
33
+ });
34
+ return { find } as any;
35
+ }
36
+
37
+ const baseProps = {
38
+ open: true,
39
+ objectName: 'sys_user',
40
+ subtitleFields: ['primary_business_unit_id.name', 'email'],
41
+ };
42
+
43
+ beforeEach(() => {
44
+ // jsdom has no matchMedia; useIsMobile needs it. Default to desktop width.
45
+ Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1280 });
46
+ window.matchMedia = ((query: string) => ({
47
+ matches: window.innerWidth < 768,
48
+ media: query,
49
+ onchange: null,
50
+ addEventListener: () => {},
51
+ removeEventListener: () => {},
52
+ addListener: () => {},
53
+ removeListener: () => {},
54
+ dispatchEvent: () => false,
55
+ })) as any;
56
+ try {
57
+ window.localStorage.clear();
58
+ } catch {
59
+ /* ignore */
60
+ }
61
+ });
62
+
63
+ describe('PeoplePicker', () => {
64
+ it('lists candidate rows on open and filters by $search', async () => {
65
+ const ds = makeDataSource();
66
+ render(
67
+ <PeoplePicker
68
+ {...baseProps}
69
+ dataSource={ds}
70
+ onOpenChange={vi.fn()}
71
+ onSelect={vi.fn()}
72
+ />,
73
+ );
74
+
75
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
76
+ expect(screen.getByText('Sales · amy@x.io')).toBeTruthy();
77
+ expect(screen.getAllByTestId('person-row')).toHaveLength(3);
78
+
79
+ fireEvent.change(screen.getByTestId('people-picker-search'), { target: { value: 'amy' } });
80
+ await waitFor(() => expect(screen.getAllByTestId('person-row')).toHaveLength(1));
81
+ // Relation subtitle (primary_business_unit_id.name) auto-adds $expand.
82
+ expect(ds.find).toHaveBeenLastCalledWith('sys_user', {
83
+ $top: 25,
84
+ $search: 'amy',
85
+ $expand: ['primary_business_unit_id'],
86
+ });
87
+ });
88
+
89
+ it('applies base candidate filters (banned != true)', async () => {
90
+ const ds = makeDataSource();
91
+ render(
92
+ <PeoplePicker
93
+ {...baseProps}
94
+ dataSource={ds}
95
+ lookupFilters={[{ field: 'banned', operator: 'ne', value: true }]}
96
+ onOpenChange={vi.fn()}
97
+ onSelect={vi.fn()}
98
+ />,
99
+ );
100
+ await waitFor(() =>
101
+ expect(ds.find).toHaveBeenCalledWith('sys_user', {
102
+ $top: 25,
103
+ $filter: { banned: { $ne: true } },
104
+ $expand: ['primary_business_unit_id'],
105
+ }),
106
+ );
107
+ });
108
+
109
+ it('single-select commits immediately and closes', async () => {
110
+ const ds = makeDataSource();
111
+ const onSelect = vi.fn();
112
+ const onSelectRecords = vi.fn();
113
+ const onOpenChange = vi.fn();
114
+ render(
115
+ <PeoplePicker
116
+ {...baseProps}
117
+ dataSource={ds}
118
+ onSelect={onSelect}
119
+ onSelectRecords={onSelectRecords}
120
+ onOpenChange={onOpenChange}
121
+ />,
122
+ );
123
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
124
+
125
+ fireEvent.click(screen.getByText('Amy Lin'));
126
+ expect(onSelect).toHaveBeenCalledWith('u1');
127
+ expect(onSelectRecords).toHaveBeenCalledWith([users[0]]);
128
+ expect(onOpenChange).toHaveBeenCalledWith(false);
129
+ });
130
+
131
+ it('multi-select echoes chips and confirms ids + records', async () => {
132
+ const ds = makeDataSource();
133
+ const onSelect = vi.fn();
134
+ const onSelectRecords = vi.fn();
135
+ render(
136
+ <PeoplePicker
137
+ {...baseProps}
138
+ multiple
139
+ dataSource={ds}
140
+ onSelect={onSelect}
141
+ onSelectRecords={onSelectRecords}
142
+ onOpenChange={vi.fn()}
143
+ />,
144
+ );
145
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
146
+
147
+ fireEvent.click(screen.getByText('Amy Lin'));
148
+ fireEvent.click(screen.getByText('Bob Wu'));
149
+ await waitFor(() => expect(screen.getAllByTestId('selection-chip')).toHaveLength(2));
150
+
151
+ fireEvent.click(screen.getByText('Confirm'));
152
+ expect(onSelect).toHaveBeenCalledWith(['u1', 'u2']);
153
+ expect(onSelectRecords).toHaveBeenCalledWith([users[0], users[1]]);
154
+ });
155
+
156
+ it('surfaces recent contacts in their own section when idle', async () => {
157
+ pushRecentLookupId('sys_user', 'u3');
158
+ const ds = makeDataSource();
159
+ render(
160
+ <PeoplePicker
161
+ {...baseProps}
162
+ dataSource={ds}
163
+ onOpenChange={vi.fn()}
164
+ onSelect={vi.fn()}
165
+ />,
166
+ );
167
+ await waitFor(() => expect(screen.getByText('Recently used')).toBeTruthy());
168
+ // Cara appears under "Recently used" and is de-duped out of the main list.
169
+ expect(screen.getByText('Cara Xu')).toBeTruthy();
170
+ expect(screen.getByText('All results')).toBeTruthy();
171
+ });
172
+
173
+ it('keyboard: ArrowDown then Enter commits the active row (single)', async () => {
174
+ const ds = makeDataSource();
175
+ const onSelect = vi.fn();
176
+ render(
177
+ <PeoplePicker {...baseProps} dataSource={ds} onSelect={onSelect} onSelectRecords={vi.fn()} onOpenChange={vi.fn()} />,
178
+ );
179
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
180
+ const search = screen.getByTestId('people-picker-search');
181
+ fireEvent.keyDown(search, { key: 'ArrowDown' });
182
+ fireEvent.keyDown(search, { key: 'Enter' });
183
+ expect(onSelect).toHaveBeenCalledWith('u1');
184
+ });
185
+
186
+ it('keyboard: Backspace on empty search removes the last chip (multi)', async () => {
187
+ const ds = makeDataSource();
188
+ render(
189
+ <PeoplePicker {...baseProps} multiple dataSource={ds} onSelect={vi.fn()} onSelectRecords={vi.fn()} onOpenChange={vi.fn()} />,
190
+ );
191
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
192
+ fireEvent.click(screen.getByText('Amy Lin'));
193
+ fireEvent.click(screen.getByText('Bob Wu'));
194
+ await waitFor(() => expect(screen.getAllByTestId('selection-chip')).toHaveLength(2));
195
+ fireEvent.keyDown(screen.getByTestId('people-picker-search'), { key: 'Backspace' });
196
+ await waitFor(() => expect(screen.getAllByTestId('selection-chip')).toHaveLength(1));
197
+ });
198
+
199
+ it('multi: Clear all empties the tray', async () => {
200
+ const ds = makeDataSource();
201
+ render(
202
+ <PeoplePicker {...baseProps} multiple dataSource={ds} onSelect={vi.fn()} onSelectRecords={vi.fn()} onOpenChange={vi.fn()} />,
203
+ );
204
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
205
+ fireEvent.click(screen.getByText('Amy Lin'));
206
+ fireEvent.click(screen.getByText('Bob Wu'));
207
+ await waitFor(() => expect(screen.getAllByTestId('selection-chip')).toHaveLength(2));
208
+ fireEvent.click(screen.getByTestId('selection-clear'));
209
+ await waitFor(() => expect(screen.queryAllByTestId('selection-chip')).toHaveLength(0));
210
+ });
211
+
212
+ it('highlights the matched term in rows while searching', async () => {
213
+ const ds = makeDataSource();
214
+ // Dialog content portals onto document.body, so query the document, not the
215
+ // render container.
216
+ render(<PeoplePicker {...baseProps} dataSource={ds} onSelect={vi.fn()} onOpenChange={vi.fn()} />);
217
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
218
+ fireEvent.change(screen.getByTestId('people-picker-search'), { target: { value: 'amy' } });
219
+ await waitFor(() => expect(document.querySelector('mark')).toBeTruthy());
220
+ expect(document.querySelector('mark')?.textContent?.toLowerCase()).toContain('am');
221
+ });
222
+
223
+ it('shows an error with a retry that refetches', async () => {
224
+ let calls = 0;
225
+ const ds = {
226
+ find: vi.fn(async () => {
227
+ calls += 1;
228
+ if (calls === 1) throw new Error('boom');
229
+ return { data: [users[0]], total: 1 };
230
+ }),
231
+ } as any;
232
+ render(
233
+ <PeoplePicker {...baseProps} dataSource={ds} onSelect={vi.fn()} onOpenChange={vi.fn()} />,
234
+ );
235
+ await waitFor(() => expect(screen.getByText('boom')).toBeTruthy());
236
+ fireEvent.click(screen.getByText('Retry'));
237
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
238
+ });
239
+
240
+ it('renders a Dialog on desktop and a bottom Sheet on mobile', async () => {
241
+ const ds = makeDataSource();
242
+ const { unmount } = render(
243
+ <PeoplePicker {...baseProps} dataSource={ds} onSelect={vi.fn()} onOpenChange={vi.fn()} />,
244
+ );
245
+ await waitFor(() => expect(screen.getByTestId('people-picker-dialog')).toBeTruthy());
246
+ expect(screen.queryByTestId('people-picker-sheet')).toBeNull();
247
+ unmount();
248
+
249
+ Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 375 });
250
+ render(<PeoplePicker {...baseProps} dataSource={ds} onSelect={vi.fn()} onOpenChange={vi.fn()} />);
251
+ await waitFor(() => expect(screen.getByTestId('people-picker-sheet')).toBeTruthy());
252
+ expect(screen.queryByTestId('people-picker-dialog')).toBeNull();
253
+ });
254
+
255
+ it('seeds the tray from an existing multi value (edit mode)', async () => {
256
+ const ds = makeDataSource();
257
+ render(
258
+ <PeoplePicker
259
+ {...baseProps}
260
+ multiple
261
+ dataSource={ds}
262
+ value={['u1']}
263
+ onSelect={vi.fn()}
264
+ onSelectRecords={vi.fn()}
265
+ onOpenChange={vi.fn()}
266
+ />,
267
+ );
268
+ await waitFor(() => expect(screen.getAllByTestId('selection-chip')).toHaveLength(1));
269
+ expect(screen.getByTestId('selection-tray').textContent).toContain('Amy Lin');
270
+ });
271
+
272
+ it('inline: renders an anchored popover, not a modal dialog', async () => {
273
+ const ds = makeDataSource();
274
+ render(
275
+ <PeoplePicker
276
+ {...baseProps}
277
+ inline
278
+ trigger={<button data-testid="inline-trigger">open</button>}
279
+ dataSource={ds}
280
+ onOpenChange={vi.fn()}
281
+ onSelect={vi.fn()}
282
+ />,
283
+ );
284
+ await waitFor(() => expect(screen.getByTestId('people-picker-inline')).toBeTruthy());
285
+ expect(screen.queryByTestId('people-picker-dialog')).toBeNull();
286
+ expect(screen.getByTestId('inline-trigger')).toBeTruthy();
287
+ });
288
+
289
+ it('inline multi: toggling a row commits live, with no confirm button', async () => {
290
+ const ds = makeDataSource();
291
+ const onSelect = vi.fn();
292
+ render(
293
+ <PeoplePicker
294
+ {...baseProps}
295
+ inline
296
+ multiple
297
+ value={[]}
298
+ dataSource={ds}
299
+ trigger={<button data-testid="inline-trigger" />}
300
+ onOpenChange={vi.fn()}
301
+ onSelect={onSelect}
302
+ onSelectRecords={vi.fn()}
303
+ />,
304
+ );
305
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
306
+ // Inline has no staging tray / confirm — selection lives in the field chips.
307
+ expect(screen.queryByText('Confirm')).toBeNull();
308
+ fireEvent.click(screen.getByText('Amy Lin'));
309
+ expect(onSelect).toHaveBeenLastCalledWith(['u1']);
310
+ fireEvent.click(screen.getByText('Bob Wu'));
311
+ expect(onSelect).toHaveBeenLastCalledWith(['u1', 'u2']);
312
+ });
313
+
314
+ it('inline single: clicking a row commits and closes', async () => {
315
+ const ds = makeDataSource();
316
+ const onSelect = vi.fn();
317
+ const onOpenChange = vi.fn();
318
+ render(
319
+ <PeoplePicker
320
+ {...baseProps}
321
+ inline
322
+ dataSource={ds}
323
+ trigger={<button data-testid="inline-trigger" />}
324
+ onOpenChange={onOpenChange}
325
+ onSelect={onSelect}
326
+ onSelectRecords={vi.fn()}
327
+ />,
328
+ );
329
+ await waitFor(() => expect(screen.getByText('Amy Lin')).toBeTruthy());
330
+ fireEvent.click(screen.getByText('Amy Lin'));
331
+ expect(onSelect).toHaveBeenCalledWith('u1');
332
+ expect(onOpenChange).toHaveBeenCalledWith(false);
333
+ });
334
+ });