@cleanweb/oore 1.0.6 → 1.1.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.
@@ -14,7 +14,7 @@ declare class MergedState<TState extends object> {
14
14
  }
15
15
  /**
16
16
  * Similar to {@link useCleanState},
17
- * but uses a single `useState` call for the entire object.
17
+ * but uses a single `useState` call for the entire state object.
18
18
  */
19
19
  export declare const useMergedState: <TState extends object>(initialState: TState) => MergedState<TState> & TState;
20
20
  export {};
@@ -72,7 +72,7 @@ var MergedState = /** @class */ (function () {
72
72
  }());
73
73
  /**
74
74
  * Similar to {@link useCleanState},
75
- * but uses a single `useState` call for the entire object.
75
+ * but uses a single `useState` call for the entire state object.
76
76
  */
77
77
  var useMergedState = function (initialState) {
78
78
  var cleanState = (0, react_1.useRef)((0, react_1.useMemo)(function () {
@@ -1,4 +1,4 @@
1
- import { ICleanStateClass, ICleanStateConstructor, PutManyPayload, PutState } from './class-types';
1
+ import type { ICleanStateClass, ICleanStateConstructor, PutManyPayload, PutState } from './class-types';
2
2
  /** @internal */
3
3
  export declare class CleanStateBase<TState extends Record<string, any>> {
4
4
  readonly reservedKeys: string[];
@@ -10,6 +10,59 @@ export declare class CleanStateBase<TState extends Record<string, any>> {
10
10
  static update: <TState_1 extends object>(this: CleanStateBase<TState_1>) => void;
11
11
  get put(): PutState<TState>;
12
12
  get initialState(): TState;
13
+ /**
14
+ * Accepts an object to be merged into the current state object,
15
+ * updating the values of specified keys to their specified values.
16
+ *
17
+ * All specified keys must be present on the initial state object.
18
+ * This is a requirement of React hooks, as the number of calls to
19
+ * useState must be the same throughout a component's lifetime.
20
+ *
21
+ * To dynamically add new keys to your state object, use a nested object,
22
+ * or use the {@link useMergedState | `useMergedState`} hook also exported by this library.
23
+ *
24
+ * When called from inside a component class, the type of the object passed
25
+ * must be explicitly specified with a type assertion.
26
+ * This is because the component classes use the dynamic `this` type to define
27
+ * the type of their state property.
28
+ *
29
+ * Use the `StateFragment` type, also aliased as `SF`, to define this type assertion.
30
+ * The assertion is not needed when calling putMany from outside the class.
31
+ *
32
+ * @example <caption>Using `putMany`</caption>
33
+ * import type { SF } from '@cleanweb/oore/base';
34
+ * import { ComponentLogic } from '@cleanweb/oore';
35
+ *
36
+ * class InputCL extends ComponentLogic<TProps> {
37
+ * getInitialState(props: TProps): TState => ({
38
+ * disabled: true,
39
+ * });
40
+ *
41
+ * enableInput = () => {
42
+ * this.state.putMany({
43
+ * disabled: false,
44
+ * } as SF<this>); // Type assertion required inside class.
45
+ * };
46
+ * }
47
+ *
48
+ * export const Input = (props: TProps) => {
49
+ * const self = useLogic(InputCL, props);
50
+ *
51
+ * return <>
52
+ * {
53
+ * // Other elements...
54
+ * }
55
+ * <button className="cta"
56
+ * onClick={() => {
57
+ * self.state.putMany({
58
+ * disabled: true
59
+ * }); // No type assertion needed outside the class.
60
+ * }}>
61
+ * Submit
62
+ * </button>
63
+ * </>;
64
+ * };
65
+ */
13
66
  readonly putMany: (newValues: PutManyPayload<TState>) => void;
14
67
  }
15
68
  /** @internal */
@@ -19,7 +19,59 @@ var CleanStateBase = /** @class */ (function () {
19
19
  var _this = this;
20
20
  this._values_ = {};
21
21
  this._setters_ = {};
22
- // readonly putMany = (newValues: Partial<TState>) => {
22
+ /**
23
+ * Accepts an object to be merged into the current state object,
24
+ * updating the values of specified keys to their specified values.
25
+ *
26
+ * All specified keys must be present on the initial state object.
27
+ * This is a requirement of React hooks, as the number of calls to
28
+ * useState must be the same throughout a component's lifetime.
29
+ *
30
+ * To dynamically add new keys to your state object, use a nested object,
31
+ * or use the {@link useMergedState | `useMergedState`} hook also exported by this library.
32
+ *
33
+ * When called from inside a component class, the type of the object passed
34
+ * must be explicitly specified with a type assertion.
35
+ * This is because the component classes use the dynamic `this` type to define
36
+ * the type of their state property.
37
+ *
38
+ * Use the `StateFragment` type, also aliased as `SF`, to define this type assertion.
39
+ * The assertion is not needed when calling putMany from outside the class.
40
+ *
41
+ * @example <caption>Using `putMany`</caption>
42
+ * import type { SF } from '@cleanweb/oore/base';
43
+ * import { ComponentLogic } from '@cleanweb/oore';
44
+ *
45
+ * class InputCL extends ComponentLogic<TProps> {
46
+ * getInitialState(props: TProps): TState => ({
47
+ * disabled: true,
48
+ * });
49
+ *
50
+ * enableInput = () => {
51
+ * this.state.putMany({
52
+ * disabled: false,
53
+ * } as SF<this>); // Type assertion required inside class.
54
+ * };
55
+ * }
56
+ *
57
+ * export const Input = (props: TProps) => {
58
+ * const self = useLogic(InputCL, props);
59
+ *
60
+ * return <>
61
+ * {
62
+ * // Other elements...
63
+ * }
64
+ * <button className="cta"
65
+ * onClick={() => {
66
+ * self.state.putMany({
67
+ * disabled: true
68
+ * }); // No type assertion needed outside the class.
69
+ * }}>
70
+ * Submit
71
+ * </button>
72
+ * </>;
73
+ * };
74
+ */
23
75
  this.putMany = function (newValues) {
24
76
  Object.entries(newValues).forEach(function (entry) {
25
77
  var _a = entry, key = _a[0], value = _a[1];
@@ -5,4 +5,5 @@ import '../../globals';
5
5
  export { CleanState } from './class';
6
6
  export { useCleanState } from './hooks';
7
7
  export type { TCleanState, TStateData, ExtractCleanStateData } from './hook-types';
8
+ export type { SF, StateFragment } from './class-types';
8
9
  export * as MergedState from '../../base/merged-state';
@@ -52,7 +52,7 @@ export declare class ComponentLogic<TProps extends TPropsBase = null> {
52
52
  * It receives the initial `props` object and should return
53
53
  * an object with the initial values for your component's state.
54
54
  */
55
- getInitialState: (props?: TProps extends null ? undefined : TProps) => object;
55
+ getInitialState: (props: TProps extends null ? undefined : TProps) => object;
56
56
  /**
57
57
  * Call React hooks from here. If your component needs
58
58
  * access to values return from the hooks you call,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/oore",
3
- "version": "1.0.6",
3
+ "version": "1.1.0",
4
4
  "description": "A library of helpers for writing cleaner React function components with object-oriented patterns.",
5
5
  "engines": {
6
6
  "node": ">=20"