@chakra-ui/counter
Advanced tools
Comparing version 1.2.0 to 1.2.1
# Change Log | ||
## 1.2.1 | ||
### Patch Changes | ||
- [#5075](https://github.com/chakra-ui/chakra-ui/pull/5075) | ||
[`b28142946`](https://github.com/chakra-ui/chakra-ui/commit/b281429462a099b7fd7f9352e837cd28d1a2da0e) | ||
Thanks [@cschroeter](https://github.com/cschroeter)! - Update babel config to | ||
transpile soruces for older browsers. This fixes issues with CRA and | ||
Storybook. | ||
- Updated dependencies | ||
[[`b28142946`](https://github.com/chakra-ui/chakra-ui/commit/b281429462a099b7fd7f9352e837cd28d1a2da0e)]: | ||
- @chakra-ui/hooks@1.7.1 | ||
- @chakra-ui/utils@1.9.1 | ||
## 1.2.0 | ||
@@ -4,0 +18,0 @@ |
@@ -9,18 +9,30 @@ 'use strict'; | ||
function useCounter(props = {}) { | ||
const { | ||
onChange, | ||
precision: precisionProp, | ||
defaultValue, | ||
value: valueProp, | ||
step: stepProp = 1, | ||
min = utils.minSafeInteger, | ||
max = utils.maxSafeInteger, | ||
keepWithinRange = true | ||
} = props; | ||
const onChangeProp = hooks.useCallbackRef(onChange); | ||
const [valueState, setValue] = react.useState(() => { | ||
function useCounter(props) { | ||
if (props === void 0) { | ||
props = {}; | ||
} | ||
var _props = props, | ||
onChange = _props.onChange, | ||
precisionProp = _props.precision, | ||
defaultValue = _props.defaultValue, | ||
valueProp = _props.value, | ||
_props$step = _props.step, | ||
stepProp = _props$step === void 0 ? 1 : _props$step, | ||
_props$min = _props.min, | ||
min = _props$min === void 0 ? utils.minSafeInteger : _props$min, | ||
_props$max = _props.max, | ||
max = _props$max === void 0 ? utils.maxSafeInteger : _props$max, | ||
_props$keepWithinRang = _props.keepWithinRange, | ||
keepWithinRange = _props$keepWithinRang === void 0 ? true : _props$keepWithinRang; | ||
var onChangeProp = hooks.useCallbackRef(onChange); | ||
var _useState = react.useState(function () { | ||
var _cast; | ||
if (defaultValue == null) return ""; | ||
return cast(defaultValue, stepProp, precisionProp) ?? ""; | ||
}); | ||
return (_cast = cast(defaultValue, stepProp, precisionProp)) != null ? _cast : ""; | ||
}), | ||
valueState = _useState[0], | ||
setValue = _useState[1]; | ||
/** | ||
@@ -31,6 +43,10 @@ * Because the component that consumes this hook can be controlled or uncontrolled | ||
const [isControlled, value] = hooks.useControllableProp(valueProp, valueState); | ||
const decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
const precision = precisionProp ?? decimalPlaces; | ||
const update = react.useCallback(next => { | ||
var _useControllableProp = hooks.useControllableProp(valueProp, valueState), | ||
isControlled = _useControllableProp[0], | ||
value = _useControllableProp[1]; | ||
var decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
var precision = precisionProp != null ? precisionProp : decimalPlaces; | ||
var update = react.useCallback(function (next) { | ||
if (next === value) return; | ||
@@ -42,7 +58,7 @@ | ||
onChangeProp?.(next.toString(), parse(next)); | ||
onChangeProp == null ? void 0 : onChangeProp(next.toString(), parse(next)); | ||
}, [onChangeProp, isControlled, value]); // Function to clamp the value and round it to the precision | ||
const clamp = react.useCallback(value => { | ||
let nextValue = value; | ||
var clamp = react.useCallback(function (value) { | ||
var nextValue = value; | ||
@@ -55,4 +71,8 @@ if (keepWithinRange) { | ||
}, [precision, keepWithinRange, max, min]); | ||
const increment = react.useCallback((step = stepProp) => { | ||
let next; | ||
var increment = react.useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; | ||
/** | ||
@@ -76,5 +96,9 @@ * Let's follow the native browser behavior for | ||
}, [clamp, stepProp, update, value]); | ||
const decrement = react.useCallback((step = stepProp) => { | ||
let next; // Same thing here. We'll follow native implementation | ||
var decrement = react.useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; // Same thing here. We'll follow native implementation | ||
if (value === "") { | ||
@@ -89,4 +113,4 @@ next = parse(-step); | ||
}, [clamp, stepProp, update, value]); | ||
const reset = react.useCallback(() => { | ||
let next; | ||
var reset = react.useCallback(function () { | ||
var next; | ||
@@ -96,3 +120,5 @@ if (defaultValue == null) { | ||
} else { | ||
next = cast(defaultValue, stepProp, precisionProp) ?? min; | ||
var _cast2; | ||
next = (_cast2 = cast(defaultValue, stepProp, precisionProp)) != null ? _cast2 : min; | ||
} | ||
@@ -102,7 +128,9 @@ | ||
}, [defaultValue, precisionProp, stepProp, update, min]); | ||
const castValue = react.useCallback(value => { | ||
const nextValue = cast(value, stepProp, precision) ?? min; | ||
var castValue = react.useCallback(function (value) { | ||
var _cast3; | ||
var nextValue = (_cast3 = cast(value, stepProp, precision)) != null ? _cast3 : min; | ||
update(nextValue); | ||
}, [precision, stepProp, update, min]); | ||
const valueAsNumber = parse(value); | ||
var valueAsNumber = parse(value); | ||
/** | ||
@@ -112,19 +140,19 @@ * Common range checks | ||
const isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
const isAtMax = valueAsNumber === max; | ||
const isAtMin = valueAsNumber === min; | ||
var isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
var isAtMax = valueAsNumber === max; | ||
var isAtMin = valueAsNumber === min; | ||
return { | ||
isOutOfRange, | ||
isAtMax, | ||
isAtMin, | ||
precision, | ||
value, | ||
valueAsNumber, | ||
update, | ||
reset, | ||
increment, | ||
decrement, | ||
clamp, | ||
isOutOfRange: isOutOfRange, | ||
isAtMax: isAtMax, | ||
isAtMin: isAtMin, | ||
precision: precision, | ||
value: value, | ||
valueAsNumber: valueAsNumber, | ||
update: update, | ||
reset: reset, | ||
increment: increment, | ||
decrement: decrement, | ||
clamp: clamp, | ||
cast: castValue, | ||
setValue | ||
setValue: setValue | ||
}; | ||
@@ -142,8 +170,8 @@ } | ||
function cast(value, step, precision) { | ||
const parsedValue = parse(value); | ||
var parsedValue = parse(value); | ||
if (Number.isNaN(parsedValue)) return undefined; | ||
const decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return utils.toPrecision(parsedValue, precision ?? decimalPlaces); | ||
var decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return utils.toPrecision(parsedValue, precision != null ? precision : decimalPlaces); | ||
} | ||
exports.useCounter = useCounter; |
@@ -9,18 +9,30 @@ 'use strict'; | ||
function useCounter(props = {}) { | ||
const { | ||
onChange, | ||
precision: precisionProp, | ||
defaultValue, | ||
value: valueProp, | ||
step: stepProp = 1, | ||
min = utils.minSafeInteger, | ||
max = utils.maxSafeInteger, | ||
keepWithinRange = true | ||
} = props; | ||
const onChangeProp = hooks.useCallbackRef(onChange); | ||
const [valueState, setValue] = react.useState(() => { | ||
function useCounter(props) { | ||
if (props === void 0) { | ||
props = {}; | ||
} | ||
var _props = props, | ||
onChange = _props.onChange, | ||
precisionProp = _props.precision, | ||
defaultValue = _props.defaultValue, | ||
valueProp = _props.value, | ||
_props$step = _props.step, | ||
stepProp = _props$step === void 0 ? 1 : _props$step, | ||
_props$min = _props.min, | ||
min = _props$min === void 0 ? utils.minSafeInteger : _props$min, | ||
_props$max = _props.max, | ||
max = _props$max === void 0 ? utils.maxSafeInteger : _props$max, | ||
_props$keepWithinRang = _props.keepWithinRange, | ||
keepWithinRange = _props$keepWithinRang === void 0 ? true : _props$keepWithinRang; | ||
var onChangeProp = hooks.useCallbackRef(onChange); | ||
var _useState = react.useState(function () { | ||
var _cast; | ||
if (defaultValue == null) return ""; | ||
return cast(defaultValue, stepProp, precisionProp) ?? ""; | ||
}); | ||
return (_cast = cast(defaultValue, stepProp, precisionProp)) != null ? _cast : ""; | ||
}), | ||
valueState = _useState[0], | ||
setValue = _useState[1]; | ||
/** | ||
@@ -31,6 +43,10 @@ * Because the component that consumes this hook can be controlled or uncontrolled | ||
const [isControlled, value] = hooks.useControllableProp(valueProp, valueState); | ||
const decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
const precision = precisionProp ?? decimalPlaces; | ||
const update = react.useCallback(next => { | ||
var _useControllableProp = hooks.useControllableProp(valueProp, valueState), | ||
isControlled = _useControllableProp[0], | ||
value = _useControllableProp[1]; | ||
var decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
var precision = precisionProp != null ? precisionProp : decimalPlaces; | ||
var update = react.useCallback(function (next) { | ||
if (next === value) return; | ||
@@ -42,7 +58,7 @@ | ||
onChangeProp?.(next.toString(), parse(next)); | ||
onChangeProp == null ? void 0 : onChangeProp(next.toString(), parse(next)); | ||
}, [onChangeProp, isControlled, value]); // Function to clamp the value and round it to the precision | ||
const clamp = react.useCallback(value => { | ||
let nextValue = value; | ||
var clamp = react.useCallback(function (value) { | ||
var nextValue = value; | ||
@@ -55,4 +71,8 @@ if (keepWithinRange) { | ||
}, [precision, keepWithinRange, max, min]); | ||
const increment = react.useCallback((step = stepProp) => { | ||
let next; | ||
var increment = react.useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; | ||
/** | ||
@@ -76,5 +96,9 @@ * Let's follow the native browser behavior for | ||
}, [clamp, stepProp, update, value]); | ||
const decrement = react.useCallback((step = stepProp) => { | ||
let next; // Same thing here. We'll follow native implementation | ||
var decrement = react.useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; // Same thing here. We'll follow native implementation | ||
if (value === "") { | ||
@@ -89,4 +113,4 @@ next = parse(-step); | ||
}, [clamp, stepProp, update, value]); | ||
const reset = react.useCallback(() => { | ||
let next; | ||
var reset = react.useCallback(function () { | ||
var next; | ||
@@ -96,3 +120,5 @@ if (defaultValue == null) { | ||
} else { | ||
next = cast(defaultValue, stepProp, precisionProp) ?? min; | ||
var _cast2; | ||
next = (_cast2 = cast(defaultValue, stepProp, precisionProp)) != null ? _cast2 : min; | ||
} | ||
@@ -102,7 +128,9 @@ | ||
}, [defaultValue, precisionProp, stepProp, update, min]); | ||
const castValue = react.useCallback(value => { | ||
const nextValue = cast(value, stepProp, precision) ?? min; | ||
var castValue = react.useCallback(function (value) { | ||
var _cast3; | ||
var nextValue = (_cast3 = cast(value, stepProp, precision)) != null ? _cast3 : min; | ||
update(nextValue); | ||
}, [precision, stepProp, update, min]); | ||
const valueAsNumber = parse(value); | ||
var valueAsNumber = parse(value); | ||
/** | ||
@@ -112,19 +140,19 @@ * Common range checks | ||
const isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
const isAtMax = valueAsNumber === max; | ||
const isAtMin = valueAsNumber === min; | ||
var isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
var isAtMax = valueAsNumber === max; | ||
var isAtMin = valueAsNumber === min; | ||
return { | ||
isOutOfRange, | ||
isAtMax, | ||
isAtMin, | ||
precision, | ||
value, | ||
valueAsNumber, | ||
update, | ||
reset, | ||
increment, | ||
decrement, | ||
clamp, | ||
isOutOfRange: isOutOfRange, | ||
isAtMax: isAtMax, | ||
isAtMin: isAtMin, | ||
precision: precision, | ||
value: value, | ||
valueAsNumber: valueAsNumber, | ||
update: update, | ||
reset: reset, | ||
increment: increment, | ||
decrement: decrement, | ||
clamp: clamp, | ||
cast: castValue, | ||
setValue | ||
setValue: setValue | ||
}; | ||
@@ -142,8 +170,8 @@ } | ||
function cast(value, step, precision) { | ||
const parsedValue = parse(value); | ||
var parsedValue = parse(value); | ||
if (Number.isNaN(parsedValue)) return undefined; | ||
const decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return utils.toPrecision(parsedValue, precision ?? decimalPlaces); | ||
var decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return utils.toPrecision(parsedValue, precision != null ? precision : decimalPlaces); | ||
} | ||
exports.useCounter = useCounter; |
@@ -5,18 +5,30 @@ import { useCallbackRef, useControllableProp } from '@chakra-ui/hooks'; | ||
function useCounter(props = {}) { | ||
const { | ||
onChange, | ||
precision: precisionProp, | ||
defaultValue, | ||
value: valueProp, | ||
step: stepProp = 1, | ||
min = minSafeInteger, | ||
max = maxSafeInteger, | ||
keepWithinRange = true | ||
} = props; | ||
const onChangeProp = useCallbackRef(onChange); | ||
const [valueState, setValue] = useState(() => { | ||
function useCounter(props) { | ||
if (props === void 0) { | ||
props = {}; | ||
} | ||
var _props = props, | ||
onChange = _props.onChange, | ||
precisionProp = _props.precision, | ||
defaultValue = _props.defaultValue, | ||
valueProp = _props.value, | ||
_props$step = _props.step, | ||
stepProp = _props$step === void 0 ? 1 : _props$step, | ||
_props$min = _props.min, | ||
min = _props$min === void 0 ? minSafeInteger : _props$min, | ||
_props$max = _props.max, | ||
max = _props$max === void 0 ? maxSafeInteger : _props$max, | ||
_props$keepWithinRang = _props.keepWithinRange, | ||
keepWithinRange = _props$keepWithinRang === void 0 ? true : _props$keepWithinRang; | ||
var onChangeProp = useCallbackRef(onChange); | ||
var _useState = useState(function () { | ||
var _cast; | ||
if (defaultValue == null) return ""; | ||
return cast(defaultValue, stepProp, precisionProp) ?? ""; | ||
}); | ||
return (_cast = cast(defaultValue, stepProp, precisionProp)) != null ? _cast : ""; | ||
}), | ||
valueState = _useState[0], | ||
setValue = _useState[1]; | ||
/** | ||
@@ -27,6 +39,10 @@ * Because the component that consumes this hook can be controlled or uncontrolled | ||
const [isControlled, value] = useControllableProp(valueProp, valueState); | ||
const decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
const precision = precisionProp ?? decimalPlaces; | ||
const update = useCallback(next => { | ||
var _useControllableProp = useControllableProp(valueProp, valueState), | ||
isControlled = _useControllableProp[0], | ||
value = _useControllableProp[1]; | ||
var decimalPlaces = getDecimalPlaces(parse(value), stepProp); | ||
var precision = precisionProp != null ? precisionProp : decimalPlaces; | ||
var update = useCallback(function (next) { | ||
if (next === value) return; | ||
@@ -38,7 +54,7 @@ | ||
onChangeProp?.(next.toString(), parse(next)); | ||
onChangeProp == null ? void 0 : onChangeProp(next.toString(), parse(next)); | ||
}, [onChangeProp, isControlled, value]); // Function to clamp the value and round it to the precision | ||
const clamp = useCallback(value => { | ||
let nextValue = value; | ||
var clamp = useCallback(function (value) { | ||
var nextValue = value; | ||
@@ -51,4 +67,8 @@ if (keepWithinRange) { | ||
}, [precision, keepWithinRange, max, min]); | ||
const increment = useCallback((step = stepProp) => { | ||
let next; | ||
var increment = useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; | ||
/** | ||
@@ -72,5 +92,9 @@ * Let's follow the native browser behavior for | ||
}, [clamp, stepProp, update, value]); | ||
const decrement = useCallback((step = stepProp) => { | ||
let next; // Same thing here. We'll follow native implementation | ||
var decrement = useCallback(function (step) { | ||
if (step === void 0) { | ||
step = stepProp; | ||
} | ||
var next; // Same thing here. We'll follow native implementation | ||
if (value === "") { | ||
@@ -85,4 +109,4 @@ next = parse(-step); | ||
}, [clamp, stepProp, update, value]); | ||
const reset = useCallback(() => { | ||
let next; | ||
var reset = useCallback(function () { | ||
var next; | ||
@@ -92,3 +116,5 @@ if (defaultValue == null) { | ||
} else { | ||
next = cast(defaultValue, stepProp, precisionProp) ?? min; | ||
var _cast2; | ||
next = (_cast2 = cast(defaultValue, stepProp, precisionProp)) != null ? _cast2 : min; | ||
} | ||
@@ -98,7 +124,9 @@ | ||
}, [defaultValue, precisionProp, stepProp, update, min]); | ||
const castValue = useCallback(value => { | ||
const nextValue = cast(value, stepProp, precision) ?? min; | ||
var castValue = useCallback(function (value) { | ||
var _cast3; | ||
var nextValue = (_cast3 = cast(value, stepProp, precision)) != null ? _cast3 : min; | ||
update(nextValue); | ||
}, [precision, stepProp, update, min]); | ||
const valueAsNumber = parse(value); | ||
var valueAsNumber = parse(value); | ||
/** | ||
@@ -108,19 +136,19 @@ * Common range checks | ||
const isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
const isAtMax = valueAsNumber === max; | ||
const isAtMin = valueAsNumber === min; | ||
var isOutOfRange = valueAsNumber > max || valueAsNumber < min; | ||
var isAtMax = valueAsNumber === max; | ||
var isAtMin = valueAsNumber === min; | ||
return { | ||
isOutOfRange, | ||
isAtMax, | ||
isAtMin, | ||
precision, | ||
value, | ||
valueAsNumber, | ||
update, | ||
reset, | ||
increment, | ||
decrement, | ||
clamp, | ||
isOutOfRange: isOutOfRange, | ||
isAtMax: isAtMax, | ||
isAtMin: isAtMin, | ||
precision: precision, | ||
value: value, | ||
valueAsNumber: valueAsNumber, | ||
update: update, | ||
reset: reset, | ||
increment: increment, | ||
decrement: decrement, | ||
clamp: clamp, | ||
cast: castValue, | ||
setValue | ||
setValue: setValue | ||
}; | ||
@@ -138,8 +166,8 @@ } | ||
function cast(value, step, precision) { | ||
const parsedValue = parse(value); | ||
var parsedValue = parse(value); | ||
if (Number.isNaN(parsedValue)) return undefined; | ||
const decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return toPrecision(parsedValue, precision ?? decimalPlaces); | ||
var decimalPlaces = getDecimalPlaces(parsedValue, step); | ||
return toPrecision(parsedValue, precision != null ? precision : decimalPlaces); | ||
} | ||
export { useCounter }; |
export * from "./use-counter"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -63,1 +63,2 @@ /// <reference types="react" /> | ||
export declare type UseCounterReturn = ReturnType<typeof useCounter>; | ||
//# sourceMappingURL=use-counter.d.ts.map |
{ | ||
"name": "@chakra-ui/counter", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A React hook for managing counters", | ||
@@ -24,3 +24,4 @@ "keywords": [ | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"src" | ||
], | ||
@@ -39,4 +40,4 @@ "publishConfig": { | ||
"dependencies": { | ||
"@chakra-ui/hooks": "1.7.0", | ||
"@chakra-ui/utils": "1.9.0" | ||
"@chakra-ui/hooks": "1.7.1", | ||
"@chakra-ui/utils": "1.9.1" | ||
}, | ||
@@ -43,0 +44,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38591
14
668
+ Added@chakra-ui/hooks@1.7.1(transitive)
+ Added@chakra-ui/react-utils@1.2.1(transitive)
+ Added@chakra-ui/utils@1.9.1(transitive)
- Removed@chakra-ui/hooks@1.7.0(transitive)
- Removed@chakra-ui/react-utils@1.2.0(transitive)
- Removed@chakra-ui/utils@1.9.0(transitive)
Updated@chakra-ui/hooks@1.7.1
Updated@chakra-ui/utils@1.9.1