extra-react-hooks 0.1.13 → 0.2.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.
Files changed (54) hide show
  1. package/README.md +21 -3
  2. package/dist/es2015/index.min.mjs +2 -2
  3. package/dist/es2015/index.min.mjs.map +1 -1
  4. package/dist/es2015/index.mjs +42 -7
  5. package/dist/es2015/index.mjs.map +1 -1
  6. package/dist/es2015/index.umd.js +44 -6
  7. package/dist/es2015/index.umd.js.map +1 -1
  8. package/dist/es2015/index.umd.min.js +2 -2
  9. package/dist/es2015/index.umd.min.js.map +1 -1
  10. package/dist/es2018/index.min.mjs +2 -2
  11. package/dist/es2018/index.min.mjs.map +1 -1
  12. package/dist/es2018/index.mjs +42 -7
  13. package/dist/es2018/index.mjs.map +1 -1
  14. package/dist/es2018/index.umd.js +44 -6
  15. package/dist/es2018/index.umd.js.map +1 -1
  16. package/dist/es2018/index.umd.min.js +2 -2
  17. package/dist/es2018/index.umd.min.js.map +1 -1
  18. package/lib/es2015/index.d.ts +3 -0
  19. package/lib/es2015/index.js +3 -0
  20. package/lib/es2015/index.js.map +1 -1
  21. package/lib/es2015/use-is-first-render.d.ts +1 -0
  22. package/lib/es2015/use-is-first-render.js +25 -0
  23. package/lib/es2015/use-is-first-render.js.map +1 -0
  24. package/lib/es2015/use-multiple-selection.d.ts +1 -1
  25. package/lib/es2015/use-multiple-selection.js +3 -3
  26. package/lib/es2015/use-multiple-selection.js.map +1 -1
  27. package/lib/es2015/use-render-counter.d.ts +1 -0
  28. package/lib/es2015/use-render-counter.js +11 -0
  29. package/lib/es2015/use-render-counter.js.map +1 -0
  30. package/lib/es2015/use-single-selection.d.ts +2 -2
  31. package/lib/es2015/use-single-selection.js +3 -3
  32. package/lib/es2015/use-single-selection.js.map +1 -1
  33. package/lib/es2015/use-update-effect.d.ts +2 -0
  34. package/lib/es2015/use-update-effect.js +15 -0
  35. package/lib/es2015/use-update-effect.js.map +1 -0
  36. package/lib/es2018/index.d.ts +3 -0
  37. package/lib/es2018/index.js +3 -0
  38. package/lib/es2018/index.js.map +1 -1
  39. package/lib/es2018/use-is-first-render.d.ts +1 -0
  40. package/lib/es2018/use-is-first-render.js +25 -0
  41. package/lib/es2018/use-is-first-render.js.map +1 -0
  42. package/lib/es2018/use-multiple-selection.d.ts +1 -1
  43. package/lib/es2018/use-multiple-selection.js +3 -3
  44. package/lib/es2018/use-multiple-selection.js.map +1 -1
  45. package/lib/es2018/use-render-counter.d.ts +1 -0
  46. package/lib/es2018/use-render-counter.js +11 -0
  47. package/lib/es2018/use-render-counter.js.map +1 -0
  48. package/lib/es2018/use-single-selection.d.ts +2 -2
  49. package/lib/es2018/use-single-selection.js +3 -3
  50. package/lib/es2018/use-single-selection.js.map +1 -1
  51. package/lib/es2018/use-update-effect.d.ts +2 -0
  52. package/lib/es2018/use-update-effect.js +15 -0
  53. package/lib/es2018/use-update-effect.js.map +1 -0
  54. package/package.json +1 -1
@@ -1,6 +1,9 @@
1
1
  export * from './use-increment';
2
2
  export * from './use-is-mounted';
3
+ export * from './use-is-first-render';
4
+ export * from './use-render-counter';
3
5
  export * from './use-mount';
6
+ export * from './use-update-effect';
4
7
  export * from './use-single-selection';
5
8
  export * from './use-multiple-selection';
6
9
  export * from './use-state-cycle';
