73 lines
3.0 KiB
JavaScript
73 lines
3.0 KiB
JavaScript
import { useMemo } from 'react';
|
|
import useGetSet from './useGetSet';
|
|
import { resolveHookState } from './misc/hookState';
|
|
export default function useCounter(initialValue, max, min) {
|
|
if (initialValue === void 0) { initialValue = 0; }
|
|
if (max === void 0) { max = null; }
|
|
if (min === void 0) { min = null; }
|
|
var init = resolveHookState(initialValue);
|
|
typeof init !== 'number' &&
|
|
console.error('initialValue has to be a number, got ' + typeof initialValue);
|
|
if (typeof min === 'number') {
|
|
init = Math.max(init, min);
|
|
}
|
|
else if (min !== null) {
|
|
console.error('min has to be a number, got ' + typeof min);
|
|
}
|
|
if (typeof max === 'number') {
|
|
init = Math.min(init, max);
|
|
}
|
|
else if (max !== null) {
|
|
console.error('max has to be a number, got ' + typeof max);
|
|
}
|
|
var _a = useGetSet(init), get = _a[0], setInternal = _a[1];
|
|
return [
|
|
get(),
|
|
useMemo(function () {
|
|
var set = function (newState) {
|
|
var prevState = get();
|
|
var rState = resolveHookState(newState, prevState);
|
|
if (prevState !== rState) {
|
|
if (typeof min === 'number') {
|
|
rState = Math.max(rState, min);
|
|
}
|
|
if (typeof max === 'number') {
|
|
rState = Math.min(rState, max);
|
|
}
|
|
prevState !== rState && setInternal(rState);
|
|
}
|
|
};
|
|
return {
|
|
get: get,
|
|
set: set,
|
|
inc: function (delta) {
|
|
if (delta === void 0) { delta = 1; }
|
|
var rDelta = resolveHookState(delta, get());
|
|
if (typeof rDelta !== 'number') {
|
|
console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
|
|
}
|
|
set(function (num) { return num + rDelta; });
|
|
},
|
|
dec: function (delta) {
|
|
if (delta === void 0) { delta = 1; }
|
|
var rDelta = resolveHookState(delta, get());
|
|
if (typeof rDelta !== 'number') {
|
|
console.error('delta has to be a number or function returning a number, got ' + typeof rDelta);
|
|
}
|
|
set(function (num) { return num - rDelta; });
|
|
},
|
|
reset: function (value) {
|
|
if (value === void 0) { value = init; }
|
|
var rValue = resolveHookState(value, get());
|
|
if (typeof rValue !== 'number') {
|
|
console.error('value has to be a number or function returning a number, got ' + typeof rValue);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
init = rValue;
|
|
set(rValue);
|
|
},
|
|
};
|
|
}, [init, min, max]),
|
|
];
|
|
}
|