[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var tslib_1 = require("tslib");
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | var useGetSet_1 = tslib_1.__importDefault(require("./useGetSet"));
|
---|
| 6 | var hookState_1 = require("./misc/hookState");
|
---|
| 7 | function useCounter(initialValue, max, min) {
|
---|
| 8 | if (initialValue === void 0) { initialValue = 0; }
|
---|
| 9 | if (max === void 0) { max = null; }
|
---|
| 10 | if (min === void 0) { min = null; }
|
---|
| 11 | var init = hookState_1.resolveHookState(initialValue);
|
---|
| 12 | typeof init !== 'number' &&
|
---|
| 13 | console.error('initialValue has to be a number, got ' + typeof initialValue);
|
---|
| 14 | if (typeof min === 'number') {
|
---|
| 15 | init = Math.max(init, min);
|
---|
| 16 | }
|
---|
| 17 | else if (min !== null) {
|
---|
| 18 | console.error('min has to be a number, got ' + typeof min);
|
---|
| 19 | }
|
---|
| 20 | if (typeof max === 'number') {
|
---|
| 21 | init = Math.min(init, max);
|
---|
| 22 | }
|
---|
| 23 | else if (max !== null) {
|
---|
| 24 | console.error('max has to be a number, got ' + typeof max);
|
---|
| 25 | }
|
---|
| 26 | var _a = useGetSet_1.default(init), get = _a[0], setInternal = _a[1];
|
---|
| 27 | return [
|
---|
| 28 | get(),
|
---|
| 29 | react_1.useMemo(function () {
|
---|
| 30 | var set = function (newState) {
|
---|
| 31 | var prevState = get();
|
---|
| 32 | var rState = hookState_1.resolveHookState(newState, prevState);
|
---|
| 33 | if (prevState !== rState) {
|
---|
| 34 | if (typeof min === 'number') {
|
---|
| 35 | rState = Math.max(rState, min);
|
---|
| 36 | }
|
---|
| 37 | if (typeof max === 'number') {
|
---|
| 38 | rState = Math.min(rState, max);
|
---|
| 39 | }
|
---|
| 40 | prevState !== rState && setInternal(rState);
|
---|
| 41 | }
|
---|
| 42 | };
|
---|
| 43 | return {
|
---|
| 44 | get: get,
|
---|
| 45 | set: set,
|
---|
| 46 | inc: function (delta) {
|
---|
| 47 | if (delta === void 0) { delta = 1; }
|
---|
| 48 | var rDelta = hookState_1.resolveHookState(delta, get());
|
---|
| 49 | if (typeof rDelta !== 'number') {
|
---|
| 50 | console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
|
---|
| 51 | }
|
---|
| 52 | set(function (num) { return num + rDelta; });
|
---|
| 53 | },
|
---|
| 54 | dec: function (delta) {
|
---|
| 55 | if (delta === void 0) { delta = 1; }
|
---|
| 56 | var rDelta = hookState_1.resolveHookState(delta, get());
|
---|
| 57 | if (typeof rDelta !== 'number') {
|
---|
| 58 | console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
|
---|
| 59 | }
|
---|
| 60 | set(function (num) { return num - rDelta; });
|
---|
| 61 | },
|
---|
| 62 | reset: function (value) {
|
---|
| 63 | if (value === void 0) { value = init; }
|
---|
| 64 | var rValue = hookState_1.resolveHookState(value, get());
|
---|
| 65 | if (typeof rValue !== 'number') {
|
---|
| 66 | console.error('value has to be a number or function returning a number, got ' + typeof rValue);
|
---|
| 67 | }
|
---|
| 68 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
---|
| 69 | init = rValue;
|
---|
| 70 | set(rValue);
|
---|
| 71 | },
|
---|
| 72 | };
|
---|
| 73 | }, [init, min, max]),
|
---|
| 74 | ];
|
---|
| 75 | }
|
---|
| 76 | exports.default = useCounter;
|
---|