@object-ui/react 0.3.0 → 0.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.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +8 -0
- package/dist/LazyPluginLoader.d.ts +38 -0
- package/dist/LazyPluginLoader.js +35 -0
- package/dist/SchemaRenderer.d.ts +7 -0
- package/dist/SchemaRenderer.js +51 -12
- package/dist/__tests__/LazyPluginLoader.test.d.ts +8 -0
- package/dist/__tests__/LazyPluginLoader.test.js +49 -0
- package/dist/components/form/FieldFactory.d.ts +43 -0
- package/dist/components/form/FieldFactory.js +157 -0
- package/dist/components/form/FormRenderer.d.ts +40 -0
- package/dist/components/form/FormRenderer.js +87 -0
- package/dist/components/form/FormRenderer.test.d.ts +8 -0
- package/dist/components/form/FormRenderer.test.js +534 -0
- package/dist/components/form/index.d.ts +11 -0
- package/dist/components/form/index.js +9 -0
- package/dist/context/SchemaRendererContext.d.ts +14 -0
- package/dist/context/SchemaRendererContext.js +23 -0
- package/dist/context/index.d.ts +8 -1
- package/dist/context/index.js +8 -1
- package/dist/hooks/index.d.ts +9 -1
- package/dist/hooks/index.js +9 -1
- package/dist/hooks/useActionRunner.d.ts +32 -0
- package/dist/hooks/useActionRunner.js +65 -0
- package/dist/hooks/useExpression.d.ts +27 -0
- package/dist/hooks/useExpression.js +41 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/index.test.d.ts +7 -0
- package/dist/index.test.js +7 -0
- package/package.json +26 -6
- package/src/LazyPluginLoader.tsx +52 -0
- package/src/SchemaRenderer.tsx +72 -12
- package/src/__tests__/LazyPluginLoader.test.tsx +80 -0
- package/src/components/form/FieldFactory.tsx +504 -0
- package/src/components/form/FormRenderer.test.tsx +599 -0
- package/src/components/form/FormRenderer.tsx +209 -0
- package/src/components/form/README.md +352 -0
- package/src/components/form/index.ts +12 -0
- package/src/context/SchemaRendererContext.tsx +43 -0
- package/src/context/index.ts +9 -1
- package/src/hooks/index.ts +11 -1
- package/src/hooks/useActionRunner.ts +81 -0
- package/src/hooks/useExpression.ts +56 -0
- package/src/index.test.ts +8 -0
- package/src/index.ts +10 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
|
@@ -0,0 +1,81 @@
|
|
|
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 { useState, useCallback, useMemo } from 'react';
|
|
10
|
+
import { ActionRunner, type ActionContext, type ActionResult } from '@object-ui/core';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for executing actions with loading state
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const { execute, loading, error, result } = useActionRunner({ data: formData });
|
|
18
|
+
*
|
|
19
|
+
* const handleClick = () => {
|
|
20
|
+
* execute({
|
|
21
|
+
* type: 'action',
|
|
22
|
+
* api: '/api/save',
|
|
23
|
+
* successMessage: 'Saved successfully!'
|
|
24
|
+
* });
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useActionRunner(context: ActionContext = {}) {
|
|
29
|
+
const [loading, setLoading] = useState(false);
|
|
30
|
+
const [error, setError] = useState<string | null>(null);
|
|
31
|
+
const [result, setResult] = useState<ActionResult | null>(null);
|
|
32
|
+
|
|
33
|
+
const runner = useMemo(
|
|
34
|
+
() => new ActionRunner(context),
|
|
35
|
+
[JSON.stringify(context)]
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const execute = useCallback(
|
|
39
|
+
async (action: any): Promise<ActionResult> => {
|
|
40
|
+
setLoading(true);
|
|
41
|
+
setError(null);
|
|
42
|
+
setResult(null);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const actionResult = await runner.execute(action);
|
|
46
|
+
setResult(actionResult);
|
|
47
|
+
|
|
48
|
+
if (!actionResult.success) {
|
|
49
|
+
setError(actionResult.error || 'Action failed');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return actionResult;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
const errorMessage = (err as Error).message;
|
|
55
|
+
setError(errorMessage);
|
|
56
|
+
const failureResult = { success: false, error: errorMessage };
|
|
57
|
+
setResult(failureResult);
|
|
58
|
+
return failureResult;
|
|
59
|
+
} finally {
|
|
60
|
+
setLoading(false);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
[runner]
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const updateContext = useCallback(
|
|
67
|
+
(newContext: Partial<ActionContext>) => {
|
|
68
|
+
runner.updateContext(newContext);
|
|
69
|
+
},
|
|
70
|
+
[runner]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
execute,
|
|
75
|
+
loading,
|
|
76
|
+
error,
|
|
77
|
+
result,
|
|
78
|
+
updateContext,
|
|
79
|
+
runner
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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 { useMemo } from 'react';
|
|
10
|
+
import { ExpressionEvaluator } from '@object-ui/core';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for evaluating expressions with dynamic context
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const isVisible = useExpression('${data.age >= 18}', { data });
|
|
18
|
+
* const label = useExpression('Hello ${user.name}!', { user });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function useExpression(
|
|
22
|
+
expression: string | boolean | number | null | undefined,
|
|
23
|
+
context: Record<string, any> = {}
|
|
24
|
+
): any {
|
|
25
|
+
// We evaluate directly without caching the evaluator to avoid issues with context changes
|
|
26
|
+
return useMemo(
|
|
27
|
+
() => {
|
|
28
|
+
const evaluator = new ExpressionEvaluator(context);
|
|
29
|
+
return evaluator.evaluate(expression);
|
|
30
|
+
},
|
|
31
|
+
[expression, context]
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Hook for evaluating conditional expressions
|
|
37
|
+
* Returns a boolean value
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* const isVisible = useCondition('${data.status === "active"}', { data });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function useCondition(
|
|
45
|
+
condition: string | boolean | undefined,
|
|
46
|
+
context: Record<string, any> = {}
|
|
47
|
+
): boolean {
|
|
48
|
+
// We evaluate directly without caching the evaluator to avoid issues with context changes
|
|
49
|
+
return useMemo(
|
|
50
|
+
() => {
|
|
51
|
+
const evaluator = new ExpressionEvaluator(context);
|
|
52
|
+
return evaluator.evaluateCondition(condition);
|
|
53
|
+
},
|
|
54
|
+
[condition, context]
|
|
55
|
+
);
|
|
56
|
+
}
|
package/src/index.test.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
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
|
+
|
|
1
9
|
import { describe, it, expect } from 'vitest';
|
|
2
10
|
|
|
3
11
|
describe('react', () => {
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
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
|
+
|
|
1
9
|
export * from './SchemaRenderer';
|
|
2
10
|
export * from './hooks'; // will be empty for now
|
|
3
11
|
export * from './context'; // will be empty for now
|
|
12
|
+
export * from './components/form';
|
|
13
|
+
export * from './LazyPluginLoader';
|
|
4
14
|
|