react-components-rails 1.0.0.beta1 → 1.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -3
- data/README.md +8 -2
- data/lib/react-components-rails.rb +3 -0
- data/package.json +20 -17
- data/pnpm-lock.yaml +1078 -0
- data/react-components-rails.gemspec +2 -2
- data/src/index.ts +38 -9
- data/tsconfig.json +11 -8
- metadata +4 -17
- data/yarn.lock +0 -5101
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "react-components-rails"
|
8
8
|
spec.licenses = ["MIT"]
|
9
|
-
spec.version = "1.0.0.
|
9
|
+
spec.version = "1.0.0.beta2"
|
10
10
|
spec.authors = ["Renaud Chaput"]
|
11
11
|
spec.email = ["renchap@gmail.com"]
|
12
12
|
|
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.required_ruby_version = ">= 2.7.0"
|
25
25
|
|
26
26
|
spec.add_development_dependency "bundler", "~> 2.3"
|
27
|
-
spec.add_development_dependency "rake", "~>
|
27
|
+
# spec.add_development_dependency "rake", "~> 12.0"
|
28
28
|
# spec.add_development_dependency "minitest", "~> 5.0"
|
29
29
|
# spec.add_development_dependency "capybara"
|
30
30
|
# spec.add_development_dependency "selenium-webdriver"
|
data/src/index.ts
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
import React from "react"
|
2
2
|
import ReactDOM from "react-dom"
|
3
|
-
import
|
4
|
-
import keys from "lodash
|
5
|
-
import assign from "lodash/assign"
|
6
|
-
import omit from "lodash/omit"
|
3
|
+
import ReactDOMClient, { Root } from "react-dom/client"
|
4
|
+
import { intersection, keys, assign, omit } from "lodash"
|
7
5
|
// import ujs from './ujs'
|
8
6
|
|
9
7
|
const CLASS_ATTRIBUTE_NAME = "data-react-class"
|
@@ -17,6 +15,11 @@ declare global {
|
|
17
15
|
|
18
16
|
const ReactComponentsRails = {
|
19
17
|
registeredComponents: {} as { [name: string]: React.ComponentType },
|
18
|
+
mountedRoots: [] as Root[],
|
19
|
+
ReactDOMClient: undefined as
|
20
|
+
| typeof ReactDOM
|
21
|
+
| typeof ReactDOMClient
|
22
|
+
| undefined,
|
20
23
|
|
21
24
|
render(node: Element, component: React.ComponentType) {
|
22
25
|
const propsJson = node.getAttribute(PROPS_ATTRIBUTE_NAME)
|
@@ -24,7 +27,15 @@ const ReactComponentsRails = {
|
|
24
27
|
|
25
28
|
const reactElement = React.createElement(component, props)
|
26
29
|
|
27
|
-
|
30
|
+
if (this.ReactDOMClient) {
|
31
|
+
if ("createRoot" in this.ReactDOMClient) {
|
32
|
+
const root = this.ReactDOMClient.createRoot(node)
|
33
|
+
root.render(reactElement)
|
34
|
+
this.mountedRoots.push(root)
|
35
|
+
} else {
|
36
|
+
this.ReactDOMClient.render(reactElement, node)
|
37
|
+
}
|
38
|
+
}
|
28
39
|
},
|
29
40
|
|
30
41
|
registerComponents(components: { [name: string]: React.Component }) {
|
@@ -43,9 +54,16 @@ const ReactComponentsRails = {
|
|
43
54
|
},
|
44
55
|
|
45
56
|
unmountComponents() {
|
46
|
-
|
47
|
-
|
48
|
-
|
57
|
+
if (this.ReactDOMClient) {
|
58
|
+
if ("createRoot" in this.ReactDOMClient) {
|
59
|
+
this.mountedRoots.forEach((root) => root.unmount())
|
60
|
+
this.mountedRoots = []
|
61
|
+
} else {
|
62
|
+
const mounted = document.querySelectorAll(`[${CLASS_ATTRIBUTE_NAME}]`)
|
63
|
+
for (let i = 0; i < mounted.length; i += 1) {
|
64
|
+
this.ReactDOMClient.unmountComponentAtNode(mounted[i])
|
65
|
+
}
|
66
|
+
}
|
49
67
|
}
|
50
68
|
},
|
51
69
|
|
@@ -76,7 +94,8 @@ const ReactComponentsRails = {
|
|
76
94
|
}
|
77
95
|
},
|
78
96
|
|
79
|
-
setup(components = {}) {
|
97
|
+
async setup(components = {}) {
|
98
|
+
await this.loadReactDOMClient()
|
80
99
|
if (typeof window.ReactComponentsRails === "undefined") {
|
81
100
|
window.ReactComponentsRails = this
|
82
101
|
// ujs.setup(this.mountComponents.bind(this), this.unmountComponents.bind(this))
|
@@ -85,6 +104,16 @@ const ReactComponentsRails = {
|
|
85
104
|
window.ReactComponentsRails.registerComponents(components)
|
86
105
|
window.ReactComponentsRails.mountComponents()
|
87
106
|
},
|
107
|
+
|
108
|
+
async loadReactDOMClient() {
|
109
|
+
if (this.ReactDOMClient) return
|
110
|
+
|
111
|
+
try {
|
112
|
+
this.ReactDOMClient = await import("react-dom/client")
|
113
|
+
} catch (e) {
|
114
|
+
this.ReactDOMClient = ReactDOM
|
115
|
+
}
|
116
|
+
},
|
88
117
|
}
|
89
118
|
|
90
119
|
export default ReactComponentsRails
|
data/tsconfig.json
CHANGED
@@ -11,8 +11,11 @@
|
|
11
11
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
12
12
|
|
13
13
|
/* Language and Environment */
|
14
|
-
"target": "es2016"
|
15
|
-
"lib": [
|
14
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
15
|
+
"lib": [
|
16
|
+
"DOM",
|
17
|
+
"ES2016"
|
18
|
+
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
|
16
19
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
17
20
|
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
18
21
|
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
@@ -24,8 +27,8 @@
|
|
24
27
|
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
25
28
|
|
26
29
|
/* Modules */
|
27
|
-
"module": "
|
28
|
-
|
30
|
+
"module": "CommonJS" /* Specify what module code is generated. */,
|
31
|
+
"rootDir": "./src" /* Specify the root folder within your source files. */,
|
29
32
|
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
30
33
|
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
31
34
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
@@ -69,12 +72,12 @@
|
|
69
72
|
/* Interop Constraints */
|
70
73
|
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
71
74
|
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
72
|
-
"esModuleInterop": true
|
75
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
73
76
|
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
74
|
-
"forceConsistentCasingInFileNames": true
|
77
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
75
78
|
|
76
79
|
/* Type Checking */
|
77
|
-
"strict": true
|
80
|
+
"strict": true /* Enable all strict type-checking options. */,
|
78
81
|
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
79
82
|
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
80
83
|
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
@@ -96,6 +99,6 @@
|
|
96
99
|
|
97
100
|
/* Completeness */
|
98
101
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
99
|
-
"skipLibCheck": true
|
102
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
100
103
|
}
|
101
104
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: react-components-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renaud Chaput
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.3'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '13.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '13.0'
|
41
27
|
description:
|
42
28
|
email:
|
43
29
|
- renchap@gmail.com
|
@@ -56,12 +42,13 @@ files:
|
|
56
42
|
- lib/component.rb
|
57
43
|
- lib/helpers.rb
|
58
44
|
- lib/railtie.rb
|
45
|
+
- lib/react-components-rails.rb
|
59
46
|
- package.json
|
47
|
+
- pnpm-lock.yaml
|
60
48
|
- react-components-rails.gemspec
|
61
49
|
- src/index.ts
|
62
50
|
- src/ujs.ts
|
63
51
|
- tsconfig.json
|
64
|
-
- yarn.lock
|
65
52
|
homepage: https://github.com/renchap/webpacker-react
|
66
53
|
licenses:
|
67
54
|
- MIT
|