@cleanweb/oore 2.0.0-alpha.16 → 2.0.0-alpha.18
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/base/index.d.ts +3 -0
- package/build/_cjs/base/index.js +19 -0
- package/build/_cjs/base/merged-state.d.ts +20 -0
- package/build/_cjs/base/merged-state.js +61 -0
- package/build/_cjs/base/methods.d.ts +58 -0
- package/build/_cjs/base/methods.js +95 -0
- package/build/_cjs/base/state/class-types.d.ts +20 -0
- package/build/_cjs/base/state/class-types.js +2 -0
- package/build/_cjs/base/state/class.d.ts +69 -0
- package/build/_cjs/base/state/class.js +129 -0
- package/build/_cjs/base/state/hook-types.d.ts +32 -0
- package/build/_cjs/base/state/hook-types.js +2 -0
- package/build/_cjs/base/state/hooks.d.ts +12 -0
- package/build/_cjs/base/state/hooks.js +41 -0
- package/build/_cjs/base/state/index.d.ts +9 -0
- package/build/_cjs/base/state/index.js +35 -0
- package/build/_cjs/classy/class/index.d.ts +128 -0
- package/build/_cjs/classy/class/index.js +174 -0
- package/build/_cjs/classy/class/types/extractor.d.ts +5 -0
- package/build/_cjs/classy/class/types/extractor.js +2 -0
- package/build/_cjs/classy/class/utils/function-name.d.ts +2 -0
- package/build/_cjs/classy/class/utils/function-name.js +17 -0
- package/build/_cjs/classy/index.d.ts +3 -0
- package/build/_cjs/classy/index.js +19 -0
- package/build/_cjs/classy/instance/index.d.ts +144 -0
- package/build/_cjs/classy/instance/index.js +177 -0
- package/build/_cjs/classy/instance/mount-callbacks.d.ts +5 -0
- package/build/_cjs/classy/instance/mount-callbacks.js +30 -0
- package/build/_cjs/classy/instance/types/hook.d.ts +13 -0
- package/build/_cjs/classy/instance/types/hook.js +2 -0
- package/build/_cjs/classy/logic/index.d.ts +116 -0
- package/build/_cjs/classy/logic/index.js +123 -0
- package/build/_cjs/classy/logic/types/hook.d.ts +16 -0
- package/build/_cjs/classy/logic/types/hook.js +2 -0
- package/build/_cjs/docs-src/api/base-classes.d.ts +3 -0
- package/build/_cjs/docs-src/api/base-classes.js +9 -0
- package/build/_cjs/docs-src/api/index.d.ts +13 -0
- package/build/_cjs/docs-src/api/index.js +44 -0
- package/build/_cjs/docs-src/api/references.d.ts +5 -0
- package/build/_cjs/docs-src/api/references.js +31 -0
- package/build/_cjs/helpers/errors.d.ts +10 -0
- package/build/_cjs/helpers/errors.js +21 -0
- package/build/_cjs/helpers/index.d.ts +13 -0
- package/build/_cjs/helpers/index.js +31 -0
- package/build/_cjs/helpers/mount-state.d.ts +5 -0
- package/build/_cjs/helpers/mount-state.js +25 -0
- package/build/_cjs/helpers/rerender.d.ts +24 -0
- package/build/_cjs/helpers/rerender.js +42 -0
- package/build/_cjs/helpers/type-guards.d.ts +1 -0
- package/build/_cjs/helpers/type-guards.js +8 -0
- package/build/_cjs/helpers/use-component/index.d.ts +6 -0
- package/build/_cjs/helpers/use-component/index.js +17 -0
- package/build/_cjs/helpers/use-component/types.d.ts +22 -0
- package/build/_cjs/helpers/use-component/types.js +2 -0
- package/build/_cjs/index.d.ts +3 -0
- package/build/_cjs/index.js +19 -0
- package/build/_cjs/slots/hook.d.ts +20 -0
- package/build/_cjs/slots/hook.js +143 -0
- package/build/_cjs/slots/index.d.ts +1 -0
- package/build/_cjs/slots/index.js +17 -0
- package/build/_cjs/slots/types.d.ts +131 -0
- package/build/_cjs/slots/types.js +2 -0
- package/build/base/index.js +19 -3
- package/build/base/merged-state.js +35 -54
- package/build/base/methods.js +23 -25
- package/build/base/state/class-types.js +2 -1
- package/build/base/state/class.js +49 -69
- package/build/base/state/hook-types.js +2 -1
- package/build/base/state/hooks.js +12 -12
- package/build/base/state/index.js +32 -4
- package/build/classy/class/index.js +89 -109
- package/build/classy/class/types/extractor.js +2 -1
- package/build/classy/class/utils/function-name.js +5 -1
- package/build/classy/index.js +19 -3
- package/build/classy/instance/index.js +62 -84
- package/build/classy/instance/mount-callbacks.js +12 -8
- package/build/classy/instance/types/hook.js +2 -1
- package/build/classy/logic/index.js +27 -28
- package/build/classy/logic/types/hook.js +2 -1
- package/build/docs-src/api/base-classes.js +9 -3
- package/build/docs-src/api/index.js +39 -8
- package/build/docs-src/api/references.js +31 -5
- package/build/globals.d.ts +130 -83
- package/build/helpers/errors.js +5 -1
- package/build/helpers/index.js +23 -5
- package/build/helpers/mount-state.js +10 -6
- package/build/helpers/rerender.js +16 -12
- package/build/helpers/type-guards.js +6 -2
- package/build/helpers/use-component/index.js +9 -5
- package/build/helpers/use-component/types.js +2 -1
- package/build/index.d.ts +0 -1
- package/build/index.js +19 -3
- package/build/slots/hook.js +58 -36
- package/build/slots/index.js +17 -1
- package/build/slots/types.js +2 -1
- package/build/tsconfig.json +5 -6
- package/package.json +31 -12
- package/build/globals.js +0 -3
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
import { useState } from 'react';
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CleanState = exports.CleanStateBase = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
13
5
|
/** @internal */
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var _this = this;
|
|
6
|
+
class CleanStateBase {
|
|
7
|
+
constructor(initialState) {
|
|
17
8
|
this._values_ = {};
|
|
18
9
|
this._setters_ = {};
|
|
19
10
|
/**
|
|
@@ -69,10 +60,10 @@ var CleanStateBase = /** @class */ (function () {
|
|
|
69
60
|
* </>;
|
|
70
61
|
* };
|
|
71
62
|
*/
|
|
72
|
-
this.putMany =
|
|
73
|
-
Object.entries(newValues).forEach(
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
this.putMany = (newValues) => {
|
|
64
|
+
Object.entries(newValues).forEach((entry) => {
|
|
65
|
+
const [key, value] = entry;
|
|
66
|
+
this.put[key](value);
|
|
76
67
|
});
|
|
77
68
|
};
|
|
78
69
|
this.reservedKeys = Object.keys(this);
|
|
@@ -86,64 +77,53 @@ var CleanStateBase = /** @class */ (function () {
|
|
|
86
77
|
* and they will always be processed in a consistent order during rerenders.
|
|
87
78
|
*/
|
|
88
79
|
this.valueKeys = Object.keys(initialState);
|
|
89
|
-
this._initialValues_ =
|
|
90
|
-
this.valueKeys.forEach(
|
|
91
|
-
if (
|
|
92
|
-
throw new Error(
|
|
93
|
-
|
|
94
|
-
Object.defineProperty(
|
|
95
|
-
get
|
|
80
|
+
this._initialValues_ = Object.assign({}, initialState);
|
|
81
|
+
this.valueKeys.forEach((key) => {
|
|
82
|
+
if (this.reservedKeys.includes(key))
|
|
83
|
+
throw new Error(`The name "${key}" is reserved by CleanState and cannot be used to index state variables. Please use a different key.`);
|
|
84
|
+
const self = this;
|
|
85
|
+
Object.defineProperty(this, key, {
|
|
86
|
+
get() {
|
|
96
87
|
return self._values_[key];
|
|
97
88
|
},
|
|
98
|
-
set
|
|
89
|
+
set(value) {
|
|
99
90
|
self._setters_[key](value);
|
|
100
91
|
},
|
|
101
92
|
enumerable: true,
|
|
102
93
|
});
|
|
103
94
|
});
|
|
104
95
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.valueKeys.forEach(function (key) {
|
|
134
|
-
var _a;
|
|
135
|
-
// @todo Make state updates accessible immediately. Use state.staged to access the scheduled updates.
|
|
136
|
-
var setter;
|
|
137
|
-
_a = retrieveState(_this.initialState[key]), _this._values_[key] = _a[0], setter = _a[1];
|
|
138
|
-
_this._setters_[key] = (function (valueOrCallback) {
|
|
139
|
-
// this._staged_[key] = value;
|
|
140
|
-
setter(valueOrCallback);
|
|
141
|
-
});
|
|
96
|
+
get put() {
|
|
97
|
+
return Object.assign({}, this._setters_);
|
|
98
|
+
}
|
|
99
|
+
get initialState() {
|
|
100
|
+
return Object.assign({}, this._initialValues_);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.CleanStateBase = CleanStateBase;
|
|
104
|
+
CleanStateBase.update = function update() {
|
|
105
|
+
if (!(this instanceof exports.CleanState))
|
|
106
|
+
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
|
+
/**
|
|
108
|
+
* Linters complain about the use of a React hook within a loop because:
|
|
109
|
+
* > By following this rule, you ensure that Hooks are called in the same order each time a component renders.
|
|
110
|
+
* > That’s what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls.
|
|
111
|
+
* To resolve this, we're calling `useState` via an alias `retrieveState`.
|
|
112
|
+
* Bypassing this rule is safe here because `useCleanState` is a special case,
|
|
113
|
+
* and it guarantees that the same useState calls will be made on every render in the exact same order.
|
|
114
|
+
* Therefore, it is safe to silence the linters, and required for this implementation to work smoothly.
|
|
115
|
+
*/
|
|
116
|
+
const retrieveState = react_1.useState;
|
|
117
|
+
this.valueKeys.forEach((key) => {
|
|
118
|
+
// @todo Make state updates accessible immediately. Use state.staged to access the scheduled updates.
|
|
119
|
+
let setter;
|
|
120
|
+
[this._values_[key], setter] = retrieveState(this.initialState[key]);
|
|
121
|
+
this._setters_[key] = ((valueOrCallback) => {
|
|
122
|
+
// this._staged_[key] = value;
|
|
123
|
+
setter(valueOrCallback);
|
|
142
124
|
});
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
}());
|
|
146
|
-
export { CleanStateBase };
|
|
125
|
+
});
|
|
126
|
+
};
|
|
147
127
|
;
|
|
148
128
|
/** @internal */
|
|
149
|
-
|
|
129
|
+
exports.CleanState = (CleanStateBase);
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCleanState = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const class_1 = require("./class");
|
|
3
6
|
/**
|
|
4
7
|
* Creates a state object, which includes the provided values,
|
|
5
8
|
* as well as helper methods for updating those values and automatically
|
|
@@ -10,21 +13,18 @@ import { CleanState } from './class';
|
|
|
10
13
|
*
|
|
11
14
|
* Discussion: [When to `useCleanState`](https://cleanjsweb.github.io/neat-react/documents/Clean_State.html).
|
|
12
15
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
props[_i - 1] = arguments[_i];
|
|
17
|
-
}
|
|
18
|
-
var initialState = typeof _initialState === 'function'
|
|
19
|
-
? useMemo(function () { return _initialState.apply(void 0, props); }, [])
|
|
16
|
+
const useCleanState = (_initialState, ...props) => {
|
|
17
|
+
const initialState = typeof _initialState === 'function'
|
|
18
|
+
? (0, react_1.useMemo)(() => _initialState(...props), [])
|
|
20
19
|
: _initialState;
|
|
21
20
|
;
|
|
22
|
-
|
|
23
|
-
return new CleanState(initialState);
|
|
21
|
+
const cleanState = (0, react_1.useRef)((0, react_1.useMemo)(() => {
|
|
22
|
+
return new class_1.CleanState(initialState);
|
|
24
23
|
}, [])).current;
|
|
25
|
-
CleanState.update.call(cleanState);
|
|
24
|
+
class_1.CleanState.update.call(cleanState);
|
|
26
25
|
return cleanState;
|
|
27
26
|
};
|
|
27
|
+
exports.useCleanState = useCleanState;
|
|
28
28
|
// Should be valid.
|
|
29
29
|
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6);
|
|
30
30
|
// useCleanState((a: boolean) => ({b: a.toString()}), true);
|
|
@@ -1,7 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @module CleanState
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
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
|
+
require("../../globals");
|
|
31
|
+
var class_1 = require("./class");
|
|
32
|
+
Object.defineProperty(exports, "CleanState", { enumerable: true, get: function () { return class_1.CleanState; } });
|
|
33
|
+
var hooks_1 = require("./hooks");
|
|
34
|
+
Object.defineProperty(exports, "useCleanState", { enumerable: true, get: function () { return hooks_1.useCleanState; } });
|
|
35
|
+
exports.MergedState = __importStar(require("../../base/merged-state"));
|
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (typeof b !== "function" && b !== null)
|
|
10
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
-
extendStatics(d, b);
|
|
12
|
-
function __() { this.constructor = d; }
|
|
13
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
-
};
|
|
15
|
-
})();
|
|
16
|
-
import { useMemo } from 'react';
|
|
17
|
-
import { ComponentInstance, useInstance } from '../instance';
|
|
18
|
-
import { setFunctionName } from './utils/function-name';
|
|
19
|
-
import { useRerender } from '../../helpers/rerender';
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Component = exports.ClassComponent = void 0;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const instance_1 = require("../instance");
|
|
7
|
+
const function_name_1 = require("./utils/function-name");
|
|
8
|
+
const rerender_1 = require("../../helpers/rerender");
|
|
20
9
|
/**
|
|
21
10
|
* @summary
|
|
22
11
|
* A modern class component for React that is fully compatible with
|
|
@@ -34,10 +23,9 @@ import { useRerender } from '../../helpers/rerender';
|
|
|
34
23
|
* making it easier to migrate older class components to the newer hooks-based system
|
|
35
24
|
* with little to no changes to their existing semantics/implementation.
|
|
36
25
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
26
|
+
class ClassComponent extends instance_1.ComponentInstance {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
41
29
|
/**
|
|
42
30
|
* Analogous to {@link React.Component.render}. A function that returns
|
|
43
31
|
* your component's JSX template.
|
|
@@ -74,107 +62,99 @@ var ClassComponent = /** @class */ (function (_super) {
|
|
|
74
62
|
* }
|
|
75
63
|
* ```
|
|
76
64
|
*/
|
|
77
|
-
|
|
78
|
-
return _this;
|
|
65
|
+
this.template = () => null;
|
|
79
66
|
}
|
|
80
67
|
/**
|
|
81
68
|
* A standard React function component that works like any
|
|
82
69
|
* other function component and can be rendered as JSX.
|
|
83
70
|
* \`<MyComponent.FC />\`
|
|
84
71
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
var _forceUpdate;
|
|
72
|
+
static _FC(props) {
|
|
73
|
+
const instance = (0, instance_1.useInstance)(this, props);
|
|
74
|
+
const { template, templateContext } = instance;
|
|
75
|
+
let _forceUpdate;
|
|
90
76
|
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
|
91
|
-
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`.
|
|
77
|
+
instance.forceUpdate = (_forceUpdate = (0, rerender_1.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`.
|
|
92
78
|
);
|
|
93
79
|
// Add calling component name to template function name in stack traces.
|
|
94
|
-
|
|
95
|
-
|
|
80
|
+
(0, react_1.useMemo)(() => {
|
|
81
|
+
(0, function_name_1.setFunctionName)(template, `${this.name}.template`);
|
|
96
82
|
}, [template]);
|
|
97
83
|
return template(templateContext);
|
|
98
|
-
}
|
|
84
|
+
}
|
|
99
85
|
;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
_a
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
86
|
+
static get FC() {
|
|
87
|
+
if (this._BoundFC)
|
|
88
|
+
return this._BoundFC;
|
|
89
|
+
return this._BoundFC = this._FC.bind(this);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.ClassComponent = ClassComponent;
|
|
93
|
+
exports.Component = ClassComponent;
|
|
94
|
+
_a = ClassComponent;
|
|
95
|
+
(() => {
|
|
96
|
+
(0, function_name_1.setFunctionName)(_a._FC, `$${_a.name}$`);
|
|
97
|
+
})();
|
|
98
|
+
/*************************************
|
|
99
|
+
* Function Component Extractor *
|
|
100
|
+
**************************************/
|
|
101
|
+
/**
|
|
102
|
+
* Extract a Function Component (FC) which can be used to render
|
|
103
|
+
* your ClassComponent just like any other React component.
|
|
104
|
+
*
|
|
105
|
+
* Each JSX reference to the returned component will render with
|
|
106
|
+
* a separate instance of your class.
|
|
107
|
+
*
|
|
108
|
+
* So you only need to call `YourClassComponent.extract()` (or `*.FC()`) once,
|
|
109
|
+
* then use the returned function component as many times as you need.
|
|
110
|
+
*
|
|
111
|
+
* It is recommended to store this returned value as a static member of
|
|
112
|
+
* your ClassComponent. While this value may be given any name, the name
|
|
113
|
+
* RC (for "React Component") is the recommended convention.
|
|
114
|
+
*
|
|
115
|
+
* @example <caption>Calling `extract` in your ClassComponent</caption>
|
|
116
|
+
* class Button extends ClassComponent {
|
|
117
|
+
* static readonly RC = this.extract(); // or this.FC();
|
|
118
|
+
* // Because of the static keyword, `this` here refers to the class itself, same as calling `Button.extract()`.
|
|
119
|
+
* }
|
|
120
|
+
*
|
|
121
|
+
* // Render with `<Button.RC />`, or export RC to use the component in other files.
|
|
122
|
+
* export default Button.RC;
|
|
123
|
+
*/
|
|
124
|
+
ClassComponent.extract = function FC(_Component, properties) {
|
|
125
|
+
const Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
|
126
|
+
const isClassComponentType = Component.prototype instanceof _a;
|
|
127
|
+
if (!isClassComponentType)
|
|
128
|
+
throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either pass, as an argument to FC(), a class that extends ClassComponent (e.g `export FC(MyComponent);`), or ensure FC() is called as a method on a ClassComponent constructor type (e.g `export MyComponent.FC()`).');
|
|
114
129
|
/*************************************
|
|
115
|
-
*
|
|
130
|
+
* Begin Function Component *
|
|
116
131
|
**************************************/
|
|
117
|
-
/**
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
*
|
|
131
|
-
* @example <caption>Calling `extract` in your ClassComponent</caption>
|
|
132
|
-
* class Button extends ClassComponent {
|
|
133
|
-
* static readonly RC = this.extract(); // or this.FC();
|
|
134
|
-
* // Because of the static keyword, `this` here refers to the class itself, same as calling `Button.extract()`.
|
|
135
|
-
* }
|
|
136
|
-
*
|
|
137
|
-
* // Render with `<Button.RC />`, or export RC to use the component in other files.
|
|
138
|
-
* export default Button.RC;
|
|
139
|
-
*/
|
|
140
|
-
ClassComponent.extract = function FC(_Component, properties) {
|
|
141
|
-
var Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
|
142
|
-
var isClassComponentType = Component.prototype instanceof _a;
|
|
143
|
-
if (!isClassComponentType)
|
|
144
|
-
throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either pass, as an argument to FC(), a class that extends ClassComponent (e.g `export FC(MyComponent);`), or ensure FC() is called as a method on a ClassComponent constructor type (e.g `export MyComponent.FC()`).');
|
|
145
|
-
/*************************************
|
|
146
|
-
* Begin Function Component *
|
|
147
|
-
**************************************/
|
|
148
|
-
/** A class-based, React function component created with `@cleanweb/oore`. {@link ClassComponent} */
|
|
149
|
-
var Wrapper = function (props) {
|
|
150
|
-
var instance = useInstance(Component, props);
|
|
151
|
-
var template = instance.template, templateContext = instance.templateContext;
|
|
152
|
-
var _forceUpdate;
|
|
153
|
-
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
|
154
|
-
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`.
|
|
155
|
-
);
|
|
156
|
-
// Add calling component name to template function name in stack traces.
|
|
157
|
-
useMemo(function () {
|
|
158
|
-
setFunctionName(template, "".concat(Component.name, ".template"));
|
|
159
|
-
}, [template]);
|
|
160
|
-
return template(templateContext);
|
|
161
|
-
};
|
|
162
|
-
/**************************************
|
|
163
|
-
* 👆🏼 End Function Component *
|
|
164
|
-
**************************************/
|
|
165
|
-
setFunctionName(Wrapper, "$".concat(Component.name, "$"));
|
|
166
|
-
return Object.assign(Wrapper, properties);
|
|
132
|
+
/** A class-based, React function component created with `@cleanweb/oore`. {@link ClassComponent} */
|
|
133
|
+
const Wrapper = (props) => {
|
|
134
|
+
const instance = (0, instance_1.useInstance)(Component, props);
|
|
135
|
+
const { template, templateContext } = instance;
|
|
136
|
+
let _forceUpdate;
|
|
137
|
+
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
|
138
|
+
instance.forceUpdate = (_forceUpdate = (0, rerender_1.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
|
+
);
|
|
140
|
+
// Add calling component name to template function name in stack traces.
|
|
141
|
+
(0, react_1.useMemo)(() => {
|
|
142
|
+
(0, function_name_1.setFunctionName)(template, `${Component.name}.template`);
|
|
143
|
+
}, [template]);
|
|
144
|
+
return template(templateContext);
|
|
167
145
|
};
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
146
|
+
/**************************************
|
|
147
|
+
* 👆🏼 End Function Component *
|
|
148
|
+
**************************************/
|
|
149
|
+
(0, function_name_1.setFunctionName)(Wrapper, `$${Component.name}$`);
|
|
150
|
+
return Object.assign(Wrapper, properties);
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* A standard React function component that works like any
|
|
154
|
+
* other function component and can be rendered as JSX.
|
|
155
|
+
* \`<MyComponent.RC />\`
|
|
156
|
+
*/
|
|
157
|
+
ClassComponent.RC = _a.extract();
|
|
178
158
|
/** /
|
|
179
159
|
testing: {
|
|
180
160
|
const a: object = {b: ''};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setFunctionName = void 0;
|
|
1
4
|
/** Provide more useful stack traces for otherwise non-specific function names. */
|
|
2
|
-
|
|
5
|
+
const setFunctionName = (func, newName) => {
|
|
3
6
|
try {
|
|
4
7
|
// Must use try block, as `name` is not configurable on older browsers, and may yield a TypeError.
|
|
5
8
|
Object.defineProperty(func, 'name', {
|
|
@@ -11,3 +14,4 @@ export var setFunctionName = function (func, newName) {
|
|
|
11
14
|
console.warn(error);
|
|
12
15
|
}
|
|
13
16
|
};
|
|
17
|
+
exports.setFunctionName = setFunctionName;
|
package/build/classy/index.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
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("./logic"), exports);
|
|
18
|
+
__exportStar(require("./instance"), exports);
|
|
19
|
+
__exportStar(require("./class"), exports);
|