feat: min/max
This commit is contained in:
parent
fad35e4c2f
commit
311c5879c4
|
@ -7,16 +7,24 @@ import {
|
||||||
|
|
||||||
const Number = ({
|
const Number = ({
|
||||||
integer,
|
integer,
|
||||||
|
max,
|
||||||
|
min,
|
||||||
onChange,
|
onChange,
|
||||||
step,
|
step,
|
||||||
value,
|
value,
|
||||||
}) => {
|
}) => {
|
||||||
const inputRef = useRef(null);
|
const inputRef = useRef(null);
|
||||||
const parser = integer ? (v) => parseInt(v, 10) : parseFloat;
|
const parser = integer ? (v) => parseInt(v, 10) : parseFloat;
|
||||||
|
const changeClamped = (event, change) => {
|
||||||
|
const clamped = Math.min(max, Math.max(min, change));
|
||||||
|
if (value !== clamped) {
|
||||||
|
onChange(event, clamped);
|
||||||
|
}
|
||||||
|
};
|
||||||
const increment = (event, factor) => {
|
const increment = (event, factor) => {
|
||||||
const value = parser(inputRef.current.value);
|
const value = parser(inputRef.current.value);
|
||||||
const incrementBy = event.shiftKey ? (step / 10) : step;
|
const incrementBy = event.shiftKey ? (step / 10) : step;
|
||||||
onChange(event, parser(parser(value + (factor * incrementBy)).toPrecision(12)), event);
|
changeClamped(event, parser(parser(value + (factor * incrementBy)).toPrecision(12)));
|
||||||
};
|
};
|
||||||
const onWheel = (event) => {
|
const onWheel = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
@ -46,7 +54,7 @@ const Number = ({
|
||||||
<input
|
<input
|
||||||
className="number__input"
|
className="number__input"
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
onChange(event, parser(event.target.value));
|
changeClamped(event, parser(event.target.value));
|
||||||
}}
|
}}
|
||||||
// onWheel={onWheel}
|
// onWheel={onWheel}
|
||||||
ref={inputRef}
|
ref={inputRef}
|
||||||
|
@ -79,6 +87,8 @@ const Number = ({
|
||||||
|
|
||||||
Number.defaultProps = {
|
Number.defaultProps = {
|
||||||
integer: false,
|
integer: false,
|
||||||
|
max: Infinity,
|
||||||
|
min: -Infinity,
|
||||||
onChange: null,
|
onChange: null,
|
||||||
step: 1,
|
step: 1,
|
||||||
};
|
};
|
||||||
|
@ -87,6 +97,8 @@ Number.displayName = 'Number';
|
||||||
|
|
||||||
Number.propTypes = {
|
Number.propTypes = {
|
||||||
integer: PropTypes.bool,
|
integer: PropTypes.bool,
|
||||||
|
max: PropTypes.number,
|
||||||
|
min: PropTypes.number,
|
||||||
onChange: PropTypes.func,
|
onChange: PropTypes.func,
|
||||||
step: PropTypes.number,
|
step: PropTypes.number,
|
||||||
value: PropTypes.number.isRequired,
|
value: PropTypes.number.isRequired,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user