@cleanweb/react 1.1.1-beta.9 → 2.1.0-beta.0
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/README.md +242 -187
- package/README.old.md +342 -0
- package/build/base/index.d.ts +1 -0
- package/build/base/index.js +1 -0
- package/build/base/merged-state.d.ts +1 -0
- package/build/base/merged-state.js +3 -2
- package/build/base/methods.d.ts +39 -6
- package/build/base/methods.js +45 -80
- package/build/base/state/class-types.d.ts +17 -0
- package/build/base/state/class-types.js +2 -0
- package/build/base/state/class.d.ts +14 -0
- package/build/base/{state.js → state/class.js} +4 -51
- package/build/base/state/hook-types.d.ts +12 -0
- package/build/base/state/hook-types.js +2 -0
- package/build/base/state/hooks.d.ts +6 -0
- package/build/base/state/hooks.js +39 -0
- package/build/base/state/index.d.ts +4 -0
- package/build/base/state/index.js +8 -0
- package/build/classy/class/index.d.ts +83 -0
- package/build/classy/class/index.js +149 -0
- package/build/classy/class/types/extractor.d.ts +5 -0
- package/build/classy/class/types/extractor.js +2 -0
- package/build/classy/class/utils/function-name.d.ts +2 -0
- package/build/classy/class/utils/function-name.js +17 -0
- package/build/classy/class/utils/rerender.d.ts +1 -0
- package/build/classy/class/utils/rerender.js +23 -0
- package/build/classy/class/utils/use-component/index.d.ts +6 -0
- package/build/classy/class/utils/use-component/index.js +17 -0
- package/build/classy/class/utils/use-component/types.d.ts +22 -0
- package/build/classy/class/utils/use-component/types.js +2 -0
- package/build/classy/instance/index.d.ts +64 -0
- package/build/classy/{instance.js → instance/index.js} +53 -42
- package/build/classy/instance/mount-callbacks.d.ts +4 -0
- package/build/classy/instance/mount-callbacks.js +30 -0
- package/build/classy/instance/types/hook.d.ts +13 -0
- package/build/classy/instance/types/hook.js +2 -0
- package/build/classy/logic/index.d.ts +48 -0
- package/build/classy/logic/index.js +113 -0
- package/build/classy/logic/types/hook.d.ts +17 -0
- package/build/classy/logic/types/hook.js +2 -0
- package/build/globals.d.ts +49 -33
- package/build/globals.js +2 -20
- package/build/helpers/index.d.ts +1 -0
- package/build/helpers/index.js +17 -0
- package/build/helpers/mount-state.d.ts +5 -0
- package/build/helpers/mount-state.js +25 -0
- package/build/index.d.ts +3 -1
- package/build/index.js +3 -15
- package/build/tsconfig.json +3 -0
- package/package.json +4 -11
- package/build/base/state.d.ts +0 -31
- package/build/classy/class.d.ts +0 -24
- package/build/classy/class.js +0 -132
- package/build/classy/instance.d.ts +0 -61
- package/build/classy/logic.d.ts +0 -20
- package/build/classy/logic.js +0 -82
- package/build/globals.private.d.ts +0 -44
- package/build/globals.private.js +0 -34
@@ -11,26 +11,8 @@ var __assign = (this && this.__assign) || function () {
|
|
11
11
|
return __assign.apply(this, arguments);
|
12
12
|
};
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
-
exports.
|
14
|
+
exports.CleanState = exports.CleanStateBase = void 0;
|
15
15
|
var react_1 = require("react");
|
16
|
-
/**
|
17
|
-
* Returns a value that is false before the component has been mounted,
|
18
|
-
* then true during all subsequent rerenders.
|
19
|
-
*/
|
20
|
-
var useMountState = function () {
|
21
|
-
/**
|
22
|
-
* This must not be a stateful value. It should not be the cause of a rerender.
|
23
|
-
* It merely provides information about the render count,
|
24
|
-
* without influencing that count itself.
|
25
|
-
* So `mounted` should never be set with `useState`.
|
26
|
-
*/
|
27
|
-
var mounted = (0, react_1.useRef)(false);
|
28
|
-
(0, react_1.useEffect)(function () {
|
29
|
-
mounted.current = true;
|
30
|
-
}, []);
|
31
|
-
return mounted.current;
|
32
|
-
};
|
33
|
-
exports.useMountState = useMountState;
|
34
16
|
var CleanStateBase = /** @class */ (function () {
|
35
17
|
function CleanStateBase(initialState) {
|
36
18
|
var _this = this;
|
@@ -85,7 +67,7 @@ var CleanStateBase = /** @class */ (function () {
|
|
85
67
|
});
|
86
68
|
CleanStateBase.update = function update() {
|
87
69
|
var _this = this;
|
88
|
-
if (!(this instanceof CleanState))
|
70
|
+
if (!(this instanceof exports.CleanState))
|
89
71
|
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);');
|
90
72
|
/**
|
91
73
|
* Linters complain about the use of a React hook within a loop because:
|
@@ -115,35 +97,6 @@ var CleanStateBase = /** @class */ (function () {
|
|
115
97
|
};
|
116
98
|
return CleanStateBase;
|
117
99
|
}());
|
100
|
+
exports.CleanStateBase = CleanStateBase;
|
118
101
|
;
|
119
|
-
|
120
|
-
var useCleanState = function (_initialState) {
|
121
|
-
var props = [];
|
122
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
123
|
-
props[_i - 1] = arguments[_i];
|
124
|
-
}
|
125
|
-
var initialState = typeof _initialState === 'function'
|
126
|
-
? (0, react_1.useMemo)(function () { return _initialState.apply(void 0, props); }, [])
|
127
|
-
: _initialState;
|
128
|
-
;
|
129
|
-
var cleanState = (0, react_1.useRef)((0, react_1.useMemo)(function () {
|
130
|
-
return new CleanState(initialState);
|
131
|
-
}, [])).current;
|
132
|
-
CleanState.update.call(cleanState);
|
133
|
-
return cleanState;
|
134
|
-
};
|
135
|
-
exports.useCleanState = useCleanState;
|
136
|
-
// Should be valid.
|
137
|
-
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6);
|
138
|
-
// useCleanState((a: boolean) => ({b: a.toString()}), true);
|
139
|
-
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), 6);
|
140
|
-
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), 6, 'word');
|
141
|
-
// useCleanState((a: number, c: string) => ({ b: a + c, f: true }), 6, 'text');
|
142
|
-
// useCleanState({ d: 5000 });
|
143
|
-
// Should fail.
|
144
|
-
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6, false);
|
145
|
-
// useCleanState((a: boolean) => ({b: a.toString()}));
|
146
|
-
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), '6');
|
147
|
-
// useCleanState((a: number, c?: string) => ({ b: `${a}` }));
|
148
|
-
// useCleanState((a: number, c: string) => ({ b: a + c, f: true }), 6, 7);
|
149
|
-
// useCleanState({ d: 5000 }, true);
|
102
|
+
exports.CleanState = CleanStateBase;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { CleanStateBase } from './class';
|
2
|
+
export type TStateData = object & {
|
3
|
+
[Key in keyof CleanStateBase<{}>]?: never;
|
4
|
+
};
|
5
|
+
/** @see https://github.com/cleanjsweb/neat-react#clean-state */
|
6
|
+
export type TCleanState<TState extends TStateData> = (CleanStateBase<TState> & Omit<TState, keyof CleanStateBase<{}>>);
|
7
|
+
export type ExtractCleanStateData<YourCleanState extends CleanStateBase<{}>> = Omit<YourCleanState, keyof CleanStateBase<{}>>;
|
8
|
+
type StateInitFunction = (...args: any[]) => object;
|
9
|
+
type StateInit = object | StateInitFunction;
|
10
|
+
export type TInitialState<Initializer extends StateInit> = Initializer extends (...args: any[]) => (infer TState extends object) ? TState : Initializer;
|
11
|
+
export type TUseCleanState = <TInit extends StateInit>(_initialState: TInit, ...props: TInit extends (...args: infer TProps extends any[]) => (infer TState extends object) ? TProps : []) => TCleanState<TInitialState<TInit>>;
|
12
|
+
export {};
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { TUseCleanState } from './hook-types';
|
2
|
+
/**
|
3
|
+
* Creates a state object, which includes the provided values, and helper methods for
|
4
|
+
* updating those values and automatically rerendering your component's UI accordingly.
|
5
|
+
*/
|
6
|
+
export declare const useCleanState: TUseCleanState;
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useCleanState = void 0;
|
4
|
+
var react_1 = require("react");
|
5
|
+
var class_1 = require("./class");
|
6
|
+
/**
|
7
|
+
* Creates a state object, which includes the provided values, and helper methods for
|
8
|
+
* updating those values and automatically rerendering your component's UI accordingly.
|
9
|
+
*/
|
10
|
+
var useCleanState = function (_initialState) {
|
11
|
+
var props = [];
|
12
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
13
|
+
props[_i - 1] = arguments[_i];
|
14
|
+
}
|
15
|
+
var initialState = typeof _initialState === 'function'
|
16
|
+
? (0, react_1.useMemo)(function () { return _initialState.apply(void 0, props); }, [])
|
17
|
+
: _initialState;
|
18
|
+
;
|
19
|
+
var cleanState = (0, react_1.useRef)((0, react_1.useMemo)(function () {
|
20
|
+
return new class_1.CleanState(initialState);
|
21
|
+
}, [])).current;
|
22
|
+
class_1.CleanState.update.call(cleanState);
|
23
|
+
return cleanState;
|
24
|
+
};
|
25
|
+
exports.useCleanState = useCleanState;
|
26
|
+
// Should be valid.
|
27
|
+
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6);
|
28
|
+
// useCleanState((a: boolean) => ({b: a.toString()}), true);
|
29
|
+
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), 6);
|
30
|
+
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), 6, 'word');
|
31
|
+
// useCleanState((a: number, c: string) => ({ b: a + c, f: true }), 6, 'text');
|
32
|
+
// useCleanState({ d: 5000 });
|
33
|
+
// Should fail.
|
34
|
+
// useCleanState((a: number) => ({b: a.toString(), q: 1}), 6, false);
|
35
|
+
// useCleanState((a: boolean) => ({b: a.toString()}));
|
36
|
+
// useCleanState((a: number, c?: string) => ({ b: `${a}` }), '6');
|
37
|
+
// useCleanState((a: number, c?: string) => ({ b: `${a}` }));
|
38
|
+
// useCleanState((a: number, c: string) => ({ b: a + c, f: true }), 6, 7);
|
39
|
+
// useCleanState({ d: 5000 }, true);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useCleanState = exports.CleanState = void 0;
|
4
|
+
require("../../globals");
|
5
|
+
var class_1 = require("./class");
|
6
|
+
Object.defineProperty(exports, "CleanState", { enumerable: true, get: function () { return class_1.CleanState; } });
|
7
|
+
var hooks_1 = require("./hooks");
|
8
|
+
Object.defineProperty(exports, "useCleanState", { enumerable: true, get: function () { return hooks_1.useCleanState; } });
|
@@ -0,0 +1,83 @@
|
|
1
|
+
import type { Extractor } from './types/extractor';
|
2
|
+
import { ComponentInstance } from '../instance';
|
3
|
+
/**
|
4
|
+
* A superset of {@link ComponentInstance} that allows defining your
|
5
|
+
* component's JSX template directly inside the class.
|
6
|
+
*
|
7
|
+
* This is designed to closely resemble the old {@link React.Component} class,
|
8
|
+
* making it easier to migrate older class components to the newer hooks-based system
|
9
|
+
* with little to no changes to their existing semantics/implementation.
|
10
|
+
*/
|
11
|
+
export declare class ClassComponent<TProps extends object = WeakEmptyObject> extends ComponentInstance<TProps> {
|
12
|
+
/**
|
13
|
+
* Analogous to {@link React.Component.render}. A function that returns
|
14
|
+
* your component's JSX template.
|
15
|
+
*
|
16
|
+
* You should place most logic that would usually go here
|
17
|
+
* in {@link ComponentInstance.beforeRender | `beforeRender`} instead.
|
18
|
+
* This helps to separate concerns and keep the template itself clean.
|
19
|
+
*
|
20
|
+
* ******
|
21
|
+
*
|
22
|
+
* Ideally the template method should only be concerned with defining the HTML/JSX structure of
|
23
|
+
* your component's UI. This may include destructuring nested instance members
|
24
|
+
* into more easily accessible local variables, or some simple transformation of data from props/state
|
25
|
+
* into a more appropriate format for display.
|
26
|
+
*
|
27
|
+
* ******
|
28
|
+
*
|
29
|
+
* @example <caption>Using a template function that returns JSX.</caption>
|
30
|
+
*
|
31
|
+
* ```tsx
|
32
|
+
* template = () => {
|
33
|
+
* const { title } = this.props;
|
34
|
+
*
|
35
|
+
* return (
|
36
|
+
* <h1>
|
37
|
+
* {this.props.title}
|
38
|
+
* </h1>
|
39
|
+
* );
|
40
|
+
* }
|
41
|
+
* ```
|
42
|
+
*/
|
43
|
+
template: (context: this['templateContext']) => JSX.Element | null;
|
44
|
+
/**
|
45
|
+
* Manually trigger a rerender of your component.
|
46
|
+
* You should rarely ever need this. But if you are migrating
|
47
|
+
* an older React.Component class, this should provide similar functionality
|
48
|
+
* to the {@link React.Component.forceUpdate | `forceUpdate`} method provided there.
|
49
|
+
*
|
50
|
+
* Note that the callback argument is currently not supported.
|
51
|
+
*/
|
52
|
+
readonly forceUpdate: VoidFunction;
|
53
|
+
/*************************************
|
54
|
+
* Function Component Extractor *
|
55
|
+
**************************************/
|
56
|
+
/**
|
57
|
+
* Extract a Function Component (FC) which can be used to render
|
58
|
+
* your ClassComponent just like any other React component.
|
59
|
+
*
|
60
|
+
* Each JSX reference to the returned component will render with
|
61
|
+
* a separate instance of your class.
|
62
|
+
*
|
63
|
+
* So you only need to call `YourClassComponent.FC()` once, then use the returned
|
64
|
+
* function component as many times as you need.
|
65
|
+
*
|
66
|
+
* It is recommended to store this returned value as a static member of
|
67
|
+
* your ClassComponent. While this value may be given any name, the name
|
68
|
+
* RC (for "React Component") is the recommended convention.
|
69
|
+
*
|
70
|
+
* @example <caption>Calling FC in your ClassComponent</caption>
|
71
|
+
* class Button extends ClassComponent {
|
72
|
+
* static readonly RC = this.FC();
|
73
|
+
* // Because of the static keyword, `this` here refers to the class itself, same as calling `Button.FC()`.
|
74
|
+
* }
|
75
|
+
*
|
76
|
+
* // Render with `<Button.RC />`, or export RC to use the component in other files.
|
77
|
+
* export default Button.RC;
|
78
|
+
*/
|
79
|
+
static readonly extract: Extractor;
|
80
|
+
/** @see {@link ClassComponent.extract} */
|
81
|
+
static readonly FC: Extractor;
|
82
|
+
}
|
83
|
+
export { ClassComponent as Component };
|
@@ -0,0 +1,149 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
3
|
+
var extendStatics = function (d, b) {
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7
|
+
return extendStatics(d, b);
|
8
|
+
};
|
9
|
+
return function (d, b) {
|
10
|
+
if (typeof b !== "function" && b !== null)
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12
|
+
extendStatics(d, b);
|
13
|
+
function __() { this.constructor = d; }
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15
|
+
};
|
16
|
+
})();
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
+
exports.Component = exports.ClassComponent = void 0;
|
19
|
+
var react_1 = require("react");
|
20
|
+
var instance_1 = require("../instance");
|
21
|
+
var function_name_1 = require("./utils/function-name");
|
22
|
+
var rerender_1 = require("./utils/rerender");
|
23
|
+
/**
|
24
|
+
* A superset of {@link ComponentInstance} that allows defining your
|
25
|
+
* component's JSX template directly inside the class.
|
26
|
+
*
|
27
|
+
* This is designed to closely resemble the old {@link React.Component} class,
|
28
|
+
* making it easier to migrate older class components to the newer hooks-based system
|
29
|
+
* with little to no changes to their existing semantics/implementation.
|
30
|
+
*/
|
31
|
+
var ClassComponent = /** @class */ (function (_super) {
|
32
|
+
__extends(ClassComponent, _super);
|
33
|
+
function ClassComponent() {
|
34
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
35
|
+
/**
|
36
|
+
* Analogous to {@link React.Component.render}. A function that returns
|
37
|
+
* your component's JSX template.
|
38
|
+
*
|
39
|
+
* You should place most logic that would usually go here
|
40
|
+
* in {@link ComponentInstance.beforeRender | `beforeRender`} instead.
|
41
|
+
* This helps to separate concerns and keep the template itself clean.
|
42
|
+
*
|
43
|
+
* ******
|
44
|
+
*
|
45
|
+
* Ideally the template method should only be concerned with defining the HTML/JSX structure of
|
46
|
+
* your component's UI. This may include destructuring nested instance members
|
47
|
+
* into more easily accessible local variables, or some simple transformation of data from props/state
|
48
|
+
* into a more appropriate format for display.
|
49
|
+
*
|
50
|
+
* ******
|
51
|
+
*
|
52
|
+
* @example <caption>Using a template function that returns JSX.</caption>
|
53
|
+
*
|
54
|
+
* ```tsx
|
55
|
+
* template = () => {
|
56
|
+
* const { title } = this.props;
|
57
|
+
*
|
58
|
+
* return (
|
59
|
+
* <h1>
|
60
|
+
* {this.props.title}
|
61
|
+
* </h1>
|
62
|
+
* );
|
63
|
+
* }
|
64
|
+
* ```
|
65
|
+
*/
|
66
|
+
_this.template = function () { return null; };
|
67
|
+
return _this;
|
68
|
+
}
|
69
|
+
var _a;
|
70
|
+
_a = ClassComponent;
|
71
|
+
/*************************************
|
72
|
+
* Function Component Extractor *
|
73
|
+
**************************************/
|
74
|
+
/**
|
75
|
+
* Extract a Function Component (FC) which can be used to render
|
76
|
+
* your ClassComponent just like any other React component.
|
77
|
+
*
|
78
|
+
* Each JSX reference to the returned component will render with
|
79
|
+
* a separate instance of your class.
|
80
|
+
*
|
81
|
+
* So you only need to call `YourClassComponent.FC()` once, then use the returned
|
82
|
+
* function component as many times as you need.
|
83
|
+
*
|
84
|
+
* It is recommended to store this returned value as a static member of
|
85
|
+
* your ClassComponent. While this value may be given any name, the name
|
86
|
+
* RC (for "React Component") is the recommended convention.
|
87
|
+
*
|
88
|
+
* @example <caption>Calling FC in your ClassComponent</caption>
|
89
|
+
* class Button extends ClassComponent {
|
90
|
+
* static readonly RC = this.FC();
|
91
|
+
* // Because of the static keyword, `this` here refers to the class itself, same as calling `Button.FC()`.
|
92
|
+
* }
|
93
|
+
*
|
94
|
+
* // Render with `<Button.RC />`, or export RC to use the component in other files.
|
95
|
+
* export default Button.RC;
|
96
|
+
*/
|
97
|
+
ClassComponent.extract = function FC(_Component) {
|
98
|
+
var Component = _Component !== null && _Component !== void 0 ? _Component : this;
|
99
|
+
var isClassComponentType = Component.prototype instanceof _a;
|
100
|
+
if (!isClassComponentType)
|
101
|
+
throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either pass a class that extends ClassComponent to FC (e.g `export FC(MyComponent);`), or ensure it is called as a method on a ClassComponent constructor type (e.g `export MyComponent.FC()`).');
|
102
|
+
/*************************************
|
103
|
+
* Begin Function Component *
|
104
|
+
**************************************/
|
105
|
+
/** A class-based React function component created with (@cleanweb/react).{@link ClassComponent} */
|
106
|
+
var Wrapper = function (props) {
|
107
|
+
var instance = (0, instance_1.useInstance)(Component, props);
|
108
|
+
var template = instance.template, templateContext = instance.templateContext;
|
109
|
+
var _forceUpdate;
|
110
|
+
// @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
|
111
|
+
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`.
|
112
|
+
);
|
113
|
+
// Add calling component name to template function name in stack traces.
|
114
|
+
(0, react_1.useMemo)(function () {
|
115
|
+
(0, function_name_1.setFunctionName)(template, "".concat(Component.name, ".template"));
|
116
|
+
}, [template]);
|
117
|
+
return template(templateContext);
|
118
|
+
};
|
119
|
+
/**************************************
|
120
|
+
* End Function Component *
|
121
|
+
**************************************/
|
122
|
+
(0, function_name_1.setFunctionName)(Wrapper, "$".concat(Component.name, "$"));
|
123
|
+
return Wrapper;
|
124
|
+
};
|
125
|
+
/** @see {@link ClassComponent.extract} */
|
126
|
+
ClassComponent.FC = _a.extract;
|
127
|
+
return ClassComponent;
|
128
|
+
}(instance_1.ComponentInstance));
|
129
|
+
exports.ClassComponent = ClassComponent;
|
130
|
+
exports.Component = ClassComponent;
|
131
|
+
/**/
|
132
|
+
testing: {
|
133
|
+
var a = { b: '' };
|
134
|
+
var MyComponentLogic = /** @class */ (function (_super) {
|
135
|
+
__extends(MyComponentLogic, _super);
|
136
|
+
function MyComponentLogic() {
|
137
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
138
|
+
_this.getInitialState = function () { return ({ a: '' }); };
|
139
|
+
// a = () => this.hooks.a = '';
|
140
|
+
_this.useHooks = function () {
|
141
|
+
_this.state.a;
|
142
|
+
};
|
143
|
+
return _this;
|
144
|
+
}
|
145
|
+
return MyComponentLogic;
|
146
|
+
}(ClassComponent));
|
147
|
+
;
|
148
|
+
var Template = MyComponentLogic.FC();
|
149
|
+
} /**/
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import type { VoidFunctionComponent } from 'react';
|
2
|
+
import type { ClassComponent } from '..';
|
3
|
+
type BaseCCConstructor = typeof ClassComponent<object>;
|
4
|
+
export type Extractor = <TComponentClass extends BaseCCConstructor>(this: TComponentClass, Component?: TComponentClass) => VoidFunctionComponent<InstanceType<TComponentClass>['props']>;
|
5
|
+
export {};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.setFunctionName = void 0;
|
4
|
+
/** Provide more useful stack traces for otherwise non-specific function names. */
|
5
|
+
var setFunctionName = function (func, newName) {
|
6
|
+
try {
|
7
|
+
// Must use try block, as `name` is not configurable on older browsers, and may yield a TypeError.
|
8
|
+
Object.defineProperty(func, 'name', {
|
9
|
+
writable: true,
|
10
|
+
value: newName,
|
11
|
+
});
|
12
|
+
}
|
13
|
+
catch (error) {
|
14
|
+
console.warn(error);
|
15
|
+
}
|
16
|
+
};
|
17
|
+
exports.setFunctionName = setFunctionName;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const useRerender: () => () => void;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useRerender = void 0;
|
4
|
+
var mount_state_1 = require("../../../helpers/mount-state");
|
5
|
+
var react_1 = require("react");
|
6
|
+
var useRerender = function () {
|
7
|
+
var isMounted = (0, mount_state_1.useMountState)();
|
8
|
+
// Skip the value, we don't need it. Grab just the setter function.
|
9
|
+
var _a = (0, react_1.useState)(Date.now()), _forceRerender = _a[1];
|
10
|
+
var rerender = function () {
|
11
|
+
if (isMounted()) {
|
12
|
+
_forceRerender(Date.now());
|
13
|
+
return;
|
14
|
+
}
|
15
|
+
setTimeout(function () {
|
16
|
+
if (!isMounted())
|
17
|
+
return;
|
18
|
+
_forceRerender(Date.now());
|
19
|
+
}, 1000);
|
20
|
+
};
|
21
|
+
return rerender;
|
22
|
+
};
|
23
|
+
exports.useRerender = useRerender;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Use = void 0;
|
4
|
+
var react_1 = require("react");
|
5
|
+
/**
|
6
|
+
* A component you can use to consume hooks
|
7
|
+
* in a {@link Component | React.Component} class component.
|
8
|
+
*/
|
9
|
+
var Use = function (params) {
|
10
|
+
var useGenericHook = params.hook, argumentsList = params.argumentsList, onUpdate = params.onUpdate;
|
11
|
+
var output = useGenericHook.apply(void 0, argumentsList);
|
12
|
+
(0, react_1.useEffect)(function () {
|
13
|
+
onUpdate(output);
|
14
|
+
}, [output]);
|
15
|
+
return null;
|
16
|
+
};
|
17
|
+
exports.Use = Use;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
interface HookWrapperProps<THookFunction extends AnyFunction> {
|
2
|
+
/**
|
3
|
+
* The React hook you which to consume.
|
4
|
+
* Render a separate instance of the `<Use />` component for each hook.
|
5
|
+
* You can also create a custom hook that combines multiple hooks,
|
6
|
+
* then use that wrapper hook with a single `<Use />` instance.
|
7
|
+
*/
|
8
|
+
hook: THookFunction;
|
9
|
+
/**
|
10
|
+
* An array containing the list of arguments
|
11
|
+
* to be passed to your hook, in the right order.
|
12
|
+
*/
|
13
|
+
argumentsList: Parameters<THookFunction>;
|
14
|
+
/**
|
15
|
+
* A callback that will be called with whatever value your hook returns.
|
16
|
+
* Use this to update your component's state with the value.
|
17
|
+
* This will allow your component to rerender whenever the hook returns a new value.
|
18
|
+
*/
|
19
|
+
onUpdate: (output: ReturnType<THookFunction>) => void;
|
20
|
+
}
|
21
|
+
export type ClassComponentHookWrapper = <Hook extends AnyFunction>(props: HookWrapperProps<Hook>) => null;
|
22
|
+
export {};
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import type { UseInstance } from './types/hook';
|
2
|
+
import { ComponentLogic } from '../../classy/logic';
|
3
|
+
type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
|
4
|
+
/** An empty function. It returns (void) without performing any operations. */
|
5
|
+
export declare const noOp: () => void;
|
6
|
+
/**
|
7
|
+
* A superset of {@link ComponentLogic} that adds support for lifecycle methods.
|
8
|
+
* This provides a declarative API for working with your React function component's lifecycle,
|
9
|
+
* a simpler alternative to the imperative approach with `useEffect` and/or `useMemo`.
|
10
|
+
*
|
11
|
+
* @see https://github.com/cleanjsweb/neat-react#lifecycle-useinstance
|
12
|
+
*/
|
13
|
+
export declare class ComponentInstance<TProps extends object = {}> extends ComponentLogic<TProps> {
|
14
|
+
/**
|
15
|
+
* Runs only _before_ first render,
|
16
|
+
* i.e before the component instance is mounted.
|
17
|
+
*
|
18
|
+
* It is ignored on subsequent rerenders.
|
19
|
+
*
|
20
|
+
* PS: You can conditionally update state from here, but with certain caveats.
|
21
|
+
* {@link https://react.dev/reference/react/useState#storing-information-from-previous-renders | See the React docs for more details}.
|
22
|
+
*/
|
23
|
+
beforeMount: IVoidFunction;
|
24
|
+
/**
|
25
|
+
* Runs only **_after_** first render, i.e after the component instance is mounted.
|
26
|
+
* It is ignored on subsequent rerenders.
|
27
|
+
*
|
28
|
+
* Should usually only be used for logic that does not directly take part in determining what to render, like
|
29
|
+
* synchronize your component with some external system.
|
30
|
+
*
|
31
|
+
* @returns A cleanup function.
|
32
|
+
*
|
33
|
+
* Uses `useEffect()` under the hood.
|
34
|
+
*/
|
35
|
+
onMount: AsyncAllowedEffectCallback;
|
36
|
+
private _templateContext;
|
37
|
+
get templateContext(): ReturnType<this["beforeRender"]>;
|
38
|
+
/**
|
39
|
+
* Runs _before_ every render cycle, including the first.
|
40
|
+
* Useful for logic that is involved in determining what to render.
|
41
|
+
*
|
42
|
+
* PS: You can conditionally update state from here, but with certain caveats.
|
43
|
+
* {@link https://react.dev/reference/react/useState#storing-information-from-previous-renders | See the React docs for more details}.
|
44
|
+
*/
|
45
|
+
beforeRender: () => object | void;
|
46
|
+
/**
|
47
|
+
* Runs **_after_** every render cycle, including the first.
|
48
|
+
*
|
49
|
+
* Should usually only be used for logic that does not directly take part in determining what to render, like
|
50
|
+
* synchronize your component with some external system.
|
51
|
+
*
|
52
|
+
* Uses `useEffect()` under the hood.
|
53
|
+
*
|
54
|
+
* Returns a cleanup function.
|
55
|
+
*/
|
56
|
+
onRender: AsyncAllowedEffectCallback;
|
57
|
+
/**
|
58
|
+
* Runs when the component is unmounted.
|
59
|
+
* It is called _after_ the cleanup function returned by onMount.
|
60
|
+
*/
|
61
|
+
cleanUp: IVoidFunction;
|
62
|
+
}
|
63
|
+
export declare const useInstance: UseInstance;
|
64
|
+
export {};
|