@cleanweb/oore 2.0.0-alpha.20 → 2.0.0-alpha.22
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/build/_cjs/classy/class/index.js +4 -2
- package/build/base/index.js +3 -19
- package/build/base/merged-state.js +4 -8
- package/build/base/methods.js +5 -9
- package/build/base/state/class-types.js +1 -2
- package/build/base/state/class.js +5 -9
- package/build/base/state/hook-types.js +1 -2
- package/build/base/state/hooks.js +7 -11
- package/build/base/state/index.js +3 -31
- package/build/classy/class/index.js +20 -22
- package/build/classy/class/types/extractor.js +1 -2
- package/build/classy/class/utils/function-name.js +1 -5
- package/build/classy/index.js +3 -19
- package/build/classy/instance/index.js +11 -16
- package/build/classy/instance/mount-callbacks.js +5 -9
- package/build/classy/instance/types/hook.js +1 -2
- package/build/classy/logic/index.js +7 -12
- package/build/classy/logic/types/hook.js +1 -2
- package/build/docs-src/api/base-classes.js +3 -9
- package/build/docs-src/api/index.js +8 -39
- package/build/docs-src/api/references.js +5 -31
- package/build/helpers/errors.js +1 -5
- package/build/helpers/index.js +5 -23
- package/build/helpers/mount-state.js +4 -8
- package/build/helpers/rerender.js +7 -11
- package/build/helpers/type-guards.js +1 -5
- package/build/helpers/use-component/index.js +3 -7
- package/build/helpers/use-component/types.js +1 -2
- package/build/index.js +3 -19
- package/build/slots/hook.js +15 -45
- package/build/slots/index.js +1 -17
- package/build/slots/types.js +1 -2
- package/build/tsconfig.json +2 -2
- package/package.json +1 -1
|
@@ -123,9 +123,11 @@ _a = ClassComponent;
|
|
|
123
123
|
*/
|
|
124
124
|
ClassComponent.extract = function FC(_Component, properties) {
|
|
125
125
|
const Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
|
126
|
-
const isClassComponentType = Component
|
|
126
|
+
const isClassComponentType = (Component === _a
|
|
127
|
+
|| Component.prototype instanceof _a
|
|
128
|
+
|| _a.isPrototypeOf(Component));
|
|
127
129
|
if (!isClassComponentType)
|
|
128
|
-
throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either
|
|
130
|
+
throw new Error('Attempted to initialize `ClassComponent` with an invalid Class type. Either call `extract()` with a class argument that extends ClassComponent (e.g `export default extract(MyComponent);`), or ensure `extract()` is called as a method on a ClassComponent constructor type (e.g `export default MyComponent.extract();`).');
|
|
129
131
|
/*************************************
|
|
130
132
|
* Begin Function Component *
|
|
131
133
|
**************************************/
|
package/build/base/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./state"), exports);
|
|
18
|
-
__exportStar(require("./methods"), exports);
|
|
19
|
-
__exportStar(require("./merged-state"), exports);
|
|
1
|
+
export * from './state';
|
|
2
|
+
export * from './methods';
|
|
3
|
+
export * from './merged-state';
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useMergedState = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
1
|
+
import { useMemo, useRef, useState } from 'react';
|
|
5
2
|
class MergedState {
|
|
6
3
|
static useRefresh() {
|
|
7
|
-
[this._values_, this.setState] =
|
|
4
|
+
[this._values_, this.setState] = useState(this.initialState);
|
|
8
5
|
}
|
|
9
6
|
get put() {
|
|
10
7
|
return Object.assign({}, this._setters_);
|
|
@@ -50,11 +47,10 @@ class MergedState {
|
|
|
50
47
|
* Similar to {@link useCleanState},
|
|
51
48
|
* but uses a single `useState` call for the entire state object.
|
|
52
49
|
*/
|
|
53
|
-
const useMergedState = (initialState) => {
|
|
54
|
-
const cleanState =
|
|
50
|
+
export const useMergedState = (initialState) => {
|
|
51
|
+
const cleanState = useRef(useMemo(() => {
|
|
55
52
|
return new MergedState(initialState);
|
|
56
53
|
}, [])).current;
|
|
57
54
|
MergedState.useRefresh.call(cleanState);
|
|
58
55
|
return cleanState;
|
|
59
56
|
};
|
|
60
|
-
exports.useMergedState = useMergedState;
|
package/build/base/methods.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @module ComponentMethods
|
|
4
3
|
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.useMethods = exports.ComponentMethods = void 0;
|
|
7
4
|
// Values
|
|
8
|
-
|
|
5
|
+
import { useMemo, useRef } from 'react';
|
|
9
6
|
/**
|
|
10
7
|
* @summary
|
|
11
8
|
* Base class for a class that holds methods for a function component.
|
|
@@ -16,7 +13,7 @@ const react_1 = require("react");
|
|
|
16
13
|
*
|
|
17
14
|
* Call the {@link useMethods} hook inside your function component to instantiate the class.
|
|
18
15
|
*/
|
|
19
|
-
class ComponentMethods {
|
|
16
|
+
export class ComponentMethods {
|
|
20
17
|
constructor() {
|
|
21
18
|
/**
|
|
22
19
|
* Persist class members during HMR. {@include ../classy/logic/hrm-preserve-keys.md}
|
|
@@ -26,7 +23,6 @@ class ComponentMethods {
|
|
|
26
23
|
this._hmrPreserveKeys = []; // @todo Keep undefined. Update to empty array after instantiation in dev env.
|
|
27
24
|
}
|
|
28
25
|
}
|
|
29
|
-
exports.ComponentMethods = ComponentMethods;
|
|
30
26
|
;
|
|
31
27
|
/**
|
|
32
28
|
* Returns an instance of the provided class,
|
|
@@ -44,8 +40,8 @@ const useMethods = (...args) => {
|
|
|
44
40
|
// In production, we only use the latestInstance the first time, and it's ignored every other time.
|
|
45
41
|
// This means changing the class at runtime will have no effect in production.
|
|
46
42
|
// latestInstance is only extracted into a separate variable for use in dev mode during HMR.
|
|
47
|
-
const latestInstance =
|
|
48
|
-
const instanceRef =
|
|
43
|
+
const latestInstance = useMemo(() => new Methods(), [Methods]);
|
|
44
|
+
const instanceRef = useRef(latestInstance);
|
|
49
45
|
const refreshState = () => {
|
|
50
46
|
// @ts-expect-error
|
|
51
47
|
instanceRef.current.props = props;
|
|
@@ -76,7 +72,7 @@ const useMethods = (...args) => {
|
|
|
76
72
|
refreshState();
|
|
77
73
|
return instanceRef.current;
|
|
78
74
|
};
|
|
79
|
-
|
|
75
|
+
export { useMethods };
|
|
80
76
|
/** /type_testing: {
|
|
81
77
|
let a = async () => {
|
|
82
78
|
const a: object = {b: ''};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CleanState = exports.CleanStateBase = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
1
|
+
import { useState } from 'react';
|
|
5
2
|
/** @internal */
|
|
6
|
-
class CleanStateBase {
|
|
3
|
+
export class CleanStateBase {
|
|
7
4
|
constructor(initialState) {
|
|
8
5
|
this._values_ = {};
|
|
9
6
|
this._setters_ = {};
|
|
@@ -100,9 +97,8 @@ class CleanStateBase {
|
|
|
100
97
|
return Object.assign({}, this._initialValues_);
|
|
101
98
|
}
|
|
102
99
|
}
|
|
103
|
-
exports.CleanStateBase = CleanStateBase;
|
|
104
100
|
CleanStateBase.update = function update() {
|
|
105
|
-
if (!(this instanceof
|
|
101
|
+
if (!(this instanceof CleanState))
|
|
106
102
|
throw new Error('CleanState.update must be called with `this` value set to a CleanState instance. Did you forget to use `.call` or `.apply`? Example: CleanState.update.call(cleanState);');
|
|
107
103
|
/**
|
|
108
104
|
* Linters complain about the use of a React hook within a loop because:
|
|
@@ -113,7 +109,7 @@ CleanStateBase.update = function update() {
|
|
|
113
109
|
* and it guarantees that the same useState calls will be made on every render in the exact same order.
|
|
114
110
|
* Therefore, it is safe to silence the linters, and required for this implementation to work smoothly.
|
|
115
111
|
*/
|
|
116
|
-
const retrieveState =
|
|
112
|
+
const retrieveState = useState;
|
|
117
113
|
this.valueKeys.forEach((key) => {
|
|
118
114
|
// @todo Make state updates accessible immediately. Use state.staged to access the scheduled updates.
|
|
119
115
|
let setter;
|
|
@@ -126,4 +122,4 @@ CleanStateBase.update = function update() {
|
|
|
126
122
|
};
|
|
127
123
|
;
|
|
128
124
|
/** @internal */
|
|
129
|
-
|
|
125
|
+
export const CleanState = (CleanStateBase);
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.useCleanState = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const class_1 = require("./class");
|
|
1
|
+
import { useMemo, useRef } from 'react';
|
|
2
|
+
import { CleanState } from './class';
|
|
6
3
|
/**
|
|
7
4
|
* Creates a state object, which includes the provided values,
|
|
8
5
|
* as well as helper methods for updating those values and automatically
|
|
@@ -13,18 +10,17 @@ const class_1 = require("./class");
|
|
|
13
10
|
*
|
|
14
11
|
* Discussion: [When to `useCleanState`](https://cleanjsweb.github.io/neat-react/documents/Clean_State.html).
|
|
15
12
|
*/
|
|
16
|
-
const useCleanState = (_initialState, ...props) => {
|
|
13
|
+
export const useCleanState = (_initialState, ...props) => {
|
|
17
14
|
const initialState = typeof _initialState === 'function'
|
|
18
|
-
?
|
|
15
|
+
? useMemo(() => _initialState(...props), [])
|
|
19
16
|
: _initialState;
|
|
20
17
|
;
|
|
21
|
-
const cleanState =
|
|
22
|
-
return new
|
|
18
|
+
const cleanState = useRef(useMemo(() => {
|
|
19
|
+
return new CleanState(initialState);
|
|
23
20
|
}, [])).current;
|
|
24
|
-
|
|
21
|
+
CleanState.update.call(cleanState);
|
|
25
22
|
return cleanState;
|
|
26
23
|
};
|
|
27
|
-
exports.useCleanState = useCleanState;
|
|
28
24
|
// Should be valid.
|
|
29
25
|
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6);
|
|
30
26
|
// useCleanState((a: boolean) => ({b: a.toString()}), true);
|
|
@@ -1,34 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @module CleanState
|
|
4
3
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
-
}
|
|
11
|
-
Object.defineProperty(o, k2, desc);
|
|
12
|
-
}) : (function(o, m, k, k2) {
|
|
13
|
-
if (k2 === undefined) k2 = k;
|
|
14
|
-
o[k2] = m[k];
|
|
15
|
-
}));
|
|
16
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
-
}) : function(o, v) {
|
|
19
|
-
o["default"] = v;
|
|
20
|
-
});
|
|
21
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
-
if (mod && mod.__esModule) return mod;
|
|
23
|
-
var result = {};
|
|
24
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
-
__setModuleDefault(result, mod);
|
|
26
|
-
return result;
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.MergedState = exports.useCleanState = exports.CleanState = void 0;
|
|
30
|
-
var class_1 = require("./class");
|
|
31
|
-
Object.defineProperty(exports, "CleanState", { enumerable: true, get: function () { return class_1.CleanState; } });
|
|
32
|
-
var hooks_1 = require("./hooks");
|
|
33
|
-
Object.defineProperty(exports, "useCleanState", { enumerable: true, get: function () { return hooks_1.useCleanState; } });
|
|
34
|
-
exports.MergedState = __importStar(require("../../base/merged-state"));
|
|
4
|
+
export { CleanState } from './class';
|
|
5
|
+
export { useCleanState } from './hooks';
|
|
6
|
+
export * as MergedState from '../../base/merged-state';
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var _a;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const function_name_1 = require("./utils/function-name");
|
|
8
|
-
const rerender_1 = require("../../helpers/rerender");
|
|
2
|
+
import { useMemo } from 'react';
|
|
3
|
+
import { ComponentInstance, useInstance } from '../instance';
|
|
4
|
+
import { setFunctionName } from './utils/function-name';
|
|
5
|
+
import { useRerender } from '../../helpers/rerender';
|
|
9
6
|
/**
|
|
10
7
|
* @summary
|
|
11
8
|
* A modern class component for React that is fully compatible with
|
|
@@ -23,7 +20,7 @@ const rerender_1 = require("../../helpers/rerender");
|
|
|
23
20
|
* making it easier to migrate older class components to the newer hooks-based system
|
|
24
21
|
* with little to no changes to their existing semantics/implementation.
|
|
25
22
|
*/
|
|
26
|
-
class ClassComponent extends
|
|
23
|
+
export class ClassComponent extends ComponentInstance {
|
|
27
24
|
constructor() {
|
|
28
25
|
super(...arguments);
|
|
29
26
|
/**
|
|
@@ -70,15 +67,15 @@ class ClassComponent extends instance_1.ComponentInstance {
|
|
|
70
67
|
* \`<MyComponent.FC />\`
|
|
71
68
|
*/
|
|
72
69
|
static _FC(props) {
|
|
73
|
-
const instance =
|
|
70
|
+
const instance = useInstance(this, props);
|
|
74
71
|
const { template, templateContext } = instance;
|
|
75
72
|
let _forceUpdate;
|
|
76
73
|
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
|
77
|
-
instance.forceUpdate = (_forceUpdate =
|
|
74
|
+
instance.forceUpdate = (_forceUpdate = useRerender() // Moved this to separate line to allow TS errors. Use proxy local variable to regain some type checking for the assignment to `instance.forceUpdate`.
|
|
78
75
|
);
|
|
79
76
|
// Add calling component name to template function name in stack traces.
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
useMemo(() => {
|
|
78
|
+
setFunctionName(template, `${this.name}.template`);
|
|
82
79
|
}, [template]);
|
|
83
80
|
return template(templateContext);
|
|
84
81
|
}
|
|
@@ -89,11 +86,9 @@ class ClassComponent extends instance_1.ComponentInstance {
|
|
|
89
86
|
return this._BoundFC = this._FC.bind(this);
|
|
90
87
|
}
|
|
91
88
|
}
|
|
92
|
-
exports.ClassComponent = ClassComponent;
|
|
93
|
-
exports.Component = ClassComponent;
|
|
94
89
|
_a = ClassComponent;
|
|
95
90
|
(() => {
|
|
96
|
-
|
|
91
|
+
setFunctionName(_a._FC, `$${_a.name}$`);
|
|
97
92
|
})();
|
|
98
93
|
/*************************************
|
|
99
94
|
* Function Component Extractor *
|
|
@@ -123,30 +118,32 @@ _a = ClassComponent;
|
|
|
123
118
|
*/
|
|
124
119
|
ClassComponent.extract = function FC(_Component, properties) {
|
|
125
120
|
const Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
|
126
|
-
const isClassComponentType = Component
|
|
121
|
+
const isClassComponentType = (Component === _a
|
|
122
|
+
|| Component.prototype instanceof _a
|
|
123
|
+
|| _a.isPrototypeOf(Component));
|
|
127
124
|
if (!isClassComponentType)
|
|
128
|
-
throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either
|
|
125
|
+
throw new Error('Attempted to initialize `ClassComponent` with an invalid Class type. Either call `extract()` with a class argument that extends ClassComponent (e.g `export default extract(MyComponent);`), or ensure `extract()` is called as a method on a ClassComponent constructor type (e.g `export default MyComponent.extract();`).');
|
|
129
126
|
/*************************************
|
|
130
127
|
* Begin Function Component *
|
|
131
128
|
**************************************/
|
|
132
129
|
/** A class-based, React function component created with `@cleanweb/oore`. {@link ClassComponent} */
|
|
133
130
|
const Wrapper = (props) => {
|
|
134
|
-
const instance =
|
|
131
|
+
const instance = useInstance(Component, props);
|
|
135
132
|
const { template, templateContext } = instance;
|
|
136
133
|
let _forceUpdate;
|
|
137
134
|
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
|
138
|
-
instance.forceUpdate = (_forceUpdate =
|
|
135
|
+
instance.forceUpdate = (_forceUpdate = useRerender() // Moved this to separate line to allow TS errors. Use proxy local variable to regain some type checking for the assignment to `instance.forceUpdate`.
|
|
139
136
|
);
|
|
140
137
|
// Add calling component name to template function name in stack traces.
|
|
141
|
-
|
|
142
|
-
|
|
138
|
+
useMemo(() => {
|
|
139
|
+
setFunctionName(template, `${Component.name}.template`);
|
|
143
140
|
}, [template]);
|
|
144
141
|
return template(templateContext);
|
|
145
142
|
};
|
|
146
143
|
/**************************************
|
|
147
144
|
* 👆🏼 End Function Component *
|
|
148
145
|
**************************************/
|
|
149
|
-
|
|
146
|
+
setFunctionName(Wrapper, `$${Component.name}$`);
|
|
150
147
|
return Object.assign(Wrapper, properties);
|
|
151
148
|
};
|
|
152
149
|
/**
|
|
@@ -155,6 +152,7 @@ ClassComponent.extract = function FC(_Component, properties) {
|
|
|
155
152
|
* \`<MyComponent.RC />\`
|
|
156
153
|
*/
|
|
157
154
|
ClassComponent.RC = _a.extract();
|
|
155
|
+
export { ClassComponent as Component };
|
|
158
156
|
/** /
|
|
159
157
|
testing: {
|
|
160
158
|
const a: object = {b: ''};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setFunctionName = void 0;
|
|
4
1
|
/** Provide more useful stack traces for otherwise non-specific function names. */
|
|
5
|
-
const setFunctionName = (func, newName) => {
|
|
2
|
+
export const setFunctionName = (func, newName) => {
|
|
6
3
|
try {
|
|
7
4
|
// Must use try block, as `name` is not configurable on older browsers, and may yield a TypeError.
|
|
8
5
|
Object.defineProperty(func, 'name', {
|
|
@@ -14,4 +11,3 @@ const setFunctionName = (func, newName) => {
|
|
|
14
11
|
console.warn(error);
|
|
15
12
|
}
|
|
16
13
|
};
|
|
17
|
-
exports.setFunctionName = setFunctionName;
|
package/build/classy/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./logic"), exports);
|
|
18
|
-
__exportStar(require("./instance"), exports);
|
|
19
|
-
__exportStar(require("./class"), exports);
|
|
1
|
+
export * from './logic';
|
|
2
|
+
export * from './instance';
|
|
3
|
+
export * from './class';
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const logic_1 = require("../../classy/logic");
|
|
6
|
-
const mount_callbacks_1 = require("./mount-callbacks");
|
|
7
|
-
const helpers_1 = require("../../helpers");
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { ComponentLogic, useLogic } from '../../classy/logic';
|
|
3
|
+
import { useMountCallbacks } from './mount-callbacks';
|
|
4
|
+
import { noOp } from '../../helpers';
|
|
8
5
|
/**
|
|
9
6
|
* A superset of {@link ComponentLogic} that adds support for lifecycle methods.
|
|
10
7
|
* This provides a declarative API for working with your React function component's lifecycle,
|
|
@@ -12,7 +9,7 @@ const helpers_1 = require("../../helpers");
|
|
|
12
9
|
*
|
|
13
10
|
* @see https://github.com/cleanjsweb/neat-react#lifecycle-useinstance
|
|
14
11
|
*/
|
|
15
|
-
class ComponentInstance extends
|
|
12
|
+
export class ComponentInstance extends ComponentLogic {
|
|
16
13
|
constructor() {
|
|
17
14
|
super(...arguments);
|
|
18
15
|
/**
|
|
@@ -37,7 +34,7 @@ class ComponentInstance extends logic_1.ComponentLogic {
|
|
|
37
34
|
*
|
|
38
35
|
* Uses `useEffect()` under the hood.
|
|
39
36
|
*/
|
|
40
|
-
this.onMount = () =>
|
|
37
|
+
this.onMount = () => noOp;
|
|
41
38
|
/**
|
|
42
39
|
* Runs _before_ every render cycle, including the first.
|
|
43
40
|
* Useful for logic that is involved in determining what to render.
|
|
@@ -64,7 +61,7 @@ class ComponentInstance extends logic_1.ComponentLogic {
|
|
|
64
61
|
*
|
|
65
62
|
* @returns A cleanup function.
|
|
66
63
|
*/
|
|
67
|
-
this.onRender = () =>
|
|
64
|
+
this.onRender = () => noOp;
|
|
68
65
|
/**
|
|
69
66
|
* Runs when the component is unmounted.
|
|
70
67
|
* It is called _after_ the cleanup function returned by onMount.
|
|
@@ -107,7 +104,6 @@ class ComponentInstance extends logic_1.ComponentLogic {
|
|
|
107
104
|
return this._templateContext;
|
|
108
105
|
}
|
|
109
106
|
}
|
|
110
|
-
exports.ComponentInstance = ComponentInstance;
|
|
111
107
|
;
|
|
112
108
|
/**
|
|
113
109
|
* Enables full separation of concerns between a React component's template
|
|
@@ -122,13 +118,13 @@ exports.ComponentInstance = ComponentInstance;
|
|
|
122
118
|
*
|
|
123
119
|
* The provided class should be a subclass of {@link ComponentInstance}.
|
|
124
120
|
*/
|
|
125
|
-
const useInstance = (...args) => {
|
|
121
|
+
export const useInstance = (...args) => {
|
|
126
122
|
var _a;
|
|
127
123
|
const [Component, props = {}] = args;
|
|
128
124
|
// useHooks.
|
|
129
|
-
const instance =
|
|
125
|
+
const instance = useLogic(Component, props);
|
|
130
126
|
// beforeMount, onMount, cleanUp.
|
|
131
|
-
|
|
127
|
+
useMountCallbacks(instance);
|
|
132
128
|
// beforeRender.
|
|
133
129
|
/**
|
|
134
130
|
* A proxy variable to allow typechecking of the assignment
|
|
@@ -138,7 +134,7 @@ const useInstance = (...args) => {
|
|
|
138
134
|
// @ts-expect-error Assigning to a readonly property.
|
|
139
135
|
instance._templateContext = (_templateContextProxy_ = (_a = instance.beforeRender) === null || _a === void 0 ? void 0 : _a.call(instance));
|
|
140
136
|
// onRender.
|
|
141
|
-
|
|
137
|
+
useEffect(() => {
|
|
142
138
|
var _a;
|
|
143
139
|
const cleanupAfterRerender = (_a = instance.onRender) === null || _a === void 0 ? void 0 : _a.call(instance);
|
|
144
140
|
return () => {
|
|
@@ -151,7 +147,6 @@ const useInstance = (...args) => {
|
|
|
151
147
|
// class FormValues<TValues> extends BrowserMemStore<TValues> {}
|
|
152
148
|
return instance;
|
|
153
149
|
};
|
|
154
|
-
exports.useInstance = useInstance;
|
|
155
150
|
/** /
|
|
156
151
|
testing: {
|
|
157
152
|
class A extends ComponentInstance<EmptyObject> {
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.useMountCallbacks = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const mount_state_1 = require("../../helpers/mount-state");
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useMountState } from '../../helpers/mount-state';
|
|
6
3
|
/** @internal */
|
|
7
|
-
const useMountCallbacks = (instance) => {
|
|
4
|
+
export const useMountCallbacks = (instance) => {
|
|
8
5
|
var _a;
|
|
9
|
-
const isMounted =
|
|
6
|
+
const isMounted = useMountState();
|
|
10
7
|
if (!isMounted())
|
|
11
8
|
(_a = instance.beforeMount) === null || _a === void 0 ? void 0 : _a.call(instance);
|
|
12
|
-
|
|
9
|
+
useEffect(() => {
|
|
13
10
|
var _a;
|
|
14
11
|
const mountHandlerCleanUp = (_a = instance.onMount) === null || _a === void 0 ? void 0 : _a.call(instance);
|
|
15
12
|
return () => {
|
|
@@ -27,4 +24,3 @@ const useMountCallbacks = (instance) => {
|
|
|
27
24
|
};
|
|
28
25
|
}, []);
|
|
29
26
|
};
|
|
30
|
-
exports.useMountCallbacks = useMountCallbacks;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.useLogic = exports.ComponentLogic = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const state_1 = require("../../base/state");
|
|
1
|
+
import { useMemo, useRef } from 'react';
|
|
2
|
+
import { useCleanState } from '../../base/state';
|
|
6
3
|
/**
|
|
7
4
|
* Base class for a class that holds methods to be used in a function component.
|
|
8
5
|
*
|
|
@@ -16,7 +13,7 @@ const state_1 = require("../../base/state");
|
|
|
16
13
|
*
|
|
17
14
|
* @typeParam TProps - {@include ./types/tprops.md}
|
|
18
15
|
*/
|
|
19
|
-
class ComponentLogic {
|
|
16
|
+
export class ComponentLogic {
|
|
20
17
|
constructor() {
|
|
21
18
|
/**
|
|
22
19
|
* Called before each instance of your component is mounted.
|
|
@@ -41,7 +38,6 @@ class ComponentLogic {
|
|
|
41
38
|
this._hmrPreserveKeys = [];
|
|
42
39
|
}
|
|
43
40
|
}
|
|
44
|
-
exports.ComponentLogic = ComponentLogic;
|
|
45
41
|
;
|
|
46
42
|
/**
|
|
47
43
|
* Returns an instance of the provided class, which holds methods for your component and
|
|
@@ -51,21 +47,21 @@ exports.ComponentLogic = ComponentLogic;
|
|
|
51
47
|
*
|
|
52
48
|
* @see https://cleanjsweb.github.io/neat-react/functions/API.useLogic.html
|
|
53
49
|
*/
|
|
54
|
-
const useLogic = (...args) => {
|
|
50
|
+
export const useLogic = (...args) => {
|
|
55
51
|
var _a;
|
|
56
52
|
const [Logic, props = {}] = args;
|
|
57
53
|
// In production, we only use the latestInstance the first time, and it's ignored every other time.
|
|
58
54
|
// This means changing the class at runtime will have no effect in production.
|
|
59
55
|
// latestInstance is only extracted into a separate variable for use in dev mode during HMR.
|
|
60
|
-
const latestInstance =
|
|
56
|
+
const latestInstance = useMemo(() => new Logic(), [Logic]);
|
|
61
57
|
// const latestInstance = useMemo(() => new Logic(), []);
|
|
62
|
-
const instanceRef =
|
|
58
|
+
const instanceRef = useRef(latestInstance);
|
|
63
59
|
const refreshState = () => {
|
|
64
60
|
var _a;
|
|
65
61
|
// @ts-expect-error
|
|
66
62
|
instanceRef.current.props = props;
|
|
67
63
|
// @ts-expect-error
|
|
68
|
-
instanceRef.current.state =
|
|
64
|
+
instanceRef.current.state = useCleanState(instanceRef.current.getInitialState, props);
|
|
69
65
|
// @ts-expect-error
|
|
70
66
|
instanceRef.current.hooks = (_a = instanceRef.current.useHooks()) !== null && _a !== void 0 ? _a : {};
|
|
71
67
|
};
|
|
@@ -90,7 +86,6 @@ const useLogic = (...args) => {
|
|
|
90
86
|
return instanceRef.current;
|
|
91
87
|
;
|
|
92
88
|
};
|
|
93
|
-
exports.useLogic = useLogic;
|
|
94
89
|
/** /
|
|
95
90
|
testing: {
|
|
96
91
|
const a: object = {b: ''};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var methods_1 = require("../../base/methods");
|
|
5
|
-
Object.defineProperty(exports, "ComponentMethods", { enumerable: true, get: function () { return methods_1.ComponentMethods; } });
|
|
6
|
-
var logic_1 = require("../../classy/logic");
|
|
7
|
-
Object.defineProperty(exports, "ComponentLogic", { enumerable: true, get: function () { return logic_1.ComponentLogic; } });
|
|
8
|
-
var instance_1 = require("../../classy/instance");
|
|
9
|
-
Object.defineProperty(exports, "ComponentInstance", { enumerable: true, get: function () { return instance_1.ComponentInstance; } });
|
|
1
|
+
export { ComponentMethods } from '../../base/methods';
|
|
2
|
+
export { ComponentLogic } from '../../classy/logic';
|
|
3
|
+
export { ComponentInstance } from '../../classy/instance';
|
|
@@ -1,44 +1,13 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* API Reference
|
|
4
3
|
* @module API
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
Object.defineProperty(o, k2, desc);
|
|
13
|
-
}) : (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
o[k2] = m[k];
|
|
16
|
-
}));
|
|
17
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
-
}) : function(o, v) {
|
|
20
|
-
o["default"] = v;
|
|
21
|
-
});
|
|
22
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
-
if (mod && mod.__esModule) return mod;
|
|
24
|
-
var result = {};
|
|
25
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
-
__setModuleDefault(result, mod);
|
|
27
|
-
return result;
|
|
28
|
-
};
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.References = exports.Helpers = exports.BaseClasses = exports.ClassComponent = exports.useInstance = exports.useLogic = exports.useMethods = exports.useCleanState = void 0;
|
|
31
|
-
var state_1 = require("../../base/state");
|
|
32
|
-
Object.defineProperty(exports, "useCleanState", { enumerable: true, get: function () { return state_1.useCleanState; } });
|
|
33
|
-
var methods_1 = require("../../base/methods");
|
|
34
|
-
Object.defineProperty(exports, "useMethods", { enumerable: true, get: function () { return methods_1.useMethods; } });
|
|
35
|
-
var logic_1 = require("../../classy/logic");
|
|
36
|
-
Object.defineProperty(exports, "useLogic", { enumerable: true, get: function () { return logic_1.useLogic; } });
|
|
37
|
-
var instance_1 = require("../../classy/instance");
|
|
38
|
-
Object.defineProperty(exports, "useInstance", { enumerable: true, get: function () { return instance_1.useInstance; } });
|
|
39
|
-
var class_1 = require("../../classy/class");
|
|
40
|
-
Object.defineProperty(exports, "ClassComponent", { enumerable: true, get: function () { return class_1.ClassComponent; } });
|
|
5
|
+
export { useCleanState } from '../../base/state';
|
|
6
|
+
export { useMethods } from '../../base/methods';
|
|
7
|
+
export { useLogic } from '../../classy/logic';
|
|
8
|
+
export { useInstance } from '../../classy/instance';
|
|
9
|
+
export { ClassComponent } from '../../classy/class';
|
|
41
10
|
/** @namespace */
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
11
|
+
export * as BaseClasses from './base-classes';
|
|
12
|
+
export * as Helpers from '../../helpers';
|
|
13
|
+
export * as References from './references';
|
|
@@ -1,31 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.ClassComponent = exports.ComponentInstance = exports.ComponentLogic = exports.$ComponentMethods = exports.$CleanState = void 0;
|
|
27
|
-
exports.$CleanState = __importStar(require("../../base/state"));
|
|
28
|
-
exports.$ComponentMethods = __importStar(require("../../base/methods"));
|
|
29
|
-
exports.ComponentLogic = __importStar(require("../../classy/logic"));
|
|
30
|
-
exports.ComponentInstance = __importStar(require("../../classy/instance"));
|
|
31
|
-
exports.ClassComponent = __importStar(require("../../classy/class"));
|
|
1
|
+
export * as $CleanState from '../../base/state';
|
|
2
|
+
export * as $ComponentMethods from '../../base/methods';
|
|
3
|
+
export * as ComponentLogic from '../../classy/logic';
|
|
4
|
+
export * as ComponentInstance from '../../classy/instance';
|
|
5
|
+
export * as ClassComponent from '../../classy/class';
|
package/build/helpers/errors.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.throwDevError = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Throw an error with the provided message
|
|
6
3
|
* only when `NODE_ENV` is *not* 'production'.
|
|
@@ -10,7 +7,7 @@ exports.throwDevError = void 0;
|
|
|
10
7
|
* Useful for enforcing certain conditions in development
|
|
11
8
|
* while failing more gracefully in production.
|
|
12
9
|
*/
|
|
13
|
-
const throwDevError = (message) => {
|
|
10
|
+
export const throwDevError = (message) => {
|
|
14
11
|
if (process.env.NODE_ENV === 'production') {
|
|
15
12
|
console.warn(message);
|
|
16
13
|
}
|
|
@@ -18,4 +15,3 @@ const throwDevError = (message) => {
|
|
|
18
15
|
throw new Error(message);
|
|
19
16
|
}
|
|
20
17
|
};
|
|
21
|
-
exports.throwDevError = throwDevError;
|
package/build/helpers/index.js
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* <!-- @ mergeModuleWith API -->
|
|
4
3
|
* @module Helpers
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
-
}
|
|
12
|
-
Object.defineProperty(o, k2, desc);
|
|
13
|
-
}) : (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
o[k2] = m[k];
|
|
16
|
-
}));
|
|
17
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
18
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.noOp = void 0;
|
|
22
|
-
__exportStar(require("./mount-state"), exports);
|
|
23
|
-
__exportStar(require("./rerender"), exports);
|
|
24
|
-
__exportStar(require("./use-component"), exports);
|
|
25
|
-
__exportStar(require("./type-guards"), exports);
|
|
5
|
+
export * from './mount-state';
|
|
6
|
+
export * from './rerender';
|
|
7
|
+
export * from './use-component';
|
|
8
|
+
export * from './type-guards';
|
|
26
9
|
/**
|
|
27
10
|
* An empty function.
|
|
28
11
|
* It returns (void) without performing any operations.
|
|
29
12
|
*/
|
|
30
|
-
const noOp = () => { };
|
|
31
|
-
exports.noOp = noOp;
|
|
13
|
+
export const noOp = () => { };
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useMountState = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
5
2
|
/**
|
|
6
3
|
* Returns a value that is false before the component has been mounted,
|
|
7
4
|
* then true during all subsequent rerenders.
|
|
8
5
|
*/
|
|
9
|
-
const useMountState = () => {
|
|
6
|
+
export const useMountState = () => {
|
|
10
7
|
/**
|
|
11
8
|
* This must not be a state value. It should not be the cause of a rerender.
|
|
12
9
|
* It merely provides information about the render count,
|
|
13
10
|
* without influencing that count itself.
|
|
14
11
|
* So `mounted` should never be created with `useState`.
|
|
15
12
|
*/
|
|
16
|
-
const mounted =
|
|
17
|
-
|
|
13
|
+
const mounted = useRef(false);
|
|
14
|
+
useEffect(() => {
|
|
18
15
|
mounted.current = true;
|
|
19
16
|
return () => {
|
|
20
17
|
mounted.current = false;
|
|
@@ -22,4 +19,3 @@ const useMountState = () => {
|
|
|
22
19
|
}, []);
|
|
23
20
|
return () => mounted.current;
|
|
24
21
|
};
|
|
25
|
-
exports.useMountState = useMountState;
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.useRerender = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const mount_state_1 = require("../helpers/mount-state");
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
import { useMountState } from '../helpers/mount-state';
|
|
6
3
|
;
|
|
7
4
|
/**
|
|
8
5
|
* Returns a function that can be called to manually trigger
|
|
9
6
|
* a rerender of your component.
|
|
10
7
|
*/
|
|
11
|
-
const useRerender = () => {
|
|
12
|
-
const isMounted =
|
|
13
|
-
const renderCount =
|
|
14
|
-
const [, forceRerender] =
|
|
8
|
+
export const useRerender = () => {
|
|
9
|
+
const isMounted = useMountState();
|
|
10
|
+
const renderCount = useRef(0);
|
|
11
|
+
const [, forceRerender] = useState(renderCount.current);
|
|
15
12
|
renderCount.current++;
|
|
16
|
-
const rerender =
|
|
13
|
+
const rerender = useCallback(() => {
|
|
17
14
|
let resolve;
|
|
18
15
|
const promise = new Promise((_r) => resolve = _r);
|
|
19
16
|
const execute = () => {
|
|
@@ -39,4 +36,3 @@ const useRerender = () => {
|
|
|
39
36
|
refresher.currentCount = renderCount.current;
|
|
40
37
|
return refresher;
|
|
41
38
|
};
|
|
42
|
-
exports.useRerender = useRerender;
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.canIndex = void 0;
|
|
4
|
-
const canIndex = (key, targetObject) => {
|
|
1
|
+
export const canIndex = (key, targetObject) => {
|
|
5
2
|
const test = typeof key === 'number' ? `${key}` : key;
|
|
6
3
|
return Reflect.ownKeys(targetObject).includes(test);
|
|
7
4
|
};
|
|
8
|
-
exports.canIndex = canIndex;
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Use = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
1
|
+
import { useEffect } from 'react';
|
|
5
2
|
/**
|
|
6
3
|
* A component you can use to consume React hooks
|
|
7
4
|
* in a {@link Component | React.Component} class.
|
|
8
5
|
*/
|
|
9
|
-
const Use = (params) => {
|
|
6
|
+
export const Use = (params) => {
|
|
10
7
|
const { hook: useGenericHook, argumentsList, onUpdate } = params;
|
|
11
8
|
const output = useGenericHook(...argumentsList);
|
|
12
|
-
|
|
9
|
+
useEffect(() => {
|
|
13
10
|
onUpdate(output);
|
|
14
11
|
}, [output]);
|
|
15
12
|
return null;
|
|
16
13
|
};
|
|
17
|
-
exports.Use = Use;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/build/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./classy"), exports);
|
|
18
|
-
__exportStar(require("./base"), exports);
|
|
19
|
-
__exportStar(require("./helpers"), exports);
|
|
1
|
+
export * from './classy';
|
|
2
|
+
export * from './base';
|
|
3
|
+
export * from './helpers';
|
package/build/slots/hook.js
CHANGED
|
@@ -1,39 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.useSlots = exports.isPortalChild = exports.getComponentSlotName = exports.isElementChild = void 0;
|
|
27
|
-
const errors_1 = require("../helpers/errors");
|
|
28
|
-
const react_1 = __importStar(require("react"));
|
|
29
|
-
const isElementChild = (child) => {
|
|
1
|
+
import { throwDevError } from '../helpers/errors';
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
export const isElementChild = (child) => {
|
|
30
4
|
if (child && typeof child === 'object' && 'type' in child) {
|
|
31
5
|
return true;
|
|
32
6
|
}
|
|
33
7
|
return false;
|
|
34
8
|
};
|
|
35
|
-
|
|
36
|
-
const getComponentSlotName = (TargetComponent, child) => {
|
|
9
|
+
export const getComponentSlotName = (TargetComponent, child) => {
|
|
37
10
|
if (child) {
|
|
38
11
|
const keyTypes = ['string', 'number', 'symbol'];
|
|
39
12
|
const slotName = child.props['data-slot-name'];
|
|
@@ -52,13 +25,11 @@ const getComponentSlotName = (TargetComponent, child) => {
|
|
|
52
25
|
}
|
|
53
26
|
return undefined;
|
|
54
27
|
};
|
|
55
|
-
|
|
56
|
-
const isPortalChild = (child) => {
|
|
28
|
+
export const isPortalChild = (child) => {
|
|
57
29
|
return (!!child
|
|
58
30
|
&& typeof child === 'object'
|
|
59
31
|
&& 'children' in child);
|
|
60
32
|
};
|
|
61
|
-
exports.isPortalChild = isPortalChild;
|
|
62
33
|
/**
|
|
63
34
|
* Groups `children` prop into predefined slots.
|
|
64
35
|
*
|
|
@@ -69,14 +40,14 @@ exports.isPortalChild = isPortalChild;
|
|
|
69
40
|
*
|
|
70
41
|
* @see {@link SlotComponent} for more on how to use the returned slot nodes.
|
|
71
42
|
*/
|
|
72
|
-
const useSlots = (children, Caller) => {
|
|
73
|
-
const slotsAliasLookup =
|
|
43
|
+
export const useSlots = (children, Caller) => {
|
|
44
|
+
const slotsAliasLookup = useMemo(() => {
|
|
74
45
|
const entries = Object.entries(Caller.Slots);
|
|
75
46
|
const aliasLookup = {};
|
|
76
47
|
entries.forEach(([alias, RegisteredSlotComponent]) => {
|
|
77
|
-
const slotName =
|
|
48
|
+
const slotName = getComponentSlotName(RegisteredSlotComponent);
|
|
78
49
|
if (!slotName) {
|
|
79
|
-
|
|
50
|
+
throwDevError(`A registered slot component did not have a slot name. All components registered as slots must either be a string tag-name or a React component with either "slotName" or "displayName". The affected component was: ${RegisteredSlotComponent}`);
|
|
80
51
|
return;
|
|
81
52
|
}
|
|
82
53
|
aliasLookup[slotName] = alias;
|
|
@@ -84,7 +55,7 @@ const useSlots = (children, Caller) => {
|
|
|
84
55
|
return aliasLookup;
|
|
85
56
|
}, [Caller.Slots]);
|
|
86
57
|
// @todo Expose original source order of `children` with respect to slot aliases.
|
|
87
|
-
const result =
|
|
58
|
+
const result = useMemo(() => {
|
|
88
59
|
var _a;
|
|
89
60
|
const slotNodes = {};
|
|
90
61
|
const unmatchedChildren = [];
|
|
@@ -92,27 +63,27 @@ const useSlots = (children, Caller) => {
|
|
|
92
63
|
const requiredSlotAliases = [
|
|
93
64
|
...((_a = Caller.requiredSlotAliases) !== null && _a !== void 0 ? _a : [])
|
|
94
65
|
];
|
|
95
|
-
|
|
66
|
+
React.Children.forEach(children, (_child) => {
|
|
96
67
|
var _a;
|
|
97
68
|
const child = _child;
|
|
98
69
|
if (!child) {
|
|
99
70
|
invalidChildren.push(child);
|
|
100
71
|
return;
|
|
101
72
|
}
|
|
102
|
-
if (!
|
|
73
|
+
if (!React.isValidElement(child)) {
|
|
103
74
|
console.warn(`Invalid node found in JSX children while parsing slots. Got: "${child}".`);
|
|
104
75
|
invalidChildren.push(child);
|
|
105
76
|
return;
|
|
106
77
|
}
|
|
107
78
|
;
|
|
108
79
|
// @todo Check for fragment
|
|
109
|
-
if (!
|
|
80
|
+
if (!isElementChild(child)) {
|
|
110
81
|
unmatchedChildren.push(child);
|
|
111
82
|
return;
|
|
112
83
|
}
|
|
113
84
|
const slotAlias = (() => {
|
|
114
85
|
var _a;
|
|
115
|
-
const slotName =
|
|
86
|
+
const slotName = getComponentSlotName(child.type, child);
|
|
116
87
|
return slotName ? (_a = slotsAliasLookup[slotName]) !== null && _a !== void 0 ? _a : null : null;
|
|
117
88
|
})();
|
|
118
89
|
if (slotAlias) {
|
|
@@ -133,11 +104,10 @@ const useSlots = (children, Caller) => {
|
|
|
133
104
|
});
|
|
134
105
|
requiredSlotAliases.forEach((slotAlias) => {
|
|
135
106
|
if (!slotNodes[slotAlias]) {
|
|
136
|
-
|
|
107
|
+
throwDevError(`Missing required slot "${String(slotAlias)}".`);
|
|
137
108
|
}
|
|
138
109
|
});
|
|
139
110
|
return [slotNodes, unmatchedChildren, invalidChildren];
|
|
140
111
|
}, [children]);
|
|
141
112
|
return result;
|
|
142
113
|
};
|
|
143
|
-
exports.useSlots = useSlots;
|
package/build/slots/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./hook"), exports);
|
|
1
|
+
export * from './hook';
|
package/build/slots/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/build/tsconfig.json
CHANGED
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"forceConsistentCasingInFileNames": true,
|
|
24
24
|
"incremental": false,
|
|
25
25
|
"esModuleInterop": true,
|
|
26
|
-
"module": "
|
|
27
|
-
"moduleResolution": "
|
|
26
|
+
"module": "esnext",
|
|
27
|
+
"moduleResolution": "bundler",
|
|
28
28
|
"resolveJsonModule": true,
|
|
29
29
|
"isolatedModules": true,
|
|
30
30
|
"jsx": "react-jsx",
|
package/package.json
CHANGED