source: imaps-frontend/node_modules/react-use/esm/useCounter.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[d565449]1import { useMemo } from 'react';
2import useGetSet from './useGetSet';
3import { resolveHookState } from './misc/hookState';
4export default function useCounter(initialValue, max, min) {
5 if (initialValue === void 0) { initialValue = 0; }
6 if (max === void 0) { max = null; }
7 if (min === void 0) { min = null; }
8 var init = resolveHookState(initialValue);
9 typeof init !== 'number' &&
10 console.error('initialValue has to be a number, got ' + typeof initialValue);
11 if (typeof min === 'number') {
12 init = Math.max(init, min);
13 }
14 else if (min !== null) {
15 console.error('min has to be a number, got ' + typeof min);
16 }
17 if (typeof max === 'number') {
18 init = Math.min(init, max);
19 }
20 else if (max !== null) {
21 console.error('max has to be a number, got ' + typeof max);
22 }
23 var _a = useGetSet(init), get = _a[0], setInternal = _a[1];
24 return [
25 get(),
26 useMemo(function () {
27 var set = function (newState) {
28 var prevState = get();
29 var rState = resolveHookState(newState, prevState);
30 if (prevState !== rState) {
31 if (typeof min === 'number') {
32 rState = Math.max(rState, min);
33 }
34 if (typeof max === 'number') {
35 rState = Math.min(rState, max);
36 }
37 prevState !== rState && setInternal(rState);
38 }
39 };
40 return {
41 get: get,
42 set: set,
43 inc: function (delta) {
44 if (delta === void 0) { delta = 1; }
45 var rDelta = resolveHookState(delta, get());
46 if (typeof rDelta !== 'number') {
47 console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
48 }
49 set(function (num) { return num + rDelta; });
50 },
51 dec: function (delta) {
52 if (delta === void 0) { delta = 1; }
53 var rDelta = resolveHookState(delta, get());
54 if (typeof rDelta !== 'number') {
55 console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
56 }
57 set(function (num) { return num - rDelta; });
58 },
59 reset: function (value) {
60 if (value === void 0) { value = init; }
61 var rValue = resolveHookState(value, get());
62 if (typeof rValue !== 'number') {
63 console.error('value has to be a number or function returning a number, got ' + typeof rValue);
64 }
65 // eslint-disable-next-line react-hooks/exhaustive-deps
66 init = rValue;
67 set(rValue);
68 },
69 };
70 }, [init, min, max]),
71 ];
72}
Note: See TracBrowser for help on using the repository browser.