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, useRef,
} from '@latus/react'; } from '@latus/react';
const Number = ({onChange, step, value}) => { const Number = ({
integer,
onChange,
step,
value,
}) => {
const inputRef = useRef(null); const inputRef = useRef(null);
const parser = integer ? (v) => parseInt(v, 10) : parseFloat;
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
step = parseFloat(step); step = parser(step);
const increment = (event, factor) => { const increment = (event, factor) => {
const value = parseFloat(inputRef.current.value); const value = parser(inputRef.current.value);
const incrementBy = event.shiftKey ? (step / 10) : step; 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 ( return (
<div className="number"> <div className="number">
@ -21,7 +27,7 @@ const Number = ({onChange, step, value}) => {
<input <input
className="number__input" className="number__input"
onChange={(event) => { onChange={(event) => {
onChange(event.target.value); onChange(parser(event.target.value));
}} }}
onWheel={(event) => { onWheel={(event) => {
if (event.deltaY > 0) { if (event.deltaY > 0) {
@ -60,11 +66,13 @@ const Number = ({onChange, step, value}) => {
}; };
Number.defaultProps = { Number.defaultProps = {
integer: false,
onChange: null, onChange: null,
step: 1, step: 1,
}; };
Number.propTypes = { Number.propTypes = {
integer: PropTypes.bool,
onChange: PropTypes.func, onChange: PropTypes.func,
step: PropTypes.number, step: PropTypes.number,
value: PropTypes.number.isRequired, value: PropTypes.number.isRequired,