@@ -16,7 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./use-increment"), exports);
18
18
  __exportStar(require("./use-is-mounted"), exports);
19
+ __exportStar(require("./use-is-first-render"), exports);
20
+ __exportStar(require("./use-render-counter"), exports);
19
21
  __exportStar(require("./use-mount"), exports);
22
+ __exportStar(require("./use-update-effect"), exports);
20
23
  __exportStar(require("./use-single-selection"), exports);
21
24
  __exportStar(require("./use-multiple-selection"), exports);
22
25
  __exportStar(require("./use-state-cycle"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,mDAAgC;AAChC,8CAA2B;AAC3B,yDAAsC;AACtC,2DAAwC;AACxC,oDAAiC;AACjC,+CAA4B;AAC5B,wDAAqC;AACrC,6CAA0B;AAC1B,qDAAkC;AAClC,6DAA0C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,mDAAgC;AAChC,wDAAqC;AACrC,uDAAoC;AACpC,8CAA2B;AAC3B,sDAAmC;AACnC,yDAAsC;AACtC,2DAAwC;AACxC,oDAAiC;AACjC,+CAA4B;AAC5B,wDAAqC;AACrC,6CAA0B;AAC1B,qDAAkC;AAClC,6DAA0C"}
@@ -0,0 +1 @@
1
+ export declare function useIsFirstRender(): () => boolean;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useIsFirstRender = void 0;
4
+ const react_1 = require("react");
5
+ var State;
6
+ (function (State) {
7
+ State[State["InitialValue"] = 0] = "InitialValue";
8
+ State[State["First"] = 1] = "First";
9
+ State[State["NotFirst"] = 2] = "NotFirst";
10
+ })(State || (State = {}));
11
+ function useIsFirstRender() {
12
+ const state = (0, react_1.useRef)(State.InitialValue);
13
+ switch (state.current) {
14
+ case State.InitialValue:
15
+ state.current = State.First;
16
+ break;
17
+ case State.First:
18
+ state.current = State.NotFirst;
19
+ break;
20
+ case State.NotFirst: break;
21
+ }
22
+ return () => state.current === State.First;
23
+ }
24
+ exports.useIsFirstRender = useIsFirstRender;
25
+ //# sourceMappingURL=use-is-first-render.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-is-first-render.js","sourceRoot":"","sources":["../../src/use-is-first-render.ts"],"names":[],"mappings":";;;AAAA,iCAA8B;AAE9B,IAAK,KAIJ;AAJD,WAAK,KAAK;IACR,iDAAY,CAAA;IACZ,mCAAK,CAAA;IACL,yCAAQ,CAAA;AACV,CAAC,EAJI,KAAK,KAAL,KAAK,QAIT;AAED,SAAgB,gBAAgB;IAC9B,MAAM,KAAK,GAAG,IAAA,cAAM,EAAC,KAAK,CAAC,YAAY,CAAC,CAAA;IAExC,QAAQ,KAAK,CAAC,OAAO,EAAE;QACrB,KAAK,KAAK,CAAC,YAAY;YACrB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,KAAK,CAAA;YAC3B,MAAK;QACP,KAAK,KAAK,CAAC,KAAK;YACd,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAA;YAC9B,MAAK;QACP,KAAK,KAAK,CAAC,QAAQ,CAAC,CAAC,MAAK;KAC3B;IAED,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,KAAK,KAAK,CAAC,KAAK,CAAA;AAC5C,CAAC;AAdD,4CAcC"}
@@ -7,7 +7,7 @@ interface IMultipleSelectionOption<T> {
7
7
  toggle(): void;
8
8
  }
9
9
  export declare function useMultipleSelection<T>(values: T[], defaultSelectedIndexes?: number[]): {
10
- values: T[];
10
+ selectedValues: T[];
11
11
  options: Array<IMultipleSelectionOption<T>>;
12
12
  };
13
13
  export {};
@@ -10,8 +10,8 @@ function useMultipleSelection(values, defaultSelectedIndexes = []) {
10
10
  (0, errors_1.assert)(index >= 0 && index < values.length, 'The index of parameter defaultSelectedIndexes must be in the range of 0 to values.length');
11
11
  }
12
12
  const [selectedIndexes, setSelectedIndexes] = (0, react_1.useState)(new Set(defaultSelectedIndexes));
13
- return {
14
- values: (0, iterable_operator_1.toArray)((0, iterable_operator_1.map)(selectedIndexes, i => values[i])),
13
+ return (0, react_1.useMemo)(() => ({
14
+ selectedValues: (0, iterable_operator_1.toArray)((0, iterable_operator_1.map)(selectedIndexes, i => values[i])),
15
15
  options: values.map((value, index) => {
16
16
  const selected = selectedIndexes.has(index);
17
17
  const select = () => {
@@ -33,7 +33,7 @@ function useMultipleSelection(values, defaultSelectedIndexes = []) {
33
33
  toggle: selected ? unselect : select
34
34
  };
35
35
  })
36
- };
36
+ }), [selectedIndexes]);
37
37
  }
38
38
  exports.useMultipleSelection = useMultipleSelection;
39
39
  //# sourceMappingURL=use-multiple-selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-multiple-selection.js","sourceRoot":"","sources":["../../src/use-multiple-selection.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,+CAA2C;AAC3C,yDAAgD;AAYhD,SAAgB,oBAAoB,CAClC,MAAW,EACX,yBAAmC,EAAE;IAKrC,IAAA,eAAM,EAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC3E,KAAK,MAAM,KAAK,IAAI,sBAAsB,EAAE;QAC1C,IAAA,eAAM,EACJ,KAAK,IAAI,CAAC,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,EACnC,0FAA0F,CAC3F,CAAA;KACF;IAED,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,IAAA,gBAAQ,EAAC,IAAI,GAAG,CAAC,sBAAsB,CAAC,CAAC,CAAA;IAEvF,OAAO;QACL,MAAM,EAAE,IAAA,2BAAO,EAAC,IAAA,uBAAG,EAAC,eAAe,EAAE,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACrD,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YACnC,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;YAC3C,MAAM,MAAM,GAAG,GAAG,EAAE;gBAClB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,CAAA;gBACpC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;gBACd,kBAAkB,CAAC,GAAG,CAAC,CAAA;YACzB,CAAC,CAAA;YACD,MAAM,QAAQ,GAAG,GAAG,EAAE;gBACpB,MAAM,GAAG,GAA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C,MAAM,MAAM,GAAG,GAAG,EAAE;gBAClB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,CAAA;gBACpC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;gBACd,kBAAkB,CAAC,GAAG,CAAC,CAAA;YACzB,CAAC,CAAA;YACD,MAAM,QAAQ,GAAG,GAAG,EAAE;gBACpB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,CAAA;gBACpC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;gBACjB,kBAAkB,CAAC,GAAG,CAAC,CAAA;YACzB,CAAC,CAAA;YAED,OAAO;gBACL,KAAK;gBACL,KAAK;gBACL,QAAQ;gBACR,MAAM;gBACN,QAAQ;gBACR,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM;aACrC,CAAA;QACH,CAAC,CAAC;KACH,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAA;AACxB,CAAC;AA1CD,oDA0CC"}
@@ -0,0 +1 @@
1
+ export declare function useRenderCounter(): number;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useRenderCounter = void 0;
4
+ const react_1 = require("react");
5
+ function useRenderCounter() {
6
+ const counter = (0, react_1.useRef)(0);
7
+ counter.current++;
8
+ return counter.current;
9
+ }
10
+ exports.useRenderCounter = useRenderCounter;
11
+ //# sourceMappingURL=use-render-counter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-render-counter.js","sourceRoot":"","sources":["../../src/use-render-counter.ts"],"names":[],"mappings":";;;AAAA,iCAA8B;AAE9B,SAAgB,gBAAgB;IAC9B,MAAM,OAAO,GAAG,IAAA,cAAM,EAAC,CAAC,CAAC,CAAA;IAEzB,OAAO,CAAC,OAAO,EAAE,CAAA;IAEjB,OAAO,OAAO,CAAC,OAAO,CAAA;AACxB,CAAC;AAND,4CAMC"}
@@ -5,10 +5,10 @@ export interface ISingleSelectionOption<T> {
5
5
  select(): void;
6
6
  }
7
7
  export declare function useSingleSelection<T>(values: T[], defaultSelectedIndex: number): {
8
- value: T;
8
+ selectedValue: T;
9
9
  options: Array<ISingleSelectionOption<T>>;
10
10
  };
11
11
  export declare function useSingleSelection<T>(values: T[]): {
12
- value: T | undefined;
12
+ selectedValue: T | undefined;
13
13
  options: Array<ISingleSelectionOption<T>>;
14
14
  };
@@ -10,15 +10,15 @@ function useSingleSelection(values, defaultSelectedIndex) {
10
10
  (0, errors_1.assert)(defaultSelectedIndex >= 0 && defaultSelectedIndex < values.length, 'The parameter defaultSelectedIndex must be in the range of 0 to values.length');
11
11
  }
12
12
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(defaultSelectedIndex);
13
- return {
14
- value: (0, types_1.isUndefined)(selectedIndex) ? undefined : values[selectedIndex],
13
+ return (0, react_1.useMemo)(() => ({
14
+ selectedValue: (0, types_1.isUndefined)(selectedIndex) ? undefined : values[selectedIndex],
15
15
  options: values.map((value, index) => ({
16
16
  value,
17
17
  index,
18
18
  selected: index === selectedIndex,
19
19
  select: () => setSelectedIndex(index)
20
20
  }))
21
- };
21
+ }), [selectedIndex]);
22
22
  }
23
23
  exports.useSingleSelection = useSingleSelection;
24
24
  //# sourceMappingURL=use-single-selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-single-selection.js","sourceRoot":"","sources":["../../src/use-single-selection.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,6CAA8D;AAC9D,+CAA2C;AAqB3C,SAAgB,kBAAkB,CAChC,MAAW,EACX,oBAA6B;IAE7B,IAAA,eAAM,EAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC3E,IAAI,IAAA,qBAAa,EAAC,oBAAoB,CAAC,EAAE;QACvC,IAAA,eAAM,EACJ,oBAAoB,IAAI,CAAC,IAAI,oBAAoB,GAAG,MAAM,CAAC,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M,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YACrC,KAAK;YACL,KAAK;YACL,QAAQ,EAAE,KAAK,KAAK,aAAa;YACjC,MAAM,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC;SACtC,CAAC,CAAC;KACJ,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAA;AACtB,CAAC;AAvBD,gDAuBC"}
@@ -0,0 +1,2 @@
1
+ import { EffectCallback } from 'react';
2
+ export declare function useUpdateEffect(effect: EffectCallback, deps?: React.DependencyList): void;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useUpdateEffect = void 0;
4
+ const react_1 = require("react");
5
+ const use_is_first_render_1 = require("./use-is-first-render");
6
+ function useUpdateEffect(effect, deps) {
7
+ const isFirstRender = (0, use_is_first_render_1.useIsFirstRender)();
8
+ (0, react_1.useEffect)(() => {
9
+ if (!isFirstRender()) {
10
+ return effect();
11
+ }
12
+ }, deps);
13
+ }
14
+ exports.useUpdateEffect = useUpdateEffect;
15
+ //# sourceMappingURL=use-update-effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-update-effect.js","sourceRoot":"","sources":["../../src/use-update-effect.ts"],"names":[],"mappings":";;;AAAA,iCAAiD;AACjD,+DAAwD;AAExD,SAAgB,eAAe,CAC7B,MAAsB,EACtB,IAA2B;IAE3B,MAAM,aAAa,GAAG,IAAA,sCAAgB,GAAE,CAAA;IAExC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,EAAE;YACpB,OAAO,MAAM,EAAE,CAAA;SAChB;IACH,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC;AAXD,0CAWC"}
@@ -1,6 +1,9 @@
1
1
  export * from './use-increment';
2
2
  export * from './use-is-mounted';
3
+ export * from './use-is-first-render';
4
+ export * from './use-render-counter';
3
5
  export * from './use-mount';
6
+ export * from './use-update-effect';
4
7
  export * from './use-single-selection';
5
8
  export * from './use-multiple-selection';
6
9
  export * from './use-state-cycle';
@@ -16,7 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./use-increment"), exports);
18
18
  __exportStar(require("./use-is-mounted"), exports);
19
+ __exportStar(require("./use-is-first-render"), exports);
20
+ __exportStar(require("./use-render-counter"), exports);
19
21
  __exportStar(require("./use-mount"), exports);
22
+ __exportStar(require("./use-update-effect"), exports);
20
23
  __exportStar(require("./use-single-selection"), exports);
21
24
  __exportStar(require("./use-multiple-selection"), exports);
22
25
  __exportStar(require("./use-state-cycle"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,mDAAgC;AAChC,8CAA2B;AAC3B,yDAAsC;AACtC,2DAAwC;AACxC,oDAAiC;AACjC,+CAA4B;AAC5B,wDAAqC;AACrC,6CAA0B;AAC1B,qDAAkC;AAClC,6DAA0C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,mDAAgC;AAChC,wDAAqC;AACrC,uDAAoC;AACpC,8CAA2B;AAC3B,sDAAmC;AACnC,yDAAsC;AACtC,2DAAwC;AACxC,oDAAiC;AACjC,+CAA4B;AAC5B,wDAAqC;AACrC,6CAA0B;AAC1B,qDAAkC;AAClC,6DAA0C"}
@@ -0,0 +1 @@
1
+ export declare function useIsFirstRender(): () => boolean;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useIsFirstRender = void 0;
4
+ const react_1 = require("react");
5
+ var State;
6
+ (function (State) {
7
+ State[State["InitialValue"] = 0] = "InitialValue";
8
+ State[State["First"] = 1] = "First";
9
+ State[State["NotFirst"] = 2] = "NotFirst";
10
+ })(State || (State = {}));
11
+ function useIsFirstRender() {
12
+ const state = (0, react_1.useRef)(State.InitialValue);
13
+ switch (state.current) {
14
+ case State.InitialValue:
15
+ state.current = State.First;
16
+ break;
17
+ case State.First:
18
+ state.current = State.NotFirst;
19
+ break;
20
+ case State.NotFirst: break;
21
+ }
22
+ return () => state.current === State.First;
23
+ }
24
+ exports.useIsFirstRender = useIsFirstRender;
25
+ //# sourceMappingURL=use-is-first-render.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-is-first-render.js","sourceRoot":"","sources":["../../src/use-is-first-render.ts"],"names":[],"mappings":";;;AAAA,iCAA8B;AAE9B,IAAK,KAIJ;AAJD,WAAK,KAAK;IACR,iDAAY,CAAA;IACZ,mCAAK,CAAA;IACL,yCAAQ,CAAA;AACV,CAAC,EAJI,KAAK,KAAL,KAAK,QAIT;AAED,SAAgB,gBAAgB;IAC9B,MAAM,KAAK,GAAG,IAAA,cAAM,EAAC,KAAK,CAAC,YAAY,CAAC,CAAA;IAExC,QAAQ,KAAK,CAAC,OAAO,EAAE;QACrB,KAAK,KAAK,CAAC,YAAY;YACrB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,KAAK,CAAA;YAC3B,MAAK;QACP,KAAK,KAAK,CAAC,KAAK;YACd,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAA;YAC9B,MAAK;QACP,KAAK,KAAK,CAAC,QAAQ,CAAC,CAAC,MAAK;KAC3B;IAED,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,KAAK,KAAK,CAAC,KAAK,CAAA;AAC5C,CAAC;AAdD,4CAcC"}
@@ -7,7 +7,7 @@ interface IMultipleSelectionOption<T> {
7
7
  toggle(): void;
8
8
  }
9
9
  export declare function useMultipleSelection<T>(values: T[], defaultSelectedIndexes?: number[]): {
10
- values: T[];
10
+ selectedValues: T[];
11
11
  options: Array<IMultipleSelectionOption<T>>;
12
12
  };
13
13
  export {};
@@ -10,8 +10,8 @@ function useMultipleSelection(values, defaultSelectedIndexes = []) {
10
10
  (0, errors_1.assert)(index >= 0 && index < values.length, 'The index of parameter defaultSelectedIndexes must be in the range of 0 to values.length');
11
11
  }
12
12
  const [selectedIndexes, setSelectedIndexes] = (0, react_1.useState)(new Set(defaultSelectedIndexes));
13
- return {
14
- values: (0, iterable_operator_1.toArray)((0, iterable_operator_1.map)(selectedIndexes, i => values[i])),
13
+ return (0, react_1.useMemo)(() => ({
14
+ selectedValues: (0, iterable_operator_1.toArray)((0, iterable_operator_1.map)(selectedIndexes, i => values[i])),
15
15
  options: values.map((value, index) => {
16
16
  const selected = selectedIndexes.has(index);
17
17
  const select = () => {
@@ -33,7 +33,7 @@ function useMultipleSelection(values, defaultSelectedIndexes = []) {
33
33
  toggle: selected ? unselect : select
34
34
  };
35
35
  })
36
- };
36
+ }), [selectedIndexes]);
37
37
  }
38
38
  exports.useMultipleSelection = useMultipleSelection;
39
39
  //# sourceMappingURL=use-multiple-selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-multiple-selection.js","sourceRoot":"","sources":["../../src/use-multiple-selection.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,+CAA2C;AAC3C,yDAAgD;AAYhD,SAAgB,oBAAoB,CAClC,MAAW,EACX,yBAAmC,EAAE;IAKrC,IAAA,eAAM,EAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC3E,KAAK,MAAM,KAAK,IAAI,sBAAsB,EAAE;QAC1C,IAAA,eAAM,EACJ,KAAK,IAAI,CAAC,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,EACnC,0FAA0F,CAC3F,CAAA;KACF;IAED,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,IAAA,gBAAQ,EAAC,IAAI,GAAG,CAAC,sBAAsB,CAAC,CAAC,CAAA;IAEvF,OAAO;QACL,MAAM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,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useRenderCounter = void 0;
4
+ const react_1 = require("react");
5
+ function useRenderCounter() {
6
+ const counter = (0, react_1.useRef)(0);
7
+ counter.current++;
8
+ return counter.current;
9
+ }
10
+ exports.useRenderCounter = useRenderCounter;
11
+ //# sourceMappingURL=use-render-counter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-render-counter.js","sourceRoot":"","sources":["../../src/use-render-counter.ts"],"names":[],"mappings":";;;AAAA,iCAA8B;AAE9B,SAAgB,gBAAgB;IAC9B,MAAM,OAAO,GAAG,IAAA,cAAM,EAAC,CAAC,CAAC,CAAA;IAEzB,OAAO,CAAC,OAAO,EAAE,CAAA;IAEjB,OAAO,OAAO,CAAC,OAAO,CAAA;AACxB,CAAC;AAND,4CAMC"}
@@ -5,10 +5,10 @@ export interface ISingleSelectionOption<T> {
5
5
  select(): void;
6
6
  }
7
7
  export declare function useSingleSelection<T>(values: T[], defaultSelectedIndex: number): {
8
- value: T;
8
+ selectedValue: T;
9
9
  options: Array<ISingleSelectionOption<T>>;
10
10
  };
11
11
  export declare function useSingleSelection<T>(values: T[]): {
12
- value: T | undefined;
12
+ selectedValue: T | undefined;
13
13
  options: Array<ISingleSelectionOption<T>>;
14
14
  };
@@ -10,15 +10,15 @@ function useSingleSelection(values, defaultSelectedIndex) {
10
10
  (0, errors_1.assert)(defaultSelectedIndex >= 0 && defaultSelectedIndex < values.length, 'The parameter defaultSelectedIndex must be in the range of 0 to values.length');
11
11
  }
12
12
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(defaultSelectedIndex);
13
- return {
14
- value: (0, types_1.isUndefined)(selectedIndex) ? undefined : values[selectedIndex],
13
+ return (0, react_1.useMemo)(() => ({
14
+ selectedValue: (0, types_1.isUndefined)(selectedIndex) ? undefined : values[selectedIndex],
15
15
  options: values.map((value, index) => ({
16
16
  value,
17
17
  index,
18
18
  selected: index === selectedIndex,
19
19
  select: () => setSelectedIndex(index)
20
20
  }))
21
- };
21
+ }), [selectedIndex]);
22
22
  }
23
23
  exports.useSingleSelection = useSingleSelection;
24
24
  //# sourceMappingURL=use-single-selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-single-selection.js","sourceRoot":"","sources":["../../src/use-single-selection.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,6CAA8D;AAC9D,+CAA2C;AAqB3C,SAAgB,kBAAkB,CAChC,MAAW,EACX,oBAA6B;IAE7B,IAAA,eAAM,EAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC3E,IAAI,IAAA,qBAAa,EAAC,oBAAoB,CAAC,EAAE;QACvC,IAAA,eAAM,EACJ,oBAAoB,IAAI,CAAC,IAAI,oBAAoB,GAAG,MAAM,CAAC,MAAM,EACjE,+EAA+E,CAChF,CAAA;KACF;IAED,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,IAAA,gBAAQ,EAAqB,oBAAoB,CAAC,CAAA;IAE5F,OAAO;QACL,KAAK,EAAE,IAAA,mBAAW,EAAC,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC;QACrE,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YACrC,KAAK;YACL,KAAK;YACL,QAAQ,EAAE,KAAK,KAAK,aAAa;YACjC,MAAM,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC;SACtC,CAAC,CAAC;KACJ,CAAA;AACH,CAAC;AAvBD,gDAuBC"}
1
+ {"version":3,"file":"use-single-selection.js","sourceRoot":"","sources":["../../src/use-single-selection.ts"],"names":[],"mappings":";;;AAAA,iCAAyC;AACzC,6CAA8D;AAC9D,+CAA2C;AAqB3C,SAAgB,kBAAkB,CAChC,MAAW,EACX,oBAA6B;IAE7B,IAAA,eAAM,EAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC3E,IAAI,IAAA,qBAAa,EAAC,oBAAoB,CAAC,EAAE;QACvC,IAAA,eAAM,EACJ,oBAAoB,IAAI,CAAC,IAAI,oBAAoB,GAAG,MAAM,CAAC,MAAM,EACjE,+EAA+E,CAChF,CAAA;KACF;IAED,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,IAAA,gBAAQ,EAAqB,oBAAoB,CAAC,CAAA;IAE5F,OAAO,IAAA,eAAO,EAAC,GAAG,EAAE,CAAC,CAAC;QACpB,aAAa,EAAE,IAAA,mBAAW,EAAC,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC;QAC7E,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YACrC,KAAK;YACL,KAAK;YACL,QAAQ,EAAE,KAAK,KAAK,aAAa;YACjC,MAAM,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC;SACtC,CAAC,CAAC;KACJ,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAA;AACtB,CAAC;AAvBD,gDAuBC"}
@@ -0,0 +1,2 @@
1
+ import { EffectCallback } from 'react';
2
+ export declare function useUpdateEffect(effect: EffectCallback, deps?: React.DependencyList): void;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useUpdateEffect = void 0;
4
+ const react_1 = require("react");
5
+ const use_is_first_render_1 = require("./use-is-first-render");
6
+ function useUpdateEffect(effect, deps) {
7
+ const isFirstRender = (0, use_is_first_render_1.useIsFirstRender)();
8
+ (0, react_1.useEffect)(() => {
9
+ if (!isFirstRender()) {
10
+ return effect();
11
+ }
12
+ }, deps);
13
+ }
14
+ exports.useUpdateEffect = useUpdateEffect;
15
+ //# sourceMappingURL=use-update-effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-update-effect.js","sourceRoot":"","sources":["../../src/use-update-effect.ts"],"names":[],"mappings":";;;AAAA,iCAAiD;AACjD,+DAAwD;AAExD,SAAgB,eAAe,CAC7B,MAAsB,EACtB,IAA2B;IAE3B,MAAM,aAAa,GAAG,IAAA,sCAAgB,GAAE,CAAA;IAExC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,EAAE;YACpB,OAAO,MAAM,EAAE,CAAA;SAChB;IACH,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC;AAXD,0CAWC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-hooks",
3
- "version": "0.1.13",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [