the-react 1.0.0 → 1.0.1
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/package.json +26 -5
- package/src/my-react/index.ts +0 -1
- package/src/router-dom/Router.tsx +56 -19
package/package.json
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "the-react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A React library for TheBugs Project",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.ts",
|
|
10
|
+
"import": "./src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./jsx-runtime": {
|
|
13
|
+
"types": "./src/types/jsx.ts",
|
|
14
|
+
"import": "./src/jsx-runtime"
|
|
15
|
+
},
|
|
16
|
+
"./jsx-dev-runtime": {
|
|
17
|
+
"types": "./src/types/jsx.ts",
|
|
18
|
+
"import": "./src/jsx-dev-runtime"
|
|
19
|
+
},
|
|
20
|
+
"./router-dom": {
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"import": "./src/router-dom/index.ts"
|
|
23
|
+
},
|
|
24
|
+
"./hooks": {
|
|
25
|
+
"types": "./src/index.ts",
|
|
26
|
+
"import": "./src/hooks/index.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
8
29
|
"scripts": {
|
|
9
30
|
"build": "vite build",
|
|
10
31
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -31,4 +52,4 @@
|
|
|
31
52
|
"vite": "^7.3.1",
|
|
32
53
|
"vite-tsconfig-paths": "^6.1.1"
|
|
33
54
|
}
|
|
34
|
-
}
|
|
55
|
+
}
|
package/src/my-react/index.ts
CHANGED
|
@@ -363,7 +363,6 @@ export class ComponentInstance<PropsType extends ComponentPropsType> {
|
|
|
363
363
|
if (typeof vNode !== "string" && vNode.type === "component") {
|
|
364
364
|
const compInstance = this.instanceMap.get(vNode.key) as ComponentInstance<any>;
|
|
365
365
|
const compDom = compInstance.domElement;
|
|
366
|
-
console.log("compInstance", compInstance)
|
|
367
366
|
if (!compDom) {
|
|
368
367
|
branchIndex++;
|
|
369
368
|
continue
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { JSXElementType } from "
|
|
1
|
+
import type { JSXElementType } from "@my-react/types/jsx"
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
interface IRouterProps {
|
|
4
5
|
path: string
|
|
5
6
|
children: JSXElementType
|
|
6
7
|
currentPath: string
|
|
8
|
+
currentSearch?: string
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export function matchPath(pattern: string, path: string) {
|
|
@@ -44,36 +46,71 @@ export function matchPath(pattern: string, path: string) {
|
|
|
44
46
|
return { matches: true, params }
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
export function
|
|
48
|
-
const
|
|
49
|
-
|
|
49
|
+
export function parseSearch(search: string): Record<string, string> {
|
|
50
|
+
const params: Record<string, string> = {}
|
|
51
|
+
if (!search) return params
|
|
52
|
+
|
|
53
|
+
const queryString = search.startsWith('?') ? search.slice(1) : search
|
|
54
|
+
const pairs = queryString.split('&')
|
|
55
|
+
|
|
56
|
+
for (const pair of pairs) {
|
|
57
|
+
const [key, value] = pair.split('=')
|
|
58
|
+
if (key) {
|
|
59
|
+
params[decodeURIComponent(key)] = decodeURIComponent(value || '').replaceAll('+', ' ')
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return params
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function Router({path, children, currentPath, currentSearch}: IRouterProps){
|
|
66
|
+
const { matches, params: pathParams } = matchPath(path, currentPath)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
console.log(`Router ${path}:`, {
|
|
70
|
+
currentPath,
|
|
71
|
+
currentSearch,
|
|
72
|
+
pathParams,
|
|
73
|
+
children,
|
|
74
|
+
})
|
|
50
75
|
|
|
51
76
|
if (!matches && path != "*") return null
|
|
52
77
|
|
|
53
78
|
if (children.type === 'component') {
|
|
54
|
-
children.props = {
|
|
79
|
+
children.props = {
|
|
80
|
+
...children.props,
|
|
81
|
+
...pathParams,
|
|
82
|
+
}
|
|
83
|
+
if (currentSearch){
|
|
84
|
+
const queryParams = parseSearch(currentSearch)
|
|
85
|
+
children.props = {
|
|
86
|
+
...children.props,
|
|
87
|
+
...queryParams
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
console.log(currentSearch)
|
|
91
|
+
}
|
|
55
92
|
console.log(`render children ${path}`)
|
|
56
|
-
return
|
|
93
|
+
return <div>{children}</div>
|
|
57
94
|
}
|
|
58
95
|
|
|
59
|
-
return
|
|
96
|
+
return <div>{children}</div>
|
|
60
97
|
}
|
|
61
98
|
|
|
62
|
-
interface ISwitchrProps{
|
|
99
|
+
interface ISwitchrProps {
|
|
63
100
|
children: any[]
|
|
64
101
|
currentPath: string
|
|
65
102
|
}
|
|
66
103
|
|
|
67
104
|
export function Switch({ currentPath, children }: ISwitchrProps) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
105
|
+
const childrenArray = Array.isArray(children) ? children : [children];
|
|
106
|
+
|
|
107
|
+
for (const child of childrenArray) {
|
|
108
|
+
if (child && child.type === 'component' && child.props.path) {
|
|
109
|
+
const { matches } = matchPath(child.props.path, currentPath);
|
|
110
|
+
if (matches || child.props.path == "*") {
|
|
111
|
+
return <div>{child}</div>;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
76
114
|
}
|
|
77
|
-
|
|
78
|
-
return null;
|
|
115
|
+
return null;
|
|
79
116
|
}
|