@forge/react 11.16.2-next.1 → 11.16.2-next.1-experimental-49a346a
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/CHANGELOG.md +22 -0
- package/out/router/components/ParamsContext.d.ts +3 -0
- package/out/router/components/ParamsContext.d.ts.map +1 -0
- package/out/router/components/ParamsContext.js +5 -0
- package/out/router/components/Route.d.ts +7 -0
- package/out/router/components/Route.d.ts.map +1 -0
- package/out/router/components/Route.js +25 -0
- package/out/router/components/Router.d.ts +7 -0
- package/out/router/components/Router.d.ts.map +1 -0
- package/out/router/components/Router.js +43 -0
- package/out/router/components/RouterContext.d.ts +9 -0
- package/out/router/components/RouterContext.d.ts.map +1 -0
- package/out/router/components/RouterContext.js +5 -0
- package/out/router/components/__test__/Router.test.d.ts +2 -0
- package/out/router/components/__test__/Router.test.d.ts.map +1 -0
- package/out/router/components/__test__/Router.test.js +77 -0
- package/out/router/components/index.d.ts +3 -0
- package/out/router/components/index.d.ts.map +1 -0
- package/out/router/components/index.js +7 -0
- package/out/router/hooks/__test__/useLocation.test.d.ts +2 -0
- package/out/router/hooks/__test__/useLocation.test.d.ts.map +1 -0
- package/out/router/hooks/__test__/useLocation.test.js +59 -0
- package/out/router/hooks/__test__/useNavigate.test.d.ts +2 -0
- package/out/router/hooks/__test__/useNavigate.test.d.ts.map +1 -0
- package/out/router/hooks/__test__/useNavigate.test.js +159 -0
- package/out/router/hooks/__test__/useParams.test.d.ts +2 -0
- package/out/router/hooks/__test__/useParams.test.d.ts.map +1 -0
- package/out/router/hooks/__test__/useParams.test.js +69 -0
- package/out/router/hooks/useLocation.d.ts +3 -0
- package/out/router/hooks/useLocation.d.ts.map +1 -0
- package/out/router/hooks/useLocation.js +13 -0
- package/out/router/hooks/useNavigate.d.ts +6 -0
- package/out/router/hooks/useNavigate.d.ts.map +1 -0
- package/out/router/hooks/useNavigate.js +45 -0
- package/out/router/hooks/useParams.d.ts +2 -0
- package/out/router/hooks/useParams.d.ts.map +1 -0
- package/out/router/hooks/useParams.js +13 -0
- package/out/router/index.d.ts +5 -0
- package/out/router/index.d.ts.map +1 -0
- package/out/router/index.js +12 -0
- package/out/router/utils/__test__/matchPath.test.d.ts +2 -0
- package/out/router/utils/__test__/matchPath.test.d.ts.map +1 -0
- package/out/router/utils/__test__/matchPath.test.js +56 -0
- package/out/router/utils/matchPath.d.ts +5 -0
- package/out/router/utils/matchPath.d.ts.map +1 -0
- package/out/router/utils/matchPath.js +34 -0
- package/out/router/utils/test-utils.d.ts +10 -0
- package/out/router/utils/test-utils.d.ts.map +1 -0
- package/out/router/utils/test-utils.js +23 -0
- package/package.json +7 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useNavigate = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const RouterContext_1 = require("../components/RouterContext");
|
|
6
|
+
const resolvePath = (currentPathname, to) => {
|
|
7
|
+
if (to.startsWith('/')) {
|
|
8
|
+
return to;
|
|
9
|
+
}
|
|
10
|
+
const currentPath = currentPathname.endsWith('/') ? currentPathname.slice(0, -1) : currentPathname;
|
|
11
|
+
const combined = currentPath + '/' + to;
|
|
12
|
+
const segments = combined.split('/').filter(Boolean);
|
|
13
|
+
const resolved = [];
|
|
14
|
+
for (const segment of segments) {
|
|
15
|
+
if (segment === '..') {
|
|
16
|
+
resolved.pop();
|
|
17
|
+
}
|
|
18
|
+
else if (segment !== '.') {
|
|
19
|
+
resolved.push(segment);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return '/' + resolved.join('/');
|
|
23
|
+
};
|
|
24
|
+
const useNavigate = () => {
|
|
25
|
+
const context = (0, react_1.useContext)(RouterContext_1.RouterContext);
|
|
26
|
+
if (!context) {
|
|
27
|
+
throw new Error('useNavigate must be used within a Router component');
|
|
28
|
+
}
|
|
29
|
+
const { history } = context;
|
|
30
|
+
const navigate = (0, react_1.useCallback)((to, options) => {
|
|
31
|
+
if (typeof to === 'number') {
|
|
32
|
+
history.go(to);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const path = resolvePath(history.location.pathname, to);
|
|
36
|
+
if (options?.replace) {
|
|
37
|
+
history.replace(path);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
history.push(path);
|
|
41
|
+
}
|
|
42
|
+
}, [history]);
|
|
43
|
+
return navigate;
|
|
44
|
+
};
|
|
45
|
+
exports.useNavigate = useNavigate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useParams.d.ts","sourceRoot":"","sources":["../../../src/router/hooks/useParams.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS,QAAO,OAAO,MAAM,EAAE,MAAM,CAMjD,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useParams = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const ParamsContext_1 = require("../components/ParamsContext");
|
|
6
|
+
const useParams = () => {
|
|
7
|
+
const context = (0, react_1.useContext)(ParamsContext_1.ParamsContext);
|
|
8
|
+
if (context === null) {
|
|
9
|
+
throw new Error('useParams must be used within a Route component');
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
};
|
|
13
|
+
exports.useParams = useParams;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Router, Route, type RouterProps, type RouteProps } from './components';
|
|
2
|
+
export { useNavigate } from './hooks/useNavigate';
|
|
3
|
+
export { useLocation } from './hooks/useLocation';
|
|
4
|
+
export { useParams } from './hooks/useParams';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useParams = exports.useLocation = exports.useNavigate = exports.Route = exports.Router = void 0;
|
|
4
|
+
var components_1 = require("./components");
|
|
5
|
+
Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return components_1.Router; } });
|
|
6
|
+
Object.defineProperty(exports, "Route", { enumerable: true, get: function () { return components_1.Route; } });
|
|
7
|
+
var useNavigate_1 = require("./hooks/useNavigate");
|
|
8
|
+
Object.defineProperty(exports, "useNavigate", { enumerable: true, get: function () { return useNavigate_1.useNavigate; } });
|
|
9
|
+
var useLocation_1 = require("./hooks/useLocation");
|
|
10
|
+
Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return useLocation_1.useLocation; } });
|
|
11
|
+
var useParams_1 = require("./hooks/useParams");
|
|
12
|
+
Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return useParams_1.useParams; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchPath.test.d.ts","sourceRoot":"","sources":["../../../../src/router/utils/__test__/matchPath.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const matchPath_1 = require("../matchPath");
|
|
4
|
+
describe('matchPath', () => {
|
|
5
|
+
it('matches exact static paths', () => {
|
|
6
|
+
expect((0, matchPath_1.matchPath)('/', '/')).toEqual({ params: {} });
|
|
7
|
+
expect((0, matchPath_1.matchPath)('/settings', '/settings')).toEqual({ params: {} });
|
|
8
|
+
});
|
|
9
|
+
it('returns null for non-matching static paths', () => {
|
|
10
|
+
expect((0, matchPath_1.matchPath)('/settings', '/about')).toBeNull();
|
|
11
|
+
expect((0, matchPath_1.matchPath)('/settings', '/')).toBeNull();
|
|
12
|
+
});
|
|
13
|
+
it('extracts a single param', () => {
|
|
14
|
+
expect((0, matchPath_1.matchPath)('/posts/:id', '/posts/123')).toEqual({ params: { id: '123' } });
|
|
15
|
+
});
|
|
16
|
+
it('extracts multiple params', () => {
|
|
17
|
+
expect((0, matchPath_1.matchPath)('/posts/:postId/comments/:commentId', '/posts/42/comments/7')).toEqual({
|
|
18
|
+
params: { postId: '42', commentId: '7' }
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
it('returns null when segment count differs', () => {
|
|
22
|
+
expect((0, matchPath_1.matchPath)('/posts/:id', '/posts')).toBeNull();
|
|
23
|
+
expect((0, matchPath_1.matchPath)('/posts/:id', '/posts/123/extra')).toBeNull();
|
|
24
|
+
});
|
|
25
|
+
it('returns null when static segments do not match', () => {
|
|
26
|
+
expect((0, matchPath_1.matchPath)('/posts/:id', '/users/123')).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
it('matches paths with mixed static and dynamic segments', () => {
|
|
29
|
+
expect((0, matchPath_1.matchPath)('/users/:userId/posts/:postId', '/users/alice/posts/99')).toEqual({
|
|
30
|
+
params: { userId: 'alice', postId: '99' }
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
it('matches catchall pattern and captures remaining segments', () => {
|
|
34
|
+
expect((0, matchPath_1.matchPath)('/files/*', '/files/docs/report.pdf')).toEqual({
|
|
35
|
+
params: { '*': 'docs/report.pdf' }
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
it('matches catchall with empty remainder', () => {
|
|
39
|
+
expect((0, matchPath_1.matchPath)('/files/*', '/files')).toEqual({
|
|
40
|
+
params: { '*': '' }
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
it('matches bare catchall against any path', () => {
|
|
44
|
+
expect((0, matchPath_1.matchPath)('*', '/anything/at/all')).toEqual({
|
|
45
|
+
params: { '*': 'anything/at/all' }
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it('matches catchall combined with dynamic params', () => {
|
|
49
|
+
expect((0, matchPath_1.matchPath)('/posts/:id/*', '/posts/42/comments/7')).toEqual({
|
|
50
|
+
params: { id: '42', '*': 'comments/7' }
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it('returns null when static segments before catchall do not match', () => {
|
|
54
|
+
expect((0, matchPath_1.matchPath)('/files/*', '/users/alice')).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchPath.d.ts","sourceRoot":"","sources":["../../../src/router/utils/matchPath.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED,eAAO,MAAM,SAAS,YAAa,MAAM,YAAY,MAAM,KAAG,WAAW,GAAG,IAiC3E,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.matchPath = void 0;
|
|
4
|
+
const matchPath = (pattern, pathname) => {
|
|
5
|
+
const patternSegments = pattern.split('/').filter(Boolean);
|
|
6
|
+
const pathSegments = pathname.split('/').filter(Boolean);
|
|
7
|
+
const hasCatchall = patternSegments.length > 0 && patternSegments[patternSegments.length - 1] === '*';
|
|
8
|
+
const matchSegments = hasCatchall ? patternSegments.slice(0, -1) : patternSegments;
|
|
9
|
+
if (hasCatchall) {
|
|
10
|
+
if (pathSegments.length < matchSegments.length) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else if (patternSegments.length !== pathSegments.length) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const params = {};
|
|
18
|
+
for (let i = 0; i < matchSegments.length; i++) {
|
|
19
|
+
const patternSegment = matchSegments[i];
|
|
20
|
+
const pathSegment = pathSegments[i];
|
|
21
|
+
if (patternSegment.startsWith(':')) {
|
|
22
|
+
const paramName = patternSegment.slice(1);
|
|
23
|
+
params[paramName] = pathSegment;
|
|
24
|
+
}
|
|
25
|
+
else if (patternSegment !== pathSegment) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (hasCatchall) {
|
|
30
|
+
params['*'] = pathSegments.slice(matchSegments.length).join('/');
|
|
31
|
+
}
|
|
32
|
+
return { params };
|
|
33
|
+
};
|
|
34
|
+
exports.matchPath = matchPath;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { History } from 'history';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a mock history object that emulates the v4 history API.
|
|
4
|
+
* The bridge returns a v4-style history object at runtime where `listen`
|
|
5
|
+
* calls back with `location` directly, but `createMemoryHistory` from
|
|
6
|
+
* the installed history v5 package passes `{ action, location }`.
|
|
7
|
+
* This wrapper adapts the v5 listener to behave like v4.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createMockHistory: (initialPath?: string) => History;
|
|
10
|
+
//# sourceMappingURL=test-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../../src/router/utils/test-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvD;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,4BAAwB,OAYrD,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMockHistory = void 0;
|
|
4
|
+
const history_1 = require("history");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a mock history object that emulates the v4 history API.
|
|
7
|
+
* The bridge returns a v4-style history object at runtime where `listen`
|
|
8
|
+
* calls back with `location` directly, but `createMemoryHistory` from
|
|
9
|
+
* the installed history v5 package passes `{ action, location }`.
|
|
10
|
+
* This wrapper adapts the v5 listener to behave like v4.
|
|
11
|
+
*/
|
|
12
|
+
const createMockHistory = (initialPath = '/') => {
|
|
13
|
+
const history = (0, history_1.createMemoryHistory)({ initialEntries: [initialPath] });
|
|
14
|
+
const originalListen = history.listen.bind(history);
|
|
15
|
+
history.listen = (callback) => {
|
|
16
|
+
return originalListen(({ location }) => callback(location));
|
|
17
|
+
};
|
|
18
|
+
jest.spyOn(history, 'push');
|
|
19
|
+
jest.spyOn(history, 'replace');
|
|
20
|
+
jest.spyOn(history, 'go');
|
|
21
|
+
return history;
|
|
22
|
+
};
|
|
23
|
+
exports.createMockHistory = createMockHistory;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forge/react",
|
|
3
|
-
"version": "11.16.2-next.1",
|
|
3
|
+
"version": "11.16.2-next.1-experimental-49a346a",
|
|
4
4
|
"description": "Forge React reconciler",
|
|
5
5
|
"author": "Atlassian",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
@@ -26,13 +26,17 @@
|
|
|
26
26
|
"./global": {
|
|
27
27
|
"types": "./out/components/global/index.d.ts",
|
|
28
28
|
"default": "./out/components/global/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./router": {
|
|
31
|
+
"types": "./out/router/index.d.ts",
|
|
32
|
+
"default": "./out/router/index.js"
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
"dependencies": {
|
|
32
36
|
"@atlaskit/adf-schema": "^48.0.0",
|
|
33
37
|
"@atlaskit/adf-utils": "^19.19.0",
|
|
34
38
|
"@atlaskit/forge-react-types": "^1.3.0",
|
|
35
|
-
"@forge/bridge": "^5.17.0-next.
|
|
39
|
+
"@forge/bridge": "^5.17.0-next.6-experimental-49a346a",
|
|
36
40
|
"@forge/egress": "^2.3.2",
|
|
37
41
|
"@forge/i18n": "0.0.7",
|
|
38
42
|
"@types/react": "^18.2.64",
|
|
@@ -46,6 +50,7 @@
|
|
|
46
50
|
},
|
|
47
51
|
"devDependencies": {
|
|
48
52
|
"@testing-library/react-hooks": "^8.0.1",
|
|
53
|
+
"history": "5.3.0",
|
|
49
54
|
"ts-morph": "^22.0.0",
|
|
50
55
|
"ts-node": "^10.9.2"
|
|
51
56
|
},
|