@object-ui/core 3.0.3 → 3.1.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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/actions/ActionEngine.d.ts +98 -0
- package/dist/actions/ActionEngine.js +222 -0
- package/dist/actions/UndoManager.d.ts +80 -0
- package/dist/actions/UndoManager.js +193 -0
- package/dist/actions/index.d.ts +2 -0
- package/dist/actions/index.js +2 -0
- package/dist/adapters/ApiDataSource.d.ts +2 -1
- package/dist/adapters/ApiDataSource.js +25 -0
- package/dist/adapters/ValueDataSource.d.ts +2 -1
- package/dist/adapters/ValueDataSource.js +99 -3
- package/dist/data-scope/ViewDataProvider.d.ts +143 -0
- package/dist/data-scope/ViewDataProvider.js +153 -0
- package/dist/data-scope/index.d.ts +1 -0
- package/dist/data-scope/index.js +1 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +7 -0
- package/dist/evaluator/ExpressionEvaluator.js +19 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/protocols/DndProtocol.d.ts +84 -0
- package/dist/protocols/DndProtocol.js +113 -0
- package/dist/protocols/KeyboardProtocol.d.ts +93 -0
- package/dist/protocols/KeyboardProtocol.js +108 -0
- package/dist/protocols/NotificationProtocol.d.ts +71 -0
- package/dist/protocols/NotificationProtocol.js +99 -0
- package/dist/protocols/ResponsiveProtocol.d.ts +73 -0
- package/dist/protocols/ResponsiveProtocol.js +158 -0
- package/dist/protocols/SharingProtocol.d.ts +71 -0
- package/dist/protocols/SharingProtocol.js +124 -0
- package/dist/protocols/index.d.ts +12 -0
- package/dist/protocols/index.js +12 -0
- package/dist/utils/debug-collector.d.ts +59 -0
- package/dist/utils/debug-collector.js +73 -0
- package/dist/utils/debug.d.ts +37 -2
- package/dist/utils/debug.js +62 -3
- package/dist/utils/expand-fields.d.ts +40 -0
- package/dist/utils/expand-fields.js +68 -0
- package/dist/utils/extract-records.d.ts +16 -0
- package/dist/utils/extract-records.js +32 -0
- package/dist/utils/normalize-quick-filter.d.ts +29 -0
- package/dist/utils/normalize-quick-filter.js +66 -0
- package/package.json +3 -3
- package/src/__tests__/protocols/DndProtocol.test.ts +186 -0
- package/src/__tests__/protocols/KeyboardProtocol.test.ts +177 -0
- package/src/__tests__/protocols/NotificationProtocol.test.ts +142 -0
- package/src/__tests__/protocols/ResponsiveProtocol.test.ts +176 -0
- package/src/__tests__/protocols/SharingProtocol.test.ts +188 -0
- package/src/actions/ActionEngine.ts +268 -0
- package/src/actions/UndoManager.ts +215 -0
- package/src/actions/__tests__/ActionEngine.test.ts +206 -0
- package/src/actions/__tests__/UndoManager.test.ts +320 -0
- package/src/actions/index.ts +2 -0
- package/src/adapters/ApiDataSource.ts +27 -0
- package/src/adapters/ValueDataSource.ts +109 -3
- package/src/adapters/__tests__/ValueDataSource.test.ts +147 -0
- package/src/data-scope/ViewDataProvider.ts +282 -0
- package/src/data-scope/__tests__/ViewDataProvider.test.ts +270 -0
- package/src/data-scope/index.ts +8 -0
- package/src/evaluator/ExpressionEvaluator.ts +22 -0
- package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +31 -1
- package/src/index.ts +5 -0
- package/src/protocols/DndProtocol.ts +184 -0
- package/src/protocols/KeyboardProtocol.ts +185 -0
- package/src/protocols/NotificationProtocol.ts +159 -0
- package/src/protocols/ResponsiveProtocol.ts +210 -0
- package/src/protocols/SharingProtocol.ts +185 -0
- package/src/protocols/index.ts +13 -0
- package/src/utils/__tests__/debug-collector.test.ts +102 -0
- package/src/utils/__tests__/debug.test.ts +52 -1
- package/src/utils/__tests__/expand-fields.test.ts +120 -0
- package/src/utils/__tests__/extract-records.test.ts +50 -0
- package/src/utils/__tests__/normalize-quick-filter.test.ts +123 -0
- package/src/utils/debug-collector.ts +100 -0
- package/src/utils/debug.ts +87 -6
- package/src/utils/expand-fields.ts +76 -0
- package/src/utils/extract-records.ts +33 -0
- package/src/utils/normalize-quick-filter.ts +78 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — expand-fields utility
|
|
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
|
+
* Build an array of field names that should be included in `$expand`
|
|
10
|
+
* when fetching data. This scans the given object schema fields
|
|
11
|
+
* (and optional column configuration) for `lookup` and `master_detail`
|
|
12
|
+
* field types, so the backend (e.g. objectql) returns expanded objects
|
|
13
|
+
* instead of raw foreign-key IDs.
|
|
14
|
+
*
|
|
15
|
+
* @param schemaFields - Object map of field metadata from `getObjectSchema()`,
|
|
16
|
+
* e.g. `{ account: { type: 'lookup', reference_to: 'accounts' }, ... }`.
|
|
17
|
+
* @param columns - Optional explicit column list. When provided, only
|
|
18
|
+
* lookup/master_detail fields that appear in `columns` are expanded.
|
|
19
|
+
* Accepts `string[]` or `ListColumn[]` (objects with a `field` property).
|
|
20
|
+
* @returns Array of field names to pass as `$expand`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const fields = {
|
|
25
|
+
* name: { type: 'text' },
|
|
26
|
+
* account: { type: 'lookup', reference_to: 'accounts' },
|
|
27
|
+
* parent: { type: 'master_detail', reference_to: 'contacts' },
|
|
28
|
+
* };
|
|
29
|
+
* buildExpandFields(fields);
|
|
30
|
+
* // → ['account', 'parent']
|
|
31
|
+
*
|
|
32
|
+
* buildExpandFields(fields, ['name', 'account']);
|
|
33
|
+
* // → ['account']
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function buildExpandFields(schemaFields, columns) {
|
|
37
|
+
if (!schemaFields || typeof schemaFields !== 'object') {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
// Collect all lookup / master_detail field names from the schema
|
|
41
|
+
const lookupFieldNames = [];
|
|
42
|
+
for (const [fieldName, fieldDef] of Object.entries(schemaFields)) {
|
|
43
|
+
if (fieldDef &&
|
|
44
|
+
typeof fieldDef === 'object' &&
|
|
45
|
+
(fieldDef.type === 'lookup' || fieldDef.type === 'master_detail')) {
|
|
46
|
+
lookupFieldNames.push(fieldName);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (lookupFieldNames.length === 0) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
// When columns are provided, restrict expansion to visible columns only
|
|
53
|
+
if (columns && Array.isArray(columns) && columns.length > 0) {
|
|
54
|
+
const columnFieldNames = new Set();
|
|
55
|
+
for (const col of columns) {
|
|
56
|
+
if (typeof col === 'string') {
|
|
57
|
+
columnFieldNames.add(col);
|
|
58
|
+
}
|
|
59
|
+
else if (col && typeof col === 'object') {
|
|
60
|
+
const name = col.field ?? col.name ?? col.fieldName;
|
|
61
|
+
if (name)
|
|
62
|
+
columnFieldNames.add(name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return lookupFieldNames.filter((f) => columnFieldNames.has(f));
|
|
66
|
+
}
|
|
67
|
+
return lookupFieldNames;
|
|
68
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
* Extract an array of records from various API response formats.
|
|
10
|
+
* Supports: raw array, { records: [] }, { data: [] }, { value: [] }.
|
|
11
|
+
*
|
|
12
|
+
* This utility normalises the different shapes returned by ObjectStack,
|
|
13
|
+
* OData, and MSW mock endpoints so that every data-fetching component
|
|
14
|
+
* can rely on a single extraction path.
|
|
15
|
+
*/
|
|
16
|
+
export declare function extractRecords(results: unknown): any[];
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
* Extract an array of records from various API response formats.
|
|
10
|
+
* Supports: raw array, { records: [] }, { data: [] }, { value: [] }.
|
|
11
|
+
*
|
|
12
|
+
* This utility normalises the different shapes returned by ObjectStack,
|
|
13
|
+
* OData, and MSW mock endpoints so that every data-fetching component
|
|
14
|
+
* can rely on a single extraction path.
|
|
15
|
+
*/
|
|
16
|
+
export function extractRecords(results) {
|
|
17
|
+
if (Array.isArray(results)) {
|
|
18
|
+
return results;
|
|
19
|
+
}
|
|
20
|
+
if (results && typeof results === 'object') {
|
|
21
|
+
if (Array.isArray(results.records)) {
|
|
22
|
+
return results.records;
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(results.data)) {
|
|
25
|
+
return results.data;
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(results.value)) {
|
|
28
|
+
return results.value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
* QuickFilter Normalization Utility
|
|
10
|
+
*
|
|
11
|
+
* Adapter layer that converts between two QuickFilter formats:
|
|
12
|
+
* - Spec format: { field, operator, value }
|
|
13
|
+
* - ObjectUI format: { id, label, filters[], icon?, defaultActive? }
|
|
14
|
+
*
|
|
15
|
+
* Both formats are accepted; spec-format items are auto-converted to ObjectUI format.
|
|
16
|
+
*/
|
|
17
|
+
import type { QuickFilterItem, ObjectUIQuickFilterItem } from '@object-ui/types';
|
|
18
|
+
/** Normalized ObjectUI QuickFilter shape (output of normalizeQuickFilter) */
|
|
19
|
+
export type NormalizedQuickFilter = ObjectUIQuickFilterItem;
|
|
20
|
+
/**
|
|
21
|
+
* Normalize a single QuickFilter item.
|
|
22
|
+
* - If it's already in ObjectUI format (has id + label + filters), return as-is.
|
|
23
|
+
* - If it's in Spec format (has field + operator), convert to ObjectUI format.
|
|
24
|
+
*/
|
|
25
|
+
export declare function normalizeQuickFilter(item: QuickFilterItem): NormalizedQuickFilter;
|
|
26
|
+
/**
|
|
27
|
+
* Normalize an array of QuickFilter items (mixed formats accepted).
|
|
28
|
+
*/
|
|
29
|
+
export declare function normalizeQuickFilters(items: QuickFilterItem[] | undefined): NormalizedQuickFilter[] | undefined;
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
* Map a human-readable / spec operator to the ObjectStack AST operator.
|
|
10
|
+
*/
|
|
11
|
+
function mapSpecOperator(op) {
|
|
12
|
+
switch (op) {
|
|
13
|
+
case 'equals':
|
|
14
|
+
case 'eq': return '=';
|
|
15
|
+
case 'notEquals':
|
|
16
|
+
case 'ne':
|
|
17
|
+
case 'neq': return '!=';
|
|
18
|
+
case 'contains': return 'contains';
|
|
19
|
+
case 'notContains': return 'notcontains';
|
|
20
|
+
case 'greaterThan':
|
|
21
|
+
case 'gt': return '>';
|
|
22
|
+
case 'greaterOrEqual':
|
|
23
|
+
case 'gte': return '>=';
|
|
24
|
+
case 'lessThan':
|
|
25
|
+
case 'lt': return '<';
|
|
26
|
+
case 'lessOrEqual':
|
|
27
|
+
case 'lte': return '<=';
|
|
28
|
+
case 'in': return 'in';
|
|
29
|
+
case 'notIn': return 'not in';
|
|
30
|
+
case 'before': return '<';
|
|
31
|
+
case 'after': return '>';
|
|
32
|
+
default: return op;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Normalize a single QuickFilter item.
|
|
37
|
+
* - If it's already in ObjectUI format (has id + label + filters), return as-is.
|
|
38
|
+
* - If it's in Spec format (has field + operator), convert to ObjectUI format.
|
|
39
|
+
*/
|
|
40
|
+
export function normalizeQuickFilter(item) {
|
|
41
|
+
// Already in ObjectUI format
|
|
42
|
+
if ('id' in item && 'filters' in item && item.label && Array.isArray(item.filters)) {
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
// Spec format: { field, operator, value }
|
|
46
|
+
if ('field' in item && 'operator' in item) {
|
|
47
|
+
const op = mapSpecOperator(item.operator);
|
|
48
|
+
return {
|
|
49
|
+
id: `${item.field}-${item.operator}-${String(item.value ?? '')}`,
|
|
50
|
+
label: item.label || `${item.field} ${item.operator} ${String(item.value ?? '')}`,
|
|
51
|
+
filters: [[item.field, op, item.value]],
|
|
52
|
+
icon: item.icon,
|
|
53
|
+
defaultActive: item.defaultActive,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// Unknown format — return as-is
|
|
57
|
+
return item;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Normalize an array of QuickFilter items (mixed formats accepted).
|
|
61
|
+
*/
|
|
62
|
+
export function normalizeQuickFilters(items) {
|
|
63
|
+
if (!items || items.length === 0)
|
|
64
|
+
return undefined;
|
|
65
|
+
return items.map(normalizeQuickFilter);
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@object-ui/core",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@objectstack/spec": "^3.0
|
|
27
|
+
"@objectstack/spec": "^3.2.0",
|
|
28
28
|
"lodash": "^4.17.23",
|
|
29
29
|
"zod": "^4.3.6",
|
|
30
|
-
"@object-ui/types": "3.0
|
|
30
|
+
"@object-ui/types": "3.1.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"typescript": "^5.9.3",
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import type { DndConfig, DragItem, DropZone, DragConstraint } from '@object-ui/types';
|
|
3
|
+
import {
|
|
4
|
+
resolveDndConfig,
|
|
5
|
+
createDragItemProps,
|
|
6
|
+
createDropZoneProps,
|
|
7
|
+
resolveDragConstraints,
|
|
8
|
+
} from '../../protocols/DndProtocol';
|
|
9
|
+
|
|
10
|
+
describe('DndProtocol', () => {
|
|
11
|
+
// ==========================================================================
|
|
12
|
+
// resolveDndConfig
|
|
13
|
+
// ==========================================================================
|
|
14
|
+
describe('resolveDndConfig', () => {
|
|
15
|
+
it('should apply all defaults for minimal config', () => {
|
|
16
|
+
const config = {} as DndConfig;
|
|
17
|
+
const resolved = resolveDndConfig(config);
|
|
18
|
+
|
|
19
|
+
expect(resolved.enabled).toBe(false);
|
|
20
|
+
expect(resolved.sortable).toBe(false);
|
|
21
|
+
expect(resolved.autoScroll).toBe(true);
|
|
22
|
+
expect(resolved.touchDelay).toBe(200);
|
|
23
|
+
expect(resolved.dragItem).toBeUndefined();
|
|
24
|
+
expect(resolved.dropZone).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should preserve explicit values', () => {
|
|
28
|
+
const config = {
|
|
29
|
+
enabled: true,
|
|
30
|
+
sortable: true,
|
|
31
|
+
autoScroll: false,
|
|
32
|
+
touchDelay: 500,
|
|
33
|
+
} as DndConfig;
|
|
34
|
+
const resolved = resolveDndConfig(config);
|
|
35
|
+
|
|
36
|
+
expect(resolved.enabled).toBe(true);
|
|
37
|
+
expect(resolved.sortable).toBe(true);
|
|
38
|
+
expect(resolved.autoScroll).toBe(false);
|
|
39
|
+
expect(resolved.touchDelay).toBe(500);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should pass through dragItem and dropZone', () => {
|
|
43
|
+
const dragItem = { type: 'card' } as DragItem;
|
|
44
|
+
const dropZone = { accept: ['card'] } as DropZone;
|
|
45
|
+
const config = { dragItem, dropZone } as DndConfig;
|
|
46
|
+
const resolved = resolveDndConfig(config);
|
|
47
|
+
|
|
48
|
+
expect(resolved.dragItem).toBe(dragItem);
|
|
49
|
+
expect(resolved.dropZone).toBe(dropZone);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// ==========================================================================
|
|
54
|
+
// createDragItemProps
|
|
55
|
+
// ==========================================================================
|
|
56
|
+
describe('createDragItemProps', () => {
|
|
57
|
+
it('should return correct props for a basic drag item', () => {
|
|
58
|
+
const item = { type: 'card', label: 'Task card' } as DragItem;
|
|
59
|
+
const props = createDragItemProps(item);
|
|
60
|
+
|
|
61
|
+
expect(props.draggable).toBe(true);
|
|
62
|
+
expect(props['aria-roledescription']).toBe('draggable');
|
|
63
|
+
expect(props['aria-label']).toBe('Task card');
|
|
64
|
+
expect(props.role).toBe('listitem');
|
|
65
|
+
expect(props['data-drag-type']).toBe('card');
|
|
66
|
+
expect(props['data-drag-handle']).toBe('element');
|
|
67
|
+
expect(props['data-drag-disabled']).toBe('false');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should set draggable false when disabled', () => {
|
|
71
|
+
const item = { type: 'card', disabled: true } as DragItem;
|
|
72
|
+
const props = createDragItemProps(item);
|
|
73
|
+
|
|
74
|
+
expect(props.draggable).toBe(false);
|
|
75
|
+
expect(props['data-drag-disabled']).toBe('true');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should use ariaLabel over label when both provided', () => {
|
|
79
|
+
const item = { type: 'card', label: 'Label', ariaLabel: 'Aria Label' } as DragItem;
|
|
80
|
+
const props = createDragItemProps(item);
|
|
81
|
+
|
|
82
|
+
expect(props['aria-label']).toBe('Aria Label');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should handle translation object for ariaLabel', () => {
|
|
86
|
+
const item = {
|
|
87
|
+
type: 'card',
|
|
88
|
+
ariaLabel: { key: 'drag.label', defaultValue: 'Translated label' },
|
|
89
|
+
} as unknown as DragItem;
|
|
90
|
+
const props = createDragItemProps(item);
|
|
91
|
+
|
|
92
|
+
expect(props['aria-label']).toBe('Translated label');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should use custom role and handle when provided', () => {
|
|
96
|
+
const item = { type: 'row', role: 'option', handle: 'grip' } as DragItem;
|
|
97
|
+
const props = createDragItemProps(item);
|
|
98
|
+
|
|
99
|
+
expect(props.role).toBe('option');
|
|
100
|
+
expect(props['data-drag-handle']).toBe('grip');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// ==========================================================================
|
|
105
|
+
// createDropZoneProps
|
|
106
|
+
// ==========================================================================
|
|
107
|
+
describe('createDropZoneProps', () => {
|
|
108
|
+
it('should return correct props for a basic drop zone', () => {
|
|
109
|
+
const zone = { accept: ['card', 'task'], label: 'Column' } as unknown as DropZone;
|
|
110
|
+
const props = createDropZoneProps(zone);
|
|
111
|
+
|
|
112
|
+
expect(props['aria-dropeffect']).toBe('move');
|
|
113
|
+
expect(props['aria-label']).toBe('Column');
|
|
114
|
+
expect(props.role).toBe('list');
|
|
115
|
+
expect(props['data-drop-accept']).toBe('card,task');
|
|
116
|
+
expect(props['data-drop-highlight']).toBe('true');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should use explicit dropEffect and role', () => {
|
|
120
|
+
const zone = { accept: ['item'], dropEffect: 'copy', role: 'region' } as unknown as DropZone;
|
|
121
|
+
const props = createDropZoneProps(zone);
|
|
122
|
+
|
|
123
|
+
expect(props['aria-dropeffect']).toBe('copy');
|
|
124
|
+
expect(props.role).toBe('region');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should set maxItems when provided', () => {
|
|
128
|
+
const zone = { accept: ['card'], maxItems: 10 } as unknown as DropZone;
|
|
129
|
+
const props = createDropZoneProps(zone);
|
|
130
|
+
|
|
131
|
+
expect(props['data-drop-max-items']).toBe(10);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('should handle ariaLabel as translation object', () => {
|
|
135
|
+
const zone = {
|
|
136
|
+
accept: ['card'],
|
|
137
|
+
ariaLabel: { key: 'drop.label', defaultValue: 'Drop here' },
|
|
138
|
+
} as unknown as DropZone;
|
|
139
|
+
const props = createDropZoneProps(zone);
|
|
140
|
+
|
|
141
|
+
expect(props['aria-label']).toBe('Drop here');
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// ==========================================================================
|
|
146
|
+
// resolveDragConstraints
|
|
147
|
+
// ==========================================================================
|
|
148
|
+
describe('resolveDragConstraints', () => {
|
|
149
|
+
it('should return base styles for default axis (both)', () => {
|
|
150
|
+
const constraint = {} as DragConstraint;
|
|
151
|
+
const styles = resolveDragConstraints(constraint);
|
|
152
|
+
|
|
153
|
+
expect(styles.position).toBe('relative');
|
|
154
|
+
expect(styles.touchAction).toBe('none');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should set touchAction to pan-y for x axis', () => {
|
|
158
|
+
const constraint = { axis: 'x' } as DragConstraint;
|
|
159
|
+
const styles = resolveDragConstraints(constraint);
|
|
160
|
+
|
|
161
|
+
expect(styles.touchAction).toBe('pan-y');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should set touchAction to pan-x for y axis', () => {
|
|
165
|
+
const constraint = { axis: 'y' } as DragConstraint;
|
|
166
|
+
const styles = resolveDragConstraints(constraint);
|
|
167
|
+
|
|
168
|
+
expect(styles.touchAction).toBe('pan-x');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should set overflow hidden for parent bounds', () => {
|
|
172
|
+
const constraint = { bounds: 'parent' } as DragConstraint;
|
|
173
|
+
const styles = resolveDragConstraints(constraint);
|
|
174
|
+
|
|
175
|
+
expect(styles.overflow).toBe('hidden');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should set grid CSS custom properties', () => {
|
|
179
|
+
const constraint = { grid: [20, 30] } as unknown as DragConstraint;
|
|
180
|
+
const styles = resolveDragConstraints(constraint);
|
|
181
|
+
|
|
182
|
+
expect(styles['--drag-grid-x']).toBe('20px');
|
|
183
|
+
expect(styles['--drag-grid-y']).toBe('30px');
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import type { KeyboardNavigationConfig, FocusTrapConfig } from '@object-ui/types';
|
|
3
|
+
import {
|
|
4
|
+
resolveKeyboardConfig,
|
|
5
|
+
parseShortcutKey,
|
|
6
|
+
matchesShortcut,
|
|
7
|
+
createFocusTrapConfig,
|
|
8
|
+
} from '../../protocols/KeyboardProtocol';
|
|
9
|
+
|
|
10
|
+
describe('KeyboardProtocol', () => {
|
|
11
|
+
// ==========================================================================
|
|
12
|
+
// resolveKeyboardConfig
|
|
13
|
+
// ==========================================================================
|
|
14
|
+
describe('resolveKeyboardConfig', () => {
|
|
15
|
+
it('should apply defaults for empty config', () => {
|
|
16
|
+
const config = {} as KeyboardNavigationConfig;
|
|
17
|
+
const resolved = resolveKeyboardConfig(config);
|
|
18
|
+
|
|
19
|
+
expect(resolved.shortcuts).toEqual([]);
|
|
20
|
+
expect(resolved.rovingTabindex).toBe(false);
|
|
21
|
+
expect(resolved.focusManagement.tabOrder).toBe('auto');
|
|
22
|
+
expect(resolved.focusManagement.skipLinks).toBe(false);
|
|
23
|
+
expect(resolved.focusManagement.focusVisible).toBe(true);
|
|
24
|
+
expect(resolved.focusManagement.arrowNavigation).toBe(false);
|
|
25
|
+
expect(resolved.focusManagement.focusTrap).toBeUndefined();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should resolve ariaLabel from string', () => {
|
|
29
|
+
const config = { ariaLabel: 'Navigation' } as KeyboardNavigationConfig;
|
|
30
|
+
const resolved = resolveKeyboardConfig(config);
|
|
31
|
+
|
|
32
|
+
expect(resolved.ariaLabel).toBe('Navigation');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should resolve ariaLabel from translation object', () => {
|
|
36
|
+
const config = {
|
|
37
|
+
ariaLabel: { key: 'nav.label', defaultValue: 'Nav panel' },
|
|
38
|
+
} as unknown as KeyboardNavigationConfig;
|
|
39
|
+
const resolved = resolveKeyboardConfig(config);
|
|
40
|
+
|
|
41
|
+
expect(resolved.ariaLabel).toBe('Nav panel');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should preserve explicit values', () => {
|
|
45
|
+
const config = {
|
|
46
|
+
rovingTabindex: true,
|
|
47
|
+
role: 'toolbar',
|
|
48
|
+
ariaDescribedBy: 'desc-id',
|
|
49
|
+
} as KeyboardNavigationConfig;
|
|
50
|
+
const resolved = resolveKeyboardConfig(config);
|
|
51
|
+
|
|
52
|
+
expect(resolved.rovingTabindex).toBe(true);
|
|
53
|
+
expect(resolved.role).toBe('toolbar');
|
|
54
|
+
expect(resolved.ariaDescribedBy).toBe('desc-id');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ==========================================================================
|
|
59
|
+
// parseShortcutKey
|
|
60
|
+
// ==========================================================================
|
|
61
|
+
describe('parseShortcutKey', () => {
|
|
62
|
+
it('should parse Ctrl+S', () => {
|
|
63
|
+
const parsed = parseShortcutKey('Ctrl+S');
|
|
64
|
+
|
|
65
|
+
expect(parsed.key).toBe('s');
|
|
66
|
+
expect(parsed.ctrlOrMeta).toBe(true);
|
|
67
|
+
expect(parsed.shift).toBe(false);
|
|
68
|
+
expect(parsed.alt).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should parse Alt+Shift+N', () => {
|
|
72
|
+
const parsed = parseShortcutKey('Alt+Shift+N');
|
|
73
|
+
|
|
74
|
+
expect(parsed.key).toBe('n');
|
|
75
|
+
expect(parsed.ctrlOrMeta).toBe(false);
|
|
76
|
+
expect(parsed.shift).toBe(true);
|
|
77
|
+
expect(parsed.alt).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should parse single key Escape', () => {
|
|
81
|
+
const parsed = parseShortcutKey('Escape');
|
|
82
|
+
|
|
83
|
+
expect(parsed.key).toBe('escape');
|
|
84
|
+
expect(parsed.ctrlOrMeta).toBe(false);
|
|
85
|
+
expect(parsed.shift).toBe(false);
|
|
86
|
+
expect(parsed.alt).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should be case insensitive', () => {
|
|
90
|
+
const parsed = parseShortcutKey('ctrl+s');
|
|
91
|
+
|
|
92
|
+
expect(parsed.key).toBe('s');
|
|
93
|
+
expect(parsed.ctrlOrMeta).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should parse single key F1', () => {
|
|
97
|
+
const parsed = parseShortcutKey('F1');
|
|
98
|
+
|
|
99
|
+
expect(parsed.key).toBe('f1');
|
|
100
|
+
expect(parsed.ctrlOrMeta).toBe(false);
|
|
101
|
+
expect(parsed.shift).toBe(false);
|
|
102
|
+
expect(parsed.alt).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should recognise Meta as ctrlOrMeta', () => {
|
|
106
|
+
const parsed = parseShortcutKey('Meta+K');
|
|
107
|
+
|
|
108
|
+
expect(parsed.ctrlOrMeta).toBe(true);
|
|
109
|
+
expect(parsed.key).toBe('k');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// ==========================================================================
|
|
114
|
+
// matchesShortcut
|
|
115
|
+
// ==========================================================================
|
|
116
|
+
describe('matchesShortcut', () => {
|
|
117
|
+
it('should match Ctrl+S event', () => {
|
|
118
|
+
const event = { key: 's', ctrlKey: true };
|
|
119
|
+
expect(matchesShortcut(event, 'Ctrl+S')).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should match Meta+S event (Cmd on Mac)', () => {
|
|
123
|
+
const event = { key: 's', metaKey: true };
|
|
124
|
+
expect(matchesShortcut(event, 'Ctrl+S')).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should not match when key differs', () => {
|
|
128
|
+
const event = { key: 'a', ctrlKey: true };
|
|
129
|
+
expect(matchesShortcut(event, 'Ctrl+S')).toBe(false);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should not match when modifier is missing', () => {
|
|
133
|
+
const event = { key: 's' };
|
|
134
|
+
expect(matchesShortcut(event, 'Ctrl+S')).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should not match when extra modifier is present', () => {
|
|
138
|
+
const event = { key: 's', ctrlKey: true, shiftKey: true };
|
|
139
|
+
expect(matchesShortcut(event, 'Ctrl+S')).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should match Escape without modifiers', () => {
|
|
143
|
+
const event = { key: 'Escape' };
|
|
144
|
+
expect(matchesShortcut(event, 'Escape')).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// ==========================================================================
|
|
149
|
+
// createFocusTrapConfig
|
|
150
|
+
// ==========================================================================
|
|
151
|
+
describe('createFocusTrapConfig', () => {
|
|
152
|
+
it('should apply defaults for minimal config', () => {
|
|
153
|
+
const config = {} as FocusTrapConfig;
|
|
154
|
+
const resolved = createFocusTrapConfig(config);
|
|
155
|
+
|
|
156
|
+
expect(resolved.enabled).toBe(false);
|
|
157
|
+
expect(resolved.returnFocus).toBe(true);
|
|
158
|
+
expect(resolved.escapeDeactivates).toBe(true);
|
|
159
|
+
expect(resolved.initialFocus).toBeUndefined();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should preserve explicit values', () => {
|
|
163
|
+
const config = {
|
|
164
|
+
enabled: true,
|
|
165
|
+
initialFocus: '#first-input',
|
|
166
|
+
returnFocus: false,
|
|
167
|
+
escapeDeactivates: false,
|
|
168
|
+
} as FocusTrapConfig;
|
|
169
|
+
const resolved = createFocusTrapConfig(config);
|
|
170
|
+
|
|
171
|
+
expect(resolved.enabled).toBe(true);
|
|
172
|
+
expect(resolved.initialFocus).toBe('#first-input');
|
|
173
|
+
expect(resolved.returnFocus).toBe(false);
|
|
174
|
+
expect(resolved.escapeDeactivates).toBe(false);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|