@rc-component/input-number
Version:
React input-number component
108 lines (101 loc) • 3.88 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/* eslint-disable react/no-unknown-property */
import * as React from 'react';
import cls from 'classnames';
import useMobile from "@rc-component/util/es/hooks/useMobile";
import raf from "@rc-component/util/es/raf";
import SemanticContext from "./SemanticContext";
/**
* When click and hold on a button - the speed of auto changing the value.
*/
const STEP_INTERVAL = 200;
/**
* When click and hold on a button - the delay before auto changing the value.
*/
const STEP_DELAY = 600;
export default function StepHandler({
prefixCls,
upNode,
downNode,
upDisabled,
downDisabled,
onStep
}) {
// ======================== Step ========================
const stepTimeoutRef = React.useRef();
const frameIds = React.useRef([]);
const onStepRef = React.useRef();
onStepRef.current = onStep;
const {
classNames,
styles
} = React.useContext(SemanticContext) || {};
const onStopStep = () => {
clearTimeout(stepTimeoutRef.current);
};
// We will interval update step when hold mouse down
const onStepMouseDown = (e, up) => {
e.preventDefault();
onStopStep();
onStepRef.current(up, 'handler');
// Loop step for interval
function loopStep() {
onStepRef.current(up, 'handler');
stepTimeoutRef.current = setTimeout(loopStep, STEP_INTERVAL);
}
// First time press will wait some time to trigger loop step update
stepTimeoutRef.current = setTimeout(loopStep, STEP_DELAY);
};
React.useEffect(() => () => {
onStopStep();
frameIds.current.forEach(id => raf.cancel(id));
}, []);
// ======================= Render =======================
const isMobile = useMobile();
if (isMobile) {
return null;
}
const handlerClassName = `${prefixCls}-handler`;
const upClassName = cls(handlerClassName, `${handlerClassName}-up`, {
[`${handlerClassName}-up-disabled`]: upDisabled
});
const downClassName = cls(handlerClassName, `${handlerClassName}-down`, {
[`${handlerClassName}-down-disabled`]: downDisabled
});
// fix: https://github.com/ant-design/ant-design/issues/43088
// In Safari, When we fire onmousedown and onmouseup events in quick succession,
// there may be a problem that the onmouseup events are executed first,
// resulting in a disordered program execution.
// So, we need to use requestAnimationFrame to ensure that the onmouseup event is executed after the onmousedown event.
const safeOnStopStep = () => frameIds.current.push(raf(onStopStep));
const sharedHandlerProps = {
unselectable: 'on',
role: 'button',
onMouseUp: safeOnStopStep,
onMouseLeave: safeOnStopStep
};
return /*#__PURE__*/React.createElement("div", {
className: cls(`${handlerClassName}-wrap`, classNames?.actions),
style: styles?.actions
}, /*#__PURE__*/React.createElement("span", _extends({}, sharedHandlerProps, {
onMouseDown: e => {
onStepMouseDown(e, true);
},
"aria-label": "Increase Value",
"aria-disabled": upDisabled,
className: upClassName
}), upNode || /*#__PURE__*/React.createElement("span", {
unselectable: "on",
className: `${prefixCls}-handler-up-inner`
})), /*#__PURE__*/React.createElement("span", _extends({}, sharedHandlerProps, {
onMouseDown: e => {
onStepMouseDown(e, false);
},
"aria-label": "Decrease Value",
"aria-disabled": downDisabled,
className: downClassName
}), downNode || /*#__PURE__*/React.createElement("span", {
unselectable: "on",
className: `${prefixCls}-handler-down-inner`
})));
}