@object-ui/react 0.3.1 → 2.0.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 +4 -0
- package/CHANGELOG.md +13 -0
- package/dist/LazyPluginLoader.d.ts +82 -0
- package/dist/LazyPluginLoader.js +124 -0
- package/dist/SchemaRenderer.js +44 -12
- package/dist/components/form/FieldFactory.js +44 -40
- package/dist/components/form/FormRenderer.js +45 -3
- package/dist/context/ActionContext.d.ts +76 -0
- package/dist/context/ActionContext.js +139 -0
- package/dist/context/SchemaRendererContext.d.ts +2 -1
- package/dist/context/SchemaRendererContext.js +4 -2
- package/dist/context/ThemeContext.d.ts +94 -0
- package/dist/context/ThemeContext.js +215 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/useActionRunner.d.ts +21 -7
- package/dist/hooks/useActionRunner.js +27 -7
- package/dist/hooks/useDiscovery.d.ts +78 -0
- package/dist/hooks/useDiscovery.js +96 -0
- package/dist/hooks/useDynamicApp.d.ts +70 -0
- package/dist/hooks/useDynamicApp.js +92 -0
- package/dist/hooks/useNavigationOverlay.d.ts +78 -0
- package/dist/hooks/useNavigationOverlay.js +112 -0
- package/dist/hooks/usePageVariables.d.ts +67 -0
- package/dist/hooks/usePageVariables.js +109 -0
- package/dist/hooks/useViewData.d.ts +73 -0
- package/dist/hooks/useViewData.js +135 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/utils/i18n.d.ts +10 -0
- package/dist/utils/i18n.js +12 -0
- package/package.json +16 -6
- package/src/LazyPluginLoader.tsx +174 -0
- package/src/SchemaRenderer.tsx +64 -12
- package/src/__tests__/LazyPluginLoader.test.tsx +176 -0
- package/src/__tests__/ThemeProvider.test.tsx +451 -0
- package/src/__tests__/useNavigationOverlay.test.ts +416 -0
- package/src/__tests__/usePageVariables.test.tsx +334 -0
- package/src/__tests__/useViewData.test.tsx +422 -0
- package/src/components/form/FieldFactory.tsx +51 -46
- package/src/components/form/FormRenderer.test.tsx +145 -0
- package/src/components/form/FormRenderer.tsx +52 -3
- package/src/context/ActionContext.tsx +213 -0
- package/src/context/SchemaRendererContext.tsx +5 -2
- package/src/context/ThemeContext.tsx +325 -0
- package/src/context/__tests__/ActionContext.test.tsx +174 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
- package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
- package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
- package/src/hooks/__tests__/useExpression.test.ts +86 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useActionRunner.ts +49 -11
- package/src/hooks/useDiscovery.ts +141 -0
- package/src/hooks/useDynamicApp.ts +142 -0
- package/src/hooks/useNavigationOverlay.ts +180 -0
- package/src/hooks/usePageVariables.tsx +157 -0
- package/src/hooks/useViewData.ts +201 -0
- package/src/index.ts +22 -0
- package/src/utils/i18n.ts +10 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
- package/dist/components/form/FormRenderer.test.d.ts +0 -8
- package/dist/components/form/FormRenderer.test.js +0 -534
- package/dist/index.test.d.ts +0 -8
- package/dist/index.test.js +0 -13
|
@@ -0,0 +1,157 @@
|
|
|
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 React, { createContext, useContext, useState, useCallback, useMemo } from 'react';
|
|
10
|
+
import type { PageVariable } from '@object-ui/types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Page variables context value.
|
|
14
|
+
* Provides access to page-level state variables.
|
|
15
|
+
*/
|
|
16
|
+
export interface PageVariablesContextValue {
|
|
17
|
+
/** Current variable values */
|
|
18
|
+
variables: Record<string, any>;
|
|
19
|
+
/** Set a single variable value */
|
|
20
|
+
setVariable: (name: string, value: any) => void;
|
|
21
|
+
/** Set multiple variable values at once */
|
|
22
|
+
setVariables: (updates: Record<string, any>) => void;
|
|
23
|
+
/** Reset all variables to their default values */
|
|
24
|
+
resetVariables: () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PageVariablesContext = createContext<PageVariablesContextValue | null>(null);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Initialize page variables from their definitions.
|
|
31
|
+
* Sets each variable to its defaultValue or type-appropriate default.
|
|
32
|
+
*/
|
|
33
|
+
function initializeVariables(definitions?: PageVariable[]): Record<string, any> {
|
|
34
|
+
if (!definitions || definitions.length === 0) return {};
|
|
35
|
+
|
|
36
|
+
const initial: Record<string, any> = {};
|
|
37
|
+
for (const def of definitions) {
|
|
38
|
+
if (def.defaultValue !== undefined) {
|
|
39
|
+
initial[def.name] = def.defaultValue;
|
|
40
|
+
} else {
|
|
41
|
+
// Type-appropriate defaults
|
|
42
|
+
switch (def.type) {
|
|
43
|
+
case 'number':
|
|
44
|
+
initial[def.name] = 0;
|
|
45
|
+
break;
|
|
46
|
+
case 'boolean':
|
|
47
|
+
initial[def.name] = false;
|
|
48
|
+
break;
|
|
49
|
+
case 'object':
|
|
50
|
+
initial[def.name] = {};
|
|
51
|
+
break;
|
|
52
|
+
case 'array':
|
|
53
|
+
initial[def.name] = [];
|
|
54
|
+
break;
|
|
55
|
+
case 'string':
|
|
56
|
+
default:
|
|
57
|
+
initial[def.name] = '';
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return initial;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Props for PageVariablesProvider
|
|
67
|
+
*/
|
|
68
|
+
export interface PageVariablesProviderProps {
|
|
69
|
+
/** Variable definitions from PageSchema.variables */
|
|
70
|
+
definitions?: PageVariable[];
|
|
71
|
+
/** Child components */
|
|
72
|
+
children: React.ReactNode;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* PageVariablesProvider — Provides page-level state variables to the component tree.
|
|
77
|
+
*
|
|
78
|
+
* Initializes variables from their definitions and provides read/write access
|
|
79
|
+
* to all child components via the usePageVariables hook.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```tsx
|
|
83
|
+
* <PageVariablesProvider definitions={[
|
|
84
|
+
* { name: 'selectedId', type: 'string', defaultValue: '' },
|
|
85
|
+
* { name: 'count', type: 'number', defaultValue: 0 },
|
|
86
|
+
* ]}>
|
|
87
|
+
* <MyComponents />
|
|
88
|
+
* </PageVariablesProvider>
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export const PageVariablesProvider: React.FC<PageVariablesProviderProps> = ({
|
|
92
|
+
definitions,
|
|
93
|
+
children,
|
|
94
|
+
}) => {
|
|
95
|
+
const [variables, setVariablesState] = useState<Record<string, any>>(() =>
|
|
96
|
+
initializeVariables(definitions)
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const setVariable = useCallback((name: string, value: any) => {
|
|
100
|
+
setVariablesState((prev) => ({ ...prev, [name]: value }));
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
const setVariables = useCallback((updates: Record<string, any>) => {
|
|
104
|
+
setVariablesState((prev) => ({ ...prev, ...updates }));
|
|
105
|
+
}, []);
|
|
106
|
+
|
|
107
|
+
const resetVariables = useCallback(() => {
|
|
108
|
+
setVariablesState(initializeVariables(definitions));
|
|
109
|
+
}, [definitions]);
|
|
110
|
+
|
|
111
|
+
const value = useMemo<PageVariablesContextValue>(
|
|
112
|
+
() => ({ variables, setVariable, setVariables, resetVariables }),
|
|
113
|
+
[variables, setVariable, setVariables, resetVariables]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<PageVariablesContext.Provider value={value}>
|
|
118
|
+
{children}
|
|
119
|
+
</PageVariablesContext.Provider>
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
PageVariablesProvider.displayName = 'PageVariablesProvider';
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Hook to access page-level variables.
|
|
127
|
+
*
|
|
128
|
+
* Returns the current variable values and setter functions.
|
|
129
|
+
* Returns a no-op fallback if used outside a PageVariablesProvider.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```tsx
|
|
133
|
+
* const { variables, setVariable } = usePageVariables();
|
|
134
|
+
* const userId = variables.selectedId;
|
|
135
|
+
* setVariable('selectedId', '123');
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export function usePageVariables(): PageVariablesContextValue {
|
|
139
|
+
const ctx = useContext(PageVariablesContext);
|
|
140
|
+
if (!ctx) {
|
|
141
|
+
// Graceful fallback — allows components to work outside a Page
|
|
142
|
+
return {
|
|
143
|
+
variables: {},
|
|
144
|
+
setVariable: () => {},
|
|
145
|
+
setVariables: () => {},
|
|
146
|
+
resetVariables: () => {},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return ctx;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Hook to check if a PageVariablesProvider is available.
|
|
154
|
+
*/
|
|
155
|
+
export function useHasPageVariables(): boolean {
|
|
156
|
+
return useContext(PageVariablesContext) !== null;
|
|
157
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — useViewData
|
|
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
|
+
* A React hook that resolves a ViewData configuration into a DataSource
|
|
9
|
+
* and provides reactive data fetching with loading/error states.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useState, useEffect, useCallback, useMemo, useContext } from 'react';
|
|
13
|
+
import type { DataSource, ViewData, QueryParams, QueryResult } from '@object-ui/types';
|
|
14
|
+
import { resolveDataSource, type ResolveDataSourceOptions } from '@object-ui/core';
|
|
15
|
+
import { SchemaRendererContext } from '../context/SchemaRendererContext';
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Types
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
export interface UseViewDataOptions<T = any> {
|
|
22
|
+
/** The ViewData configuration from the schema */
|
|
23
|
+
viewData?: ViewData | null;
|
|
24
|
+
/** Resource name (object name, used for DataSource.find calls) */
|
|
25
|
+
resource?: string;
|
|
26
|
+
/** Initial query parameters */
|
|
27
|
+
params?: QueryParams;
|
|
28
|
+
/** Explicit data source override (bypasses context) */
|
|
29
|
+
dataSource?: DataSource<T> | null;
|
|
30
|
+
/** Options for adapter construction */
|
|
31
|
+
adapterOptions?: ResolveDataSourceOptions;
|
|
32
|
+
/** If true, skip the initial fetch (useful for forms in create mode) */
|
|
33
|
+
skip?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface UseViewDataResult<T = any> {
|
|
37
|
+
/** Resolved data array */
|
|
38
|
+
data: T[];
|
|
39
|
+
/** Total number of records (from server or array length) */
|
|
40
|
+
totalCount: number;
|
|
41
|
+
/** Whether data is being fetched */
|
|
42
|
+
loading: boolean;
|
|
43
|
+
/** Error from the last fetch attempt */
|
|
44
|
+
error: Error | null;
|
|
45
|
+
/** The resolved DataSource instance (can be used for create/update/delete) */
|
|
46
|
+
dataSource: DataSource<T> | null;
|
|
47
|
+
/** Re-fetch data with current or new params */
|
|
48
|
+
refresh: (newParams?: QueryParams) => Promise<void>;
|
|
49
|
+
/** Fetch a single record by ID */
|
|
50
|
+
fetchOne: (id: string | number) => Promise<T | null>;
|
|
51
|
+
/** Whether more records are available */
|
|
52
|
+
hasMore: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// Hook
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* useViewData — resolves ViewData into a DataSource and provides reactive data.
|
|
61
|
+
*
|
|
62
|
+
* This hook centralizes the data resolution pattern that was previously
|
|
63
|
+
* duplicated across plugins (ObjectGrid, ObjectView, etc.). It:
|
|
64
|
+
*
|
|
65
|
+
* 1. Resolves the ViewData config to the right DataSource adapter
|
|
66
|
+
* - `provider: 'object'` → context DataSource (ObjectStackAdapter)
|
|
67
|
+
* - `provider: 'api'` → ApiDataSource (raw HTTP)
|
|
68
|
+
* - `provider: 'value'` → ValueDataSource (in-memory)
|
|
69
|
+
* 2. Fetches data automatically on mount
|
|
70
|
+
* 3. Provides loading/error states and refresh capability
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* function MyGrid({ schema }) {
|
|
75
|
+
* const { data, loading, error, refresh, dataSource } = useViewData({
|
|
76
|
+
* viewData: schema.data,
|
|
77
|
+
* resource: schema.objectName,
|
|
78
|
+
* params: { $top: 50 },
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (loading) return <Spinner />;
|
|
82
|
+
* if (error) return <ErrorMessage error={error} />;
|
|
83
|
+
* return <Table data={data} />;
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function useViewData<T = any>(options: UseViewDataOptions<T>): UseViewDataResult<T> {
|
|
88
|
+
const {
|
|
89
|
+
viewData,
|
|
90
|
+
resource = '',
|
|
91
|
+
params,
|
|
92
|
+
dataSource: explicitDataSource,
|
|
93
|
+
adapterOptions,
|
|
94
|
+
skip = false,
|
|
95
|
+
} = options;
|
|
96
|
+
|
|
97
|
+
// Get context DataSource
|
|
98
|
+
const context = useContext(SchemaRendererContext);
|
|
99
|
+
const contextDataSource = context?.dataSource ?? null;
|
|
100
|
+
|
|
101
|
+
// Resolve the DataSource — memoize to prevent re-creation on every render.
|
|
102
|
+
// We key on the viewData provider + config identity.
|
|
103
|
+
const resolvedDataSource = useMemo(() => {
|
|
104
|
+
if (explicitDataSource) return explicitDataSource;
|
|
105
|
+
return resolveDataSource<T>(viewData, contextDataSource, adapterOptions);
|
|
106
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
107
|
+
}, [
|
|
108
|
+
explicitDataSource,
|
|
109
|
+
viewData?.provider,
|
|
110
|
+
// For 'api' provider, key on read/write URLs
|
|
111
|
+
viewData?.provider === 'api' ? (viewData as any).read?.url : undefined,
|
|
112
|
+
viewData?.provider === 'api' ? (viewData as any).write?.url : undefined,
|
|
113
|
+
// For 'value' provider, key on items reference (or length as proxy)
|
|
114
|
+
viewData?.provider === 'value' ? (viewData as any).items?.length : undefined,
|
|
115
|
+
contextDataSource,
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
// State
|
|
119
|
+
const [data, setData] = useState<T[]>([]);
|
|
120
|
+
const [totalCount, setTotalCount] = useState(0);
|
|
121
|
+
const [loading, setLoading] = useState(!skip);
|
|
122
|
+
const [error, setError] = useState<Error | null>(null);
|
|
123
|
+
const [hasMore, setHasMore] = useState(false);
|
|
124
|
+
|
|
125
|
+
// Stable params reference
|
|
126
|
+
const paramsKey = useMemo(() => JSON.stringify(params ?? {}), [params]);
|
|
127
|
+
|
|
128
|
+
// Fetch function
|
|
129
|
+
const fetchData = useCallback(
|
|
130
|
+
async (fetchParams?: QueryParams) => {
|
|
131
|
+
if (!resolvedDataSource || !resource) {
|
|
132
|
+
setLoading(false);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setLoading(true);
|
|
137
|
+
setError(null);
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
const mergedParams = fetchParams ?? (params ? { ...params } : undefined);
|
|
141
|
+
const result: QueryResult<T> = await resolvedDataSource.find(resource, mergedParams);
|
|
142
|
+
setData(result.data ?? []);
|
|
143
|
+
setTotalCount(result.total ?? result.data?.length ?? 0);
|
|
144
|
+
setHasMore(result.hasMore ?? false);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
147
|
+
setData([]);
|
|
148
|
+
setTotalCount(0);
|
|
149
|
+
} finally {
|
|
150
|
+
setLoading(false);
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
[resolvedDataSource, resource, params],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// Auto-fetch on mount / when deps change
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
if (skip) {
|
|
159
|
+
setLoading(false);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// For value provider, data is synchronous — still go through fetchData
|
|
164
|
+
// for consistency, but it resolves immediately.
|
|
165
|
+
fetchData();
|
|
166
|
+
|
|
167
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
168
|
+
}, [resolvedDataSource, resource, paramsKey, skip]);
|
|
169
|
+
|
|
170
|
+
// Refresh function (can accept new params)
|
|
171
|
+
const refresh = useCallback(
|
|
172
|
+
async (newParams?: QueryParams) => {
|
|
173
|
+
await fetchData(newParams);
|
|
174
|
+
},
|
|
175
|
+
[fetchData],
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// Fetch one record
|
|
179
|
+
const fetchOne = useCallback(
|
|
180
|
+
async (id: string | number): Promise<T | null> => {
|
|
181
|
+
if (!resolvedDataSource) return null;
|
|
182
|
+
try {
|
|
183
|
+
return await resolvedDataSource.findOne(resource, id);
|
|
184
|
+
} catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
[resolvedDataSource, resource],
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
data,
|
|
193
|
+
totalCount,
|
|
194
|
+
loading,
|
|
195
|
+
error,
|
|
196
|
+
dataSource: resolvedDataSource,
|
|
197
|
+
refresh,
|
|
198
|
+
fetchOne,
|
|
199
|
+
hasMore,
|
|
200
|
+
};
|
|
201
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,4 +10,26 @@ export * from './SchemaRenderer';
|
|
|
10
10
|
export * from './hooks'; // will be empty for now
|
|
11
11
|
export * from './context'; // will be empty for now
|
|
12
12
|
export * from './components/form';
|
|
13
|
+
export * from './LazyPluginLoader';
|
|
14
|
+
|
|
15
|
+
// Built-in i18n support
|
|
16
|
+
export {
|
|
17
|
+
I18nProvider,
|
|
18
|
+
useObjectTranslation,
|
|
19
|
+
useI18nContext,
|
|
20
|
+
createI18n,
|
|
21
|
+
getDirection,
|
|
22
|
+
getAvailableLanguages,
|
|
23
|
+
formatDate,
|
|
24
|
+
formatDateTime,
|
|
25
|
+
formatRelativeTime,
|
|
26
|
+
formatCurrency,
|
|
27
|
+
formatNumber,
|
|
28
|
+
type I18nConfig,
|
|
29
|
+
type I18nProviderProps,
|
|
30
|
+
type TranslationKeys,
|
|
31
|
+
type DateFormatOptions,
|
|
32
|
+
type CurrencyFormatOptions,
|
|
33
|
+
type NumberFormatOptions,
|
|
34
|
+
} from '@object-ui/i18n';
|
|
13
35
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves an I18nLabel to a plain string.
|
|
3
|
+
* I18nLabel can be either a string or an object { key, defaultValue?, params? }.
|
|
4
|
+
* When it's an object, we return the defaultValue or the key as fallback.
|
|
5
|
+
*/
|
|
6
|
+
export function resolveI18nLabel(label: string | { key: string; defaultValue?: string; params?: Record<string, any> } | undefined): string | undefined {
|
|
7
|
+
if (label === undefined || label === null) return undefined;
|
|
8
|
+
if (typeof label === 'string') return label;
|
|
9
|
+
return label.defaultValue || label.key;
|
|
10
|
+
}
|