UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

116 lines (112 loc) 3.48 kB
"use client"; const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs'); const require_ref = require('../../utils/ref.cjs'); const require_utils_index = require('../../utils/index.cjs'); let react = require("react"); react = require_rolldown_runtime.__toESM(react); //#region src/hooks/use-counter/index.ts const parse = (value) => parseFloat(value.toString().replace(/[^\w.-]+/g, "")); const getCountDecimal = (value, step) => Math.max((0, require_utils_index.utils_exports.countDecimal)(step), (0, require_utils_index.utils_exports.countDecimal)(value)); const casting = (value, step, precision) => { value = parse(value); return !Number.isNaN(value) ? (0, require_utils_index.utils_exports.toPrecision)(value, precision ?? getCountDecimal(value, step)) : void 0; }; /** * `useCounter` is a custom hook that returns the current counter value. * * @see https://yamada-ui.com/docs/hooks/use-counter */ const useCounter = ({ keepWithinRange = true, max: maxValue = Number.MAX_SAFE_INTEGER, min: minValue = Number.MIN_SAFE_INTEGER, step: stepProp = 1,...props } = {}) => { const onChange = require_ref.useCallbackRef(props.onChange); const [defaultValue, setValue] = (0, react.useState)(() => { if (props.defaultValue == null) return ""; return casting(props.defaultValue, stepProp, props.precision) ?? ""; }); const isControlled = typeof props.value !== "undefined"; const value = isControlled ? props.value : defaultValue; const countDecimal$1 = getCountDecimal(parse(value), stepProp); const precision = props.precision ?? countDecimal$1; const update = (0, react.useCallback)((next) => { if (next === value) return; if (!isControlled) setValue(next.toString()); onChange(next.toString(), parse(next)); }, [ onChange, isControlled, value ]); const clamp = (0, react.useCallback)((value$1) => { let nextValue = value$1; if (keepWithinRange) nextValue = (0, require_utils_index.utils_exports.clampNumber)(nextValue, minValue, maxValue); return (0, require_utils_index.utils_exports.toPrecision)(nextValue, precision); }, [ precision, keepWithinRange, maxValue, minValue ]); const increment = (0, react.useCallback)((step = stepProp) => { let next; if (value === "") next = parse(step); else next = parse(value) + step; next = clamp(next); update(next); }, [ clamp, stepProp, update, value ]); const decrement = (0, react.useCallback)((step = stepProp) => { let next; if (value === "") next = parse(-step); else next = parse(value) - step; next = clamp(next); update(next); }, [ clamp, stepProp, update, value ]); const reset = (0, react.useCallback)(() => { let next; if (props.defaultValue == null) next = ""; else next = casting(props.defaultValue, stepProp, props.precision) ?? minValue; update(next); }, [ props.defaultValue, props.precision, stepProp, update, minValue ]); const cast = (0, react.useCallback)((value$1) => { update(casting(value$1, stepProp, precision) ?? minValue); }, [ precision, stepProp, update, minValue ]); const valueAsNumber = parse(value); return { cast, clamp, decrement, increment, max: valueAsNumber === maxValue, min: valueAsNumber === minValue, out: valueAsNumber < minValue || maxValue < valueAsNumber, precision, reset, setValue, step: stepProp, update, value, valueAsNumber }; }; //#endregion exports.useCounter = useCounter; //# sourceMappingURL=index.cjs.map