UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

85 lines (77 loc) 1.94 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["onChange", "defaultValue"]; import * as React from "react"; import StepperStateless from "./StepperStateless"; import validateIncrement from "../utils/validateIncrement"; import validateDecrement from "../utils/validateDecrement"; import useStateWithCallback from "../hooks/useStateWithCallback"; const Stepper = _ref => { let { onChange, defaultValue = 0 } = _ref, props = _objectWithoutProperties(_ref, _excluded); const [value, setValue] = useStateWithCallback(defaultValue, onChange); const incrementCounter = () => { const { maxValue = Number.POSITIVE_INFINITY, step = 1 } = props; setValue(validateIncrement({ value, maxValue, step })); }; const decrementCounter = () => { const { minValue = Number.NEGATIVE_INFINITY, step = 1 } = props; setValue(validateDecrement({ value, minValue, step })); }; const handleKeyDown = ev => { if (ev.keyCode === 40) { ev.preventDefault(); decrementCounter(); } if (ev.keyCode === 38) { ev.preventDefault(); incrementCounter(); } }; const { onBlur, onFocus, disabled, name, dataTest, minValue, maxValue, titleIncrement, titleDecrement, active } = props; return /*#__PURE__*/React.createElement(StepperStateless, { onKeyDown: handleKeyDown, onIncrement: incrementCounter, onDecrement: decrementCounter, minValue: minValue, maxValue: maxValue, onFocus: onFocus, onBlur: onBlur, active: active, disabled: disabled, dataTest: dataTest, value: value, name: name, titleIncrement: titleIncrement, titleDecrement: titleDecrement }); }; export default Stepper; export { default as StepperStateless } from "./StepperStateless";