@cleanweb/react 1.1.1-beta.16 → 1.1.1-beta.17

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.
@@ -1,4 +1,11 @@
1
1
  import type { TCleanState, TStateData } from './state';
2
+ /**
3
+ * Base class for a class that holds methods intended for use in a function component.
4
+ * These methods will have access to the components state and props via
5
+ * `this.state` and `this.props` respectively.
6
+ *
7
+ * Call the {@link useMethods} hook inside your function component to instantiate the class.
8
+ */
2
9
  export declare class ComponentMethods<TProps extends object = {}, TState extends TStateData | null = null> {
3
10
  readonly props: TProps;
4
11
  state: TState extends TStateData ? TCleanState<TState> : null;
@@ -8,5 +15,11 @@ type UseMethods = {
8
15
  <Class extends typeof ComponentMethods<object, null>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state?: null): InstanceType<Class>;
9
16
  <Class extends typeof ComponentMethods<HardEmptyObject, null>>(Methods: Class & Constructor<InstanceType<Class>>): InstanceType<Class>;
10
17
  };
18
+ /**
19
+ * Returns an instance of the provided class,
20
+ * with the state and props arguments added as instance members.
21
+ *
22
+ * `state` must be an instance of `CleanState` created with {@link useCleanState}.
23
+ */
11
24
  declare const useMethods: UseMethods;
12
25
  export { useMethods };
@@ -52,7 +52,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
54
  exports.useMethods = exports.ComponentMethods = void 0;
55
+ // Values
55
56
  var react_1 = require("react");
