@dotcms/react 0.0.1-alpha.7 → 0.0.1-alpha.9
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/.babelrc +12 -0
- package/.eslintrc.json +18 -0
- package/jest.config.ts +11 -0
- package/package.json +27 -30
- package/project.json +51 -0
- package/src/{index.d.ts → index.ts} +0 -1
- package/src/lib/components/Column/Column.module.css +99 -0
- package/src/lib/components/Column/Column.spec.tsx +78 -0
- package/src/lib/components/Column/Column.tsx +45 -0
- package/src/lib/components/Container/Container.module.css +7 -0
- package/src/lib/components/Container/Container.spec.tsx +82 -0
- package/src/lib/components/Container/Container.tsx +105 -0
- package/src/lib/components/DotcmsLayout/DotcmsLayout.module.css +7 -0
- package/src/lib/components/DotcmsLayout/DotcmsLayout.spec.tsx +41 -0
- package/src/lib/components/DotcmsLayout/{DotcmsLayout.d.ts → DotcmsLayout.tsx} +19 -3
- package/src/lib/components/PageProvider/PageProvider.module.css +7 -0
- package/src/lib/components/PageProvider/PageProvider.spec.tsx +54 -0
- package/src/lib/components/PageProvider/{PageProvider.d.ts → PageProvider.tsx} +14 -1
- package/src/lib/components/Row/Row.module.css +5 -0
- package/src/lib/components/Row/Row.spec.tsx +92 -0
- package/src/lib/components/Row/Row.tsx +51 -0
- package/src/lib/contexts/PageContext.tsx +5 -0
- package/src/lib/hooks/useDotcmsEditor.spec.ts +56 -0
- package/src/lib/hooks/useDotcmsEditor.ts +29 -0
- package/src/lib/hooks/useDotcmsPageContext.spec.tsx +43 -0
- package/src/lib/hooks/{useDotcmsPageContext.d.ts → useDotcmsPageContext.tsx} +7 -1
- package/src/lib/mocks/mockPageContext.tsx +84 -0
- package/src/lib/utils/utils.ts +69 -0
- package/tsconfig.json +20 -0
- package/tsconfig.lib.json +23 -0
- package/tsconfig.spec.json +20 -0
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -2912
- package/src/lib/components/Column/Column.d.ts +0 -5
- package/src/lib/components/Container/Container.d.ts +0 -5
- package/src/lib/components/Row/Row.d.ts +0 -25
- package/src/lib/contexts/PageContext.d.ts +0 -3
- package/src/lib/hooks/usePageEditor.d.ts +0 -50
- package/src/lib/mocks/mockPageContext.d.ts +0 -7
- package/src/lib/utils/utils.d.ts +0 -55
- /package/src/lib/mocks/{index.d.ts → index.ts} +0 -0
package/.babelrc
ADDED
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["plugin:@nrwl/nx/react", "../../../.eslintrc.base.json"],
|
|
3
|
+
"ignorePatterns": ["!**/*"],
|
|
4
|
+
"overrides": [
|
|
5
|
+
{
|
|
6
|
+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
+
"rules": {}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"files": ["*.ts", "*.tsx"],
|
|
11
|
+
"rules": {}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"files": ["*.js", "*.jsx"],
|
|
15
|
+
"rules": {}
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
export default {
|
|
3
|
+
displayName: 'sdk-react',
|
|
4
|
+
preset: '../../../jest.preset.js',
|
|
5
|
+
transform: {
|
|
6
|
+
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
|
|
7
|
+
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nrwl/react/babel'] }]
|
|
8
|
+
},
|
|
9
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
10
|
+
coverageDirectory: '../../../coverage/libs/sdk/react'
|
|
11
|
+
};
|
package/package.json
CHANGED
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"module": "./index.esm.js",
|
|
30
|
-
"type": "module",
|
|
31
|
-
"main": "./index.esm.js"
|
|
2
|
+
"name": "@dotcms/react",
|
|
3
|
+
"version": "0.0.1-alpha.9",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"react": ">=18",
|
|
6
|
+
"react-dom": ">=18",
|
|
7
|
+
"@dotcms/client": "0.0.1-alpha.9"
|
|
8
|
+
},
|
|
9
|
+
"description": "Official React Components library to render a dotCMS page.",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/dotCMS/core.git#master"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"dotCMS",
|
|
16
|
+
"CMS",
|
|
17
|
+
"Content Management",
|
|
18
|
+
"API Client",
|
|
19
|
+
"REST API",
|
|
20
|
+
"React",
|
|
21
|
+
"Components"
|
|
22
|
+
],
|
|
23
|
+
"author": "dotcms <dev@dotcms.com>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/dotCMS/core/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/dotCMS/core/tree/master/core-web/libs/sdk/react/README.md"
|
|
32
29
|
}
|
package/project.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sdk-react",
|
|
3
|
+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "libs/sdk/react/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"lint": {
|
|
9
|
+
"executor": "@nrwl/linter:eslint",
|
|
10
|
+
"outputs": ["{options.outputFile}"],
|
|
11
|
+
"options": {
|
|
12
|
+
"lintFilePatterns": ["libs/sdk/react/**/*.{ts,tsx,js,jsx}"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"build": {
|
|
16
|
+
"executor": "@nrwl/rollup:rollup",
|
|
17
|
+
"outputs": ["{options.outputPath}"],
|
|
18
|
+
"options": {
|
|
19
|
+
"outputPath": "dist/libs/sdk/react",
|
|
20
|
+
"tsConfig": "libs/sdk/react/tsconfig.lib.json",
|
|
21
|
+
"project": "libs/sdk/react/package.json",
|
|
22
|
+
"entryFile": "libs/sdk/react/src/index.ts",
|
|
23
|
+
"external": ["react/jsx-runtime"],
|
|
24
|
+
"rollupConfig": "@nrwl/react/plugins/bundle-rollup",
|
|
25
|
+
"compiler": "babel",
|
|
26
|
+
"extractCss": false,
|
|
27
|
+
"assets": [
|
|
28
|
+
{
|
|
29
|
+
"glob": "libs/sdk/react/README.md",
|
|
30
|
+
"input": ".",
|
|
31
|
+
"output": "."
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"test": {
|
|
37
|
+
"executor": "@nrwl/jest:jest",
|
|
38
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
39
|
+
"options": {
|
|
40
|
+
"jestConfig": "libs/sdk/react/jest.config.ts",
|
|
41
|
+
"passWithNoTests": true
|
|
42
|
+
},
|
|
43
|
+
"configurations": {
|
|
44
|
+
"ci": {
|
|
45
|
+
"ci": true,
|
|
46
|
+
"codeCoverage": true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
.col-start-1 {
|
|
2
|
+
grid-column-start: 1;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.col-start-2 {
|
|
6
|
+
grid-column-start: 2;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.col-start-3 {
|
|
10
|
+
grid-column-start: 3;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.col-start-4 {
|
|
14
|
+
grid-column-start: 4;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.col-start-5 {
|
|
18
|
+
grid-column-start: 5;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.col-start-6 {
|
|
22
|
+
grid-column-start: 6;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.col-start-7 {
|
|
26
|
+
grid-column-start: 7;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.col-start-8 {
|
|
30
|
+
grid-column-start: 8;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.col-start-9 {
|
|
34
|
+
grid-column-start: 9;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.col-start-10 {
|
|
38
|
+
grid-column-start: 10;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.col-start-11 {
|
|
42
|
+
grid-column-start: 11;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.col-start-12 {
|
|
46
|
+
grid-column-start: 12;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.col-end-1 {
|
|
50
|
+
grid-column-end: 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.col-end-2 {
|
|
54
|
+
grid-column-end: 2;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.col-end-3 {
|
|
58
|
+
grid-column-end: 3;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.col-end-4 {
|
|
62
|
+
grid-column-end: 4;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.col-end-5 {
|
|
66
|
+
grid-column-end: 5;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.col-end-6 {
|
|
70
|
+
grid-column-end: 6;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.col-end-7 {
|
|
74
|
+
grid-column-end: 7;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.col-end-8 {
|
|
78
|
+
grid-column-end: 8;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.col-end-9 {
|
|
82
|
+
grid-column-end: 9;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.col-end-10 {
|
|
86
|
+
grid-column-end: 10;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.col-end-11 {
|
|
90
|
+
grid-column-end: 11;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.col-end-12 {
|
|
94
|
+
grid-column-end: 12;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.col-end-13 {
|
|
98
|
+
grid-column-end: 13;
|
|
99
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import '@testing-library/jest-dom';
|
|
3
|
+
|
|
4
|
+
import * as dotcmsClient from '@dotcms/client';
|
|
5
|
+
|
|
6
|
+
import { Column } from './Column';
|
|
7
|
+
|
|
8
|
+
import { MockContextRender } from '../../mocks/mockPageContext';
|
|
9
|
+
import { ContainerProps } from '../Container/Container';
|
|
10
|
+
|
|
11
|
+
jest.mock('../Container/Container', () => {
|
|
12
|
+
return {
|
|
13
|
+
Container: ({ containerRef }: Partial<ContainerProps>) => (
|
|
14
|
+
<div data-testid="mockContainer">{containerRef?.identifier}</div>
|
|
15
|
+
)
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('Column', () => {
|
|
20
|
+
const mockColumnData = {
|
|
21
|
+
width: 6, // Adjust as needed
|
|
22
|
+
leftOffset: 3, // Adjust as needed
|
|
23
|
+
containers: [
|
|
24
|
+
{ identifier: 'Container1', uuid: 'unique-id-1' },
|
|
25
|
+
{ identifier: 'Container2', uuid: 'unique-id-2' }
|
|
26
|
+
// Add more containers as needed for your test
|
|
27
|
+
],
|
|
28
|
+
styleClass: ''
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe('Column is inside editor', () => {
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest.spyOn(dotcmsClient, 'isInsideEditor').mockReturnValue(true);
|
|
34
|
+
render(
|
|
35
|
+
<MockContextRender mockContext={{ isInsideEditor: true }}>
|
|
36
|
+
<Column column={mockColumnData} />
|
|
37
|
+
</MockContextRender>
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('applies the correct width and start classes based on props', () => {
|
|
42
|
+
const columnElement = screen.getByTestId('column');
|
|
43
|
+
expect(columnElement).toHaveClass('col-end-9');
|
|
44
|
+
expect(columnElement).toHaveClass('col-start-3');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('applies the correct data attr', () => {
|
|
48
|
+
expect(screen.getByTestId('column')).toHaveAttribute('data-dot', 'column');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('renders the correct number of containers', () => {
|
|
52
|
+
const containers = screen.getAllByTestId('mockContainer');
|
|
53
|
+
expect(containers.length).toBe(mockColumnData.containers.length);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('passes the correct props to each Container', () => {
|
|
57
|
+
mockColumnData.containers.forEach((container) => {
|
|
58
|
+
expect(screen.getByText(container.identifier)).toBeInTheDocument();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('Column is not inside editor', () => {
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
jest.spyOn(dotcmsClient, 'isInsideEditor').mockReturnValue(false);
|
|
66
|
+
render(
|
|
67
|
+
<MockContextRender mockContext={{ isInsideEditor: false }}>
|
|
68
|
+
<Column column={mockColumnData} />
|
|
69
|
+
</MockContextRender>
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should not have dot attrs', () => {
|
|
74
|
+
const columnElement = screen.queryByTestId('column');
|
|
75
|
+
expect(columnElement).toBeNull();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import styles from './column.module.css';
|
|
4
|
+
|
|
5
|
+
import { PageContext } from '../../contexts/PageContext';
|
|
6
|
+
import { combineClasses, getPositionStyleClasses } from '../../utils/utils';
|
|
7
|
+
import { Container } from '../Container/Container';
|
|
8
|
+
import { PageProviderContext } from '../PageProvider/PageProvider';
|
|
9
|
+
|
|
10
|
+
export interface ColumnProps {
|
|
11
|
+
readonly column: PageProviderContext['layout']['body']['rows'][0]['columns'][0];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function Column({ column }: ColumnProps) {
|
|
15
|
+
const { isInsideEditor } = useContext(PageContext) as PageProviderContext;
|
|
16
|
+
|
|
17
|
+
const { startClass, endClass } = getPositionStyleClasses(
|
|
18
|
+
column.leftOffset,
|
|
19
|
+
column.width + column.leftOffset
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const combinedClasses = combineClasses([
|
|
23
|
+
styles[endClass],
|
|
24
|
+
styles[startClass],
|
|
25
|
+
column.styleClass
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const columnProps = isInsideEditor
|
|
29
|
+
? {
|
|
30
|
+
'data-dot': 'column',
|
|
31
|
+
'data-testid': 'column'
|
|
32
|
+
}
|
|
33
|
+
: {};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div {...columnProps} className={combinedClasses}>
|
|
37
|
+
{column.containers.map((container) => (
|
|
38
|
+
<Container
|
|
39
|
+
key={`${container.identifier}-${container.uuid}`}
|
|
40
|
+
containerRef={container}
|
|
41
|
+
/>
|
|
42
|
+
))}
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
2
|
+
|
|
3
|
+
import { render, screen } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
import * as dotcmsClient from '@dotcms/client';
|
|
6
|
+
|
|
7
|
+
import { Container } from './Container';
|
|
8
|
+
|
|
9
|
+
import { MockContextRender, mockPageContext } from '../../mocks/mockPageContext';
|
|
10
|
+
|
|
11
|
+
describe('Container', () => {
|
|
12
|
+
// Mock data for your context and container
|
|
13
|
+
jest.spyOn(dotcmsClient, 'isInsideEditor').mockReturnValue(true);
|
|
14
|
+
|
|
15
|
+
describe('with contentlets', () => {
|
|
16
|
+
const mockContainerRef = {
|
|
17
|
+
identifier: 'container-1',
|
|
18
|
+
uuid: '1',
|
|
19
|
+
containers: []
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
it('renders NoContent component for unsupported content types', () => {
|
|
23
|
+
const updatedContext = {
|
|
24
|
+
...mockPageContext,
|
|
25
|
+
components: {}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
render(
|
|
29
|
+
<MockContextRender mockContext={updatedContext}>
|
|
30
|
+
<Container containerRef={mockContainerRef} />
|
|
31
|
+
</MockContextRender>
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
expect(screen.getByTestId('no-component')).toHaveTextContent(
|
|
35
|
+
'No Component for content-type-1'
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('without contentlets', () => {
|
|
40
|
+
const mockContainerRef = {
|
|
41
|
+
identifier: 'container-2',
|
|
42
|
+
uuid: '2',
|
|
43
|
+
containers: []
|
|
44
|
+
};
|
|
45
|
+
it('renders EmptyContainer component in editor mode', () => {
|
|
46
|
+
const updatedContext = {
|
|
47
|
+
...mockPageContext,
|
|
48
|
+
components: {},
|
|
49
|
+
isInsideEditor: true
|
|
50
|
+
};
|
|
51
|
+
render(
|
|
52
|
+
<MockContextRender mockContext={updatedContext}>
|
|
53
|
+
<Container containerRef={mockContainerRef} />
|
|
54
|
+
</MockContextRender>
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(screen.getByTestId('empty-container')).toHaveTextContent(
|
|
58
|
+
'This container is empty.'
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('dont render EmptyContainer component outside editor mode', () => {
|
|
63
|
+
jest.spyOn(dotcmsClient, 'isInsideEditor').mockReturnValue(false);
|
|
64
|
+
|
|
65
|
+
const updatedContext = {
|
|
66
|
+
...mockPageContext,
|
|
67
|
+
components: {},
|
|
68
|
+
isInsideEditor: false
|
|
69
|
+
};
|
|
70
|
+
render(
|
|
71
|
+
<MockContextRender mockContext={updatedContext}>
|
|
72
|
+
<Container containerRef={mockContainerRef} />
|
|
73
|
+
</MockContextRender>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
expect(screen.queryByTestId('empty-container')).toBeNull();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Add tests for pointer events, dynamic component rendering, and other scenarios...
|
|
82
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { PageContext } from '../../contexts/PageContext';
|
|
4
|
+
import { getContainersData } from '../../utils/utils';
|
|
5
|
+
import { PageProviderContext } from '../PageProvider/PageProvider';
|
|
6
|
+
|
|
7
|
+
const FAKE_CONTENLET = {
|
|
8
|
+
identifier: 'TEMP_EMPTY_CONTENTLET',
|
|
9
|
+
title: 'TEMP_EMPTY_CONTENTLET',
|
|
10
|
+
contentType: 'TEMP_EMPTY_CONTENTLET_TYPE',
|
|
11
|
+
inode: 'TEMPY_EMPTY_CONTENTLET_INODE',
|
|
12
|
+
widgetTitle: 'TEMP_EMPTY_CONTENTLET',
|
|
13
|
+
onNumberOfPages: 1
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function EmptyContainer() {
|
|
17
|
+
return (
|
|
18
|
+
<div
|
|
19
|
+
data-testid="empty-container"
|
|
20
|
+
style={{
|
|
21
|
+
width: '100%',
|
|
22
|
+
backgroundColor: '#ECF0FD',
|
|
23
|
+
display: 'flex',
|
|
24
|
+
justifyContent: 'center',
|
|
25
|
+
alignItems: 'center',
|
|
26
|
+
color: '#030E32',
|
|
27
|
+
height: '10rem'
|
|
28
|
+
}}>
|
|
29
|
+
This container is empty.
|
|
30
|
+
</div>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function NoContent({ contentType }: { readonly contentType: string }) {
|
|
35
|
+
return <div data-testid="no-component">No Component for {contentType}</div>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ContainerProps {
|
|
39
|
+
readonly containerRef: PageProviderContext['layout']['body']['rows'][0]['columns'][0]['containers'][0];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function Container({ containerRef }: ContainerProps) {
|
|
43
|
+
const { isInsideEditor } = useContext(PageContext) as PageProviderContext;
|
|
44
|
+
|
|
45
|
+
const { identifier, uuid } = containerRef;
|
|
46
|
+
|
|
47
|
+
// Get the containers from the global context
|
|
48
|
+
const { containers, components } = useContext<PageProviderContext | null>(
|
|
49
|
+
PageContext
|
|
50
|
+
) as PageProviderContext;
|
|
51
|
+
|
|
52
|
+
const { acceptTypes, contentlets, maxContentlets, variantId, path } = getContainersData(
|
|
53
|
+
containers,
|
|
54
|
+
containerRef
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const updatedContentlets =
|
|
58
|
+
contentlets.length === 0 && isInsideEditor ? [FAKE_CONTENLET] : contentlets;
|
|
59
|
+
|
|
60
|
+
const container = {
|
|
61
|
+
acceptTypes,
|
|
62
|
+
identifier: path ?? identifier,
|
|
63
|
+
maxContentlets,
|
|
64
|
+
variantId,
|
|
65
|
+
uuid
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const renderContentlets = updatedContentlets.map((contentlet) => {
|
|
69
|
+
const ContentTypeComponent = components[contentlet.contentType] || NoContent;
|
|
70
|
+
|
|
71
|
+
const Component =
|
|
72
|
+
contentlet.identifier === 'TEMP_EMPTY_CONTENTLET'
|
|
73
|
+
? EmptyContainer
|
|
74
|
+
: ContentTypeComponent;
|
|
75
|
+
|
|
76
|
+
return isInsideEditor ? (
|
|
77
|
+
<div
|
|
78
|
+
data-dot-object="contentlet"
|
|
79
|
+
data-dot-identifier={contentlet.identifier}
|
|
80
|
+
data-dot-title={contentlet.widgetTitle || contentlet.title}
|
|
81
|
+
data-dot-inode={contentlet.inode}
|
|
82
|
+
data-dot-type={contentlet.contentType}
|
|
83
|
+
data-dot-container={JSON.stringify(container)}
|
|
84
|
+
key={contentlet.identifier}>
|
|
85
|
+
<Component {...contentlet} />
|
|
86
|
+
</div>
|
|
87
|
+
) : (
|
|
88
|
+
<Component {...contentlet} key={contentlet.identifier} />
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return isInsideEditor ? (
|
|
93
|
+
<div
|
|
94
|
+
data-dot-object="container"
|
|
95
|
+
data-dot-accept-types={acceptTypes}
|
|
96
|
+
data-dot-identifier={path ?? identifier}
|
|
97
|
+
data-max-contentlets={maxContentlets}
|
|
98
|
+
data-uuid={uuid}>
|
|
99
|
+
{renderContentlets}
|
|
100
|
+
</div>
|
|
101
|
+
) : (
|
|
102
|
+
// eslint-disable-next-line react/jsx-no-useless-fragment
|
|
103
|
+
<>{renderContentlets}</>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { ElementRef, ForwardRefExoticComponent } from 'react';
|
|
4
|
+
|
|
5
|
+
import { DotcmsLayout } from './DotcmsLayout';
|
|
6
|
+
|
|
7
|
+
import { mockPageContext } from '../../mocks/mockPageContext';
|
|
8
|
+
|
|
9
|
+
jest.mock('../Row/Row', () => {
|
|
10
|
+
const { forwardRef } = jest.requireActual('react');
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
Row: forwardRef(
|
|
14
|
+
(
|
|
15
|
+
{ children }: { children: JSX.Element },
|
|
16
|
+
ref: ElementRef<ForwardRefExoticComponent<unknown>>
|
|
17
|
+
) => (
|
|
18
|
+
<div data-testid="mockRow" ref={ref}>
|
|
19
|
+
{children}
|
|
20
|
+
</div>
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
jest.mock('../PageProvider/PageProvider', () => {
|
|
27
|
+
return {
|
|
28
|
+
PageProvider: ({ children }: { children: JSX.Element }) => (
|
|
29
|
+
<div data-testid="mockPageProvider">{children}</div>
|
|
30
|
+
)
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('DotcmsLayout', () => {
|
|
35
|
+
it('renders correctly with PageProvider and rows', () => {
|
|
36
|
+
render(<DotcmsLayout entity={{ ...mockPageContext, isInsideEditor: true }} />);
|
|
37
|
+
expect(screen.getAllByTestId('mockRow').length).toBe(
|
|
38
|
+
mockPageContext.layout.body.rows.length
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { DotCMSPageEditorConfig } from '@dotcms/client';
|
|
2
|
+
|
|
3
|
+
import { useDotcmsEditor } from '../../hooks/useDotcmsEditor';
|
|
4
|
+
import { PageProvider, PageProviderContext } from '../PageProvider/PageProvider';
|
|
5
|
+
import { Row } from '../Row/Row';
|
|
3
6
|
/**
|
|
4
7
|
* `DotcmsPageProps` is a type that defines the properties for the `DotcmsLayout` component.
|
|
5
8
|
* It includes a readonly `entity` property that represents the context for a DotCMS page.
|
|
@@ -20,7 +23,10 @@ export type DotcmsPageProps = {
|
|
|
20
23
|
* @readonly
|
|
21
24
|
*/
|
|
22
25
|
readonly entity: PageProviderContext;
|
|
26
|
+
|
|
27
|
+
readonly config?: DotCMSPageEditorConfig;
|
|
23
28
|
};
|
|
29
|
+
|
|
24
30
|
/**
|
|
25
31
|
* `DotcmsLayout` is a functional component that renders a layout for a DotCMS page.
|
|
26
32
|
* It takes a `DotcmsPageProps` object as a parameter and returns a JSX element.
|
|
@@ -29,4 +35,14 @@ export type DotcmsPageProps = {
|
|
|
29
35
|
* @param {DotcmsPageProps} props - The properties for the DotCMS page.
|
|
30
36
|
* @returns {JSX.Element} - A JSX element that represents the layout for a DotCMS page.
|
|
31
37
|
*/
|
|
32
|
-
export
|
|
38
|
+
export function DotcmsLayout({ entity, config }: DotcmsPageProps): JSX.Element {
|
|
39
|
+
const isInsideEditor = useDotcmsEditor(config);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<PageProvider entity={{ ...entity, isInsideEditor }}>
|
|
43
|
+
{entity.layout.body.rows.map((row, index) => (
|
|
44
|
+
<Row key={index} row={row} />
|
|
45
|
+
))}
|
|
46
|
+
</PageProvider>
|
|
47
|
+
);
|
|
48
|
+
}
|