From 4f7416f1c593e5aab098f88476879e9df7e5309b Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 26 Jan 2021 11:44:52 -0600 Subject: [PATCH] feat: ints --- packages/core/src/components/number/index.jsx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/core/src/components/number/index.jsx b/packages/core/src/components/number/index.jsx index 60bb5bc..0eb395c 100644 --- a/packages/core/src/components/number/index.jsx +++ b/packages/core/src/components/number/index.jsx @@ -6,14 +6,20 @@ import { useRef, } from '@latus/react'; -const Number = ({onChange, step, value}) => { +const Number = ({ + integer, + onChange, + step, + value, +}) => { const inputRef = useRef(null); + const parser = integer ? (v) => parseInt(v, 10) : parseFloat; // eslint-disable-next-line no-param-reassign - step = parseFloat(step); + step = parser(step); const increment = (event, factor) => { - const value = parseFloat(inputRef.current.value); + const value = parser(inputRef.current.value); const incrementBy = event.shiftKey ? (step / 10) : step; - onChange(parseFloat(parseFloat(value + (factor * incrementBy)).toPrecision(12)), event); + onChange(parser(parser(value + (factor * incrementBy)).toPrecision(12)), event); }; return (
@@ -21,7 +27,7 @@ const Number = ({onChange, step, value}) => { { - onChange(event.target.value); + onChange(parser(event.target.value)); }} onWheel={(event) => { if (event.deltaY > 0) { @@ -60,11 +66,13 @@ const Number = ({onChange, step, value}) => { }; Number.defaultProps = { + integer: false, onChange: null, step: 1, }; Number.propTypes = { + integer: PropTypes.bool, onChange: PropTypes.func, step: PropTypes.number, value: PropTypes.number.isRequired,