57
+ /**
58
+ * Base class for a class that holds methods intended for use in a function component.
59
+ * These methods will have access to the components state and props via
60
+ * `this.state` and `this.props` respectively.
61
+ *
62
+ * Call the {@link useMethods} hook inside your function component to instantiate the class.
63
+ */
56
64
  var ComponentMethods = /** @class */ (function () {
57
65
  function ComponentMethods() {
58
66
  }
@@ -60,6 +68,12 @@ var ComponentMethods = /** @class */ (function () {
60
68
  }());
61
69
  exports.ComponentMethods = ComponentMethods;
62
70
  ;
71
+ /**
72
+ * Returns an instance of the provided class,
73
+ * with the state and props arguments added as instance members.
74
+ *
75
+ * `state` must be an instance of `CleanState` created with {@link useCleanState}.
76
+ */
63
77
  var useMethods = function () {
64
78
  var args = [];
65
79
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -28,5 +28,9 @@ type StateInitFunction = (...args: any[]) => object;
28
28
  type StateInit = object | StateInitFunction;
29
29
  type TInitialState<Initializer extends StateInit> = Initializer extends (...args: any[]) => (infer TState extends object) ? TState : Initializer;
30
30
  type TUseCleanState = <TInit extends StateInit>(_initialState: TInit, ...props: TInit extends (...args: infer TProps extends any[]) => (infer TState extends object) ? TProps : []) => TCleanState<TInitialState<TInit>>;
31
+ /**
32
+ * Creates a state object, which includes the provided values, and helper methods for
33
+ * updating those values and automatically rerendering your component's UI accordingly.
34
+ */
31
35
  export declare const useCleanState: TUseCleanState;
32
36
  export {};
@@ -118,6 +118,10 @@ var CleanStateBase = /** @class */ (function () {
118
118
  }());
119
119
  ;
120
120
  var CleanState = CleanStateBase;
121
+ /**
122
+ * Creates a state object, which includes the provided values, and helper methods for
123
+ * updating those values and automatically rerendering your component's UI accordingly.
124
+ */
121
125
  var useCleanState = function (_initialState) {
122
126
  var props = [];
123
127
  for (var _i = 1; _i < arguments.length; _i++) {
@@ -8,19 +8,108 @@ type ComponentClassParams = ConstructorParameters<typeof ClassComponent>;
8
8
  type o = object;
9
9
  export interface IComponentClass<Instance extends ClassComponent<o, o, THooksBase> = ClassComponent, Params extends ComponentClassParams = ComponentClassParams> extends IComponentInstanceClass<Instance, Params> {
10
10
  }
11
- type Extractor = <TComponent extends typeof ClassComponent<o, o, o>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
11
+ type Extractor = <TComponent extends typeof ClassComponent<o, o, THooksBase>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
12
12
  type ReactTemplate = React.JSX.Element | null;
13
+ /**
14
+ * A superset of {@link ComponentInstance} that allows defining your
15
+ * component's JSX template directly inside the class.
16
+ *
17
+ * This is designed to closely resemble the old {@link React.Component} class,
18
+ * making it easier to migrate older class components to the newer hooks-based system
19
+ * with little to no changes to their existing semantics/implementation.
20
+ */
13
21
  export declare class ClassComponent<TProps extends o = WeakEmptyObject, TState extends TStateData = WeakEmptyObject, THooks extends THooksBase = void> extends ComponentInstance<TProps, TState, THooks> {
22
+ /**
23
+ * @deprecated An older alternative to {@link template}.
24
+ *
25
+ * Using this will add a component boundary between your JSX template
26
+ * and the function component returned from ClassComponent.FC();
27
+ *
28
+ * This means that from React's perspective, your template won't "own" the state and props it consumes.
29
+ * This could lead to subtle unexpected changes in behaviour.
30
+ *
31
+ * In most cases, you should use {@link template} instead, as it allows your class component
32
+ * to function more predictably as a single unit.
33
+ */
14
34
  Render?: VoidFunctionComponent<{}>;
35
+ /**
36
+ * Your components JSX template. This a variable that should represent what you
37
+ * would normally return from a regular function component.
38
+ *
39
+ * Alternatively, it can be a function that returns the required JSX.
40
+ * The function form is useful if your template references many nested instance members
41
+ * and you would like to destructure them into more easily accessible local variables.
42
+ *
43
+ * Besides that, you should place all other logic in {@link ComponentInstance.beforeRender | `beforeRender`}
44
+ * in order to separate concerns and keep the template itself clean.
45
+ *
46
+ * @example <caption>Using a simple JSX template</caption>
47
+ * template = (
48
+ * <h1>
49
+ * {this.props.title}
50
+ * </h1>
51
+ * );
52
+ *
53
+ * @example <caption>Using a template function that returns JSX.</caption>
54
+ * template = () => {
55
+ * const { title } = this.props;
56
+ *
57
+ * return (
58
+ * <h1>
59
+ * {this.props.title}
60
+ * </h1>
61
+ * );
62
+ * }
63
+ */
15
64
  template?: ReactTemplate | (() => ReactTemplate);
65
+ /**
66
+ * Manually trigger a rerender of your component.
67
+ * You should rarely ever need this. But if you are migrating
68
+ * an older React.Component class, this should provide similar functionality
69
+ * to the {@link Component.forceUpdate | `forceUpdate`} method provided there.
70
+ *
71
+ * Note that the callback argument is currently not supported.
72
+ */
16
73
  readonly forceUpdate: VoidFunction;
74
+ /*************************************
75
+ * Function Component Extractor *
76
+ **************************************/
77
+ /**
78
+ * Extract a function component which can be used to render
79
+ * your ClassComponent just like any other component.
80
+ *
81
+ * Each JSX reference to this returned component will render with
82
+ * a separate instance of your class.
83
+ *
84
+ * So you only need to call `YourClassComponent.FC()` once, then use the returned
85
+ * function component as many times as you need.
86
+ */
17
87
  static readonly FC: Extractor;
18
88
  }
19
89
  interface HookWrapperProps<THookFunction extends AnyFunction> {
90
+ /**
91
+ * The React hook you which to consume.
92
+ * Render a separate instance of the `<Use />` component for each hook.
93
+ * You can also create a custom hook that combines multiple hooks,
94
+ * then use that wrapper hook with a single `<Use />` instance.
95
+ */
20
96
  hook: THookFunction;
97
+ /**
98
+ * An array containing the list of arguments
99
+ * to be passed to your hook, in the right order.
100
+ */
21
101
  argumentsList: Parameters<THookFunction>;
102
+ /**
103
+ * A callback that will be called with whatever value your hook returns.
104
+ * Use this to update your component's state with the value.
105
+ * This will allow your component to rerender whenever the hook returns a new value.
106
+ */
22
107
  onUpdate: (output: ReturnType<THookFunction>) => void;
23
108
  }
24
109
  type ClassComponentHookWrapper = <Hook extends AnyFunction>(props: HookWrapperProps<Hook>) => null;
110
+ /**
111
+ * A component you can use to consume hooks
112
+ * in a {@link Component | React.Component} class component.
113
+ */
25
114
  export declare const Use: ClassComponentHookWrapper;
26
115
  export {};
@@ -43,16 +43,43 @@ var useRerender = function () {
43
43
  };
44
44
  exports.useRerender = useRerender;
45
45
  ;
46
+ /**
47
+ * A superset of {@link ComponentInstance} that allows defining your
48
+ * component's JSX template directly inside the class.
49
+ *
50
+ * This is designed to closely resemble the old {@link React.Component} class,
51
+ * making it easier to migrate older class components to the newer hooks-based system
52
+ * with little to no changes to their existing semantics/implementation.
53
+ */
46
54
  var ClassComponent = /** @class */ (function (_super) {
47
55
  __extends(ClassComponent, _super);
48
56
  function ClassComponent() {
49
57
  return _super !== null && _super.apply(this, arguments) || this;
50
58
  }
59
+ /*************************************
60
+ * Function Component Extractor *
61
+ **************************************/
62
+ // @todo Attempt using implicit `this` value to allow rendering <MyComponent.FC /> directly.
63
+ // const FC = (props) => { const self = useMemo(() => useInstance(this, props), {}); return self.template(); }
64
+ /**
65
+ * Extract a function component which can be used to render
66
+ * your ClassComponent just like any other component.
67
+ *
68
+ * Each JSX reference to this returned component will render with
69
+ * a separate instance of your class.
70
+ *
71
+ * So you only need to call `YourClassComponent.FC()` once, then use the returned
72
+ * function component as many times as you need.
73
+ */
51
74
  ClassComponent.FC = function FC(_Component) {
52
75
  var Component = _Component !== null && _Component !== void 0 ? _Component : this;
53
76
  var isClassComponentType = Component.prototype instanceof ClassComponent;
54
77
  if (!Component.getInitialState || !isClassComponentType)
55
78
  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()`).');
79
+ /*************************************
80
+ * Begin Function Component *
81
+ **************************************/
82
+ /** A class-based React function component created with (@cleanweb/react).ClassComponent */
56
83
  var Wrapper = function (props) {
57
84
  var instance = (0, instance_1.useInstance)(Component, props);
58
85
  var Render = instance.Render, template = instance.template;
@@ -106,13 +133,19 @@ var ClassComponent = /** @class */ (function (_super) {
106
133
  default: return template;
107
134
  }
108
135
  };
109
- // Include calling component name in wrapper function name on stack traces.
136
+ /*************************************
137
+ * End Function Component *
138
+ **************************************/
110
139
  setFunctionName(Wrapper, "$".concat(Component.name, "$"));
111
140
  return Wrapper;
112
141
  };
113
142
  return ClassComponent;
114
143
  }(instance_1.ComponentInstance));
115
144
  exports.ClassComponent = ClassComponent;
145
+ /**
146
+ * A component you can use to consume hooks
147
+ * in a {@link Component | React.Component} class component.
148
+ */
116
149
  var Use = function (params) {
117
150
  var useGenericHook = params.hook, argumentsList = params.argumentsList, onUpdate = params.onUpdate;
118
151
  var output = useGenericHook.apply(void 0, argumentsList);
@@ -127,7 +160,11 @@ testing: {
127
160
  var MyComponentLogic = /** @class */ (function (_super) {
128
161
  __extends(MyComponentLogic, _super);
129
162
  function MyComponentLogic() {
130
- return _super !== null && _super.apply(this, arguments) || this;
163
+ var _this = _super !== null && _super.apply(this, arguments) || this;
164
+ // a = () => this.hooks.a = '';
165
+ _this.useHooks = function () {
166
+ };
167
+ return _this;
131
168
  }
132
169
  MyComponentLogic.getInitialState = function () { return ({ a: '' }); };
133
170
  return MyComponentLogic;
@@ -2,9 +2,13 @@ import type { TStateData } from '../base/state';
2
2
  import type { THooksBase, IComponentLogicClass } from './logic';
3
3
  import { ComponentLogic } from './logic';
4
4
  type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
5
- type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
6
- export declare const useMountCallbacks: UseMountCallbacks;
5
+ /** An empty function. It returns (void) without performing any operations. */
7
6
  export declare const noOp: () => void;
7
+ /**
8
+ * A superset of {@link ComponentLogic} that adds support for special lifecycle methods.
9
+ * This provides a declarative API for working with your React function component's lifecycle,
10
+ * a simpler alternative to the imperative approach with `useEffect` and/or `useMemo`.
11
+ */
8
12
  export declare class ComponentInstance<TProps extends o = {}, TState extends TStateData = WeakEmptyObject, THooks extends THooksBase = void> extends ComponentLogic<TProps, TState, THooks> {
9
13
  /**
10
14
  * Runs only _before_ first render, i.e before the component instance is mounted.
@@ -15,7 +15,7 @@ var __extends = (this && this.__extends) || (function () {
15
15
  };
16
16
  })();
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.useInstance = exports.ComponentInstance = exports.noOp = exports.useMountCallbacks = void 0;
18
+ exports.useInstance = exports.ComponentInstance = exports.noOp = void 0;
19
19
  var react_1 = require("react");
20
20
  var state_1 = require("../base/state");
21
21
  var logic_1 = require("./logic");
@@ -43,10 +43,15 @@ var useMountCallbacks = function (instance) {
43
43
  };
44
44
  }, []);
45
45
  };
46
- exports.useMountCallbacks = useMountCallbacks;
46
+ /** An empty function. It returns (void) without performing any operations. */
47
47
  var noOp = function () { };
48
48
  exports.noOp = noOp;
49
49
  // @todo Use rollup. Insert globals.ts reference tag to all d.ts output files.
50
+ /**
51
+ * A superset of {@link ComponentLogic} that adds support for special lifecycle methods.
52
+ * This provides a declarative API for working with your React function component's lifecycle,
53
+ * a simpler alternative to the imperative approach with `useEffect` and/or `useMemo`.
54
+ */
50
55
  var ComponentInstance = /** @class */ (function (_super) {
51
56
  __extends(ComponentInstance, _super);
52
57
  function ComponentInstance() {
@@ -134,7 +139,7 @@ var useInstance = function () {
134
139
  '
135
140
  */
136
141
  // beforeMount, onMount, cleanUp.
137
- (0, exports.useMountCallbacks)(instance);
142
+ useMountCallbacks(instance);
138
143
  // beforeRender.
139
144
  (_a = instance.beforeRender) === null || _a === void 0 ? void 0 : _a.call(instance);
140
145
  // onRender.
@@ -3,11 +3,46 @@ export type HardEmpty = HardEmptyObject;
3
3
  export type WeakEmpty = WeakEmptyObject;
4
4
  export type THooksBase = o | void;
5
5
  type o = object;
6
- export declare class ComponentLogic<TProps extends object = {}, TState extends TStateData = WeakEmpty, THooks extends THooksBase = void> {
6
+ /**
7
+ * Base class for a class that holds methods intended for use in a function component,
8
+ * as well as a static method for initializing state.
9
+ *
10
+ * These methods will have access to the components state and props via
11
+ * `this.state` and `this.props` respectively.
12
+ *
13
+ * The special {@link ComponentLogic.useHooks | useHooks} method allows you to consume
14
+ * React hooks within this class.
15
+ *
16
+ * Call the {@link useLogic} hook inside your function component to instantiate the class.
17
+ */
18
+ export declare class ComponentLogic<
19
+ /** Describe the values your component expects to be passed as props. */
20
+ TProps extends object = {},
21
+ /** An object type that descibes your component's state. */
22
+ TState extends TStateData = WeakEmpty,
23
+ /** The object type returned by your component's {@link useHooks} method. */
24
+ THooks extends THooksBase = void> {
7
25
  state: TCleanState<TState>;
8
26
  readonly props: TProps;
9
27
  readonly hooks: THooks extends object ? THooks : WeakEmptyObject;
28
+ /**
29
+ * Called before each instance of your component is mounted.
30
+ * It receives the initial `props` object and should return
31
+ * an object with the initial values for your component's state.
32
+ */
10
33
  static getInitialState: (p?: object) => object;
34
+ /**
35
+ * This allows you to seamlessly consume React hooks in
36
+ * your class component.
37
+ *
38
+ * It is called after state and props are updated on each render.
39
+ * Call any hooks (e.g `useEffect`) you which to consume inside this function.
40
+ *
41
+ * To expose any returned values from your hooks to the rest of your component,
42
+ * return an object that contains all the relevant values.
43
+ *
44
+ * This object will be accessible as `this.hooks` to the rest of your class.
45
+ */
11
46
  useHooks: THooks extends void ? undefined | (() => void | HardEmptyObject) : () => THooks;
12
47
  }
13
48
  type LogicClassParams = ConstructorParameters<typeof ComponentLogic>;
@@ -19,9 +19,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.useLogic = exports.ComponentLogic = void 0;
20
20
  var react_1 = require("react");
21
21
  var state_1 = require("../base/state");
22
+ /**
23
+ * Base class for a class that holds methods intended for use in a function component,
24
+ * as well as a static method for initializing state.
25
+ *
26
+ * These methods will have access to the components state and props via
27
+ * `this.state` and `this.props` respectively.
28
+ *
29
+ * The special {@link ComponentLogic.useHooks | useHooks} method allows you to consume
30
+ * React hooks within this class.
31
+ *
32
+ * Call the {@link useLogic} hook inside your function component to instantiate the class.
33
+ */
22
34
  var ComponentLogic = /** @class */ (function () {
23
35
  function ComponentLogic() {
24
36
  }
37
+ /**
38
+ * Called before each instance of your component is mounted.
39
+ * It receives the initial `props` object and should return
40
+ * an object with the initial values for your component's state.
41
+ */
25
42
  ComponentLogic.getInitialState = function (p) { return ({}); };
26
43
  return ComponentLogic;
27
44
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.16",
3
+ "version": "1.1.1-beta.17",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"