fix: number validation and shift inc

This commit is contained in:
cha0s 2020-06-23 20:41:43 -05:00
parent 04d65c0a6b
commit 009b298fdb

View File

@ -14,6 +14,7 @@ const Number = ({
options,
value,
}) => {
const [whole, fraction = 0] = `${value}`.split('.');
const inputRef = useRef(null);
return (
<span className="number">
@ -32,24 +33,30 @@ const Number = ({
onChange={(event) => {
const {value: number} = event.target;
// eslint-disable-next-line no-restricted-globals
if (!isNaN(number)) {
onChange('' === number ? 0 : parseInt(number, 10), event);
if (number.match(/^(?:[0-9]+)?\.?(?:[0-9]+)?$/)) {
onChange('' === number ? 0 : parseFloat(number), event);
}
}}
// onWheel={(e) => (e.preventDefault(), false)}
ref={inputRef}
type="text"
value={value}
value={`${whole}.${fraction}`}
/>
<div className="controls">
<button
onClick={(event) => onChange(1 + parseInt(inputRef.current.value, 10), event)}
onClick={(event) => {
const incrementBy = event.shiftKey ? 0.1 : 1;
onChange(parseFloat(inputRef.current.value) + incrementBy, event);
}}
type="button"
>
+
</button>
<button
onClick={(event) => onChange(-1 + parseInt(inputRef.current.value, 10), event)}
onClick={(event) => {
const incrementBy = event.shiftKey ? 0.1 : 1;
onChange(parseFloat(inputRef.current.value) - incrementBy, event);
}}
type="button"
>
-