feat: ints

This commit is contained in:
cha0s 2021-01-26 11:44:52 -06:00
parent 10376dd7a1
commit 4f7416f1c5

View File

@ -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 (
<div className="number">
@ -21,7 +27,7 @@ const Number = ({onChange, step, value}) => {
<input
className="number__input"
onChange={(event) => {
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,