MCPcopy
hub / github.com/autoNumeric/autoNumeric / _wheelAndUpDownActions

Method _wheelAndUpDownActions

src/AutoNumeric.js:7571–7638  ·  view source on GitHub ↗

* Helper function that DRY the similar behaviors of the mousewheel and up/down arrow keys, which increment/decrement the element value, either by a fixed value, or using the 'progressive' heuristic * * @param {WheelEvent|KeyboardEvent} e The `wheel` or keyboard event * @param {boolean

(e, isUp, isDown, step)

Source from the content-addressed store, hash-verified

7569 * @private
7570 */
7571 _wheelAndUpDownActions(e, isUp, isDown, step) {
7572 // 0) First, save the caret position so we can set it back once the value has been changed
7573 const selectionStart = e.target.selectionStart || 0;
7574 const selectionEnd = e.target.selectionEnd || 0;
7575
7576 // 1) Get the unformatted value
7577 const currentUnformattedValue = this.rawValue;
7578
7579 let result;
7580 if (AutoNumericHelper.isUndefinedOrNullOrEmpty(currentUnformattedValue)) {
7581 // If by default the input is empty, start at '0'
7582 if (this.settings.minimumValue > 0 || this.settings.maximumValue < 0) {
7583 // or if '0' is not between min and max value, 'minimumValue' if the user does a wheelup, 'maximumValue' if the user does a wheeldown
7584 if (isUp) {
7585 result = this.settings.minimumValue;
7586 } else {
7587 result = this.settings.maximumValue;
7588 }
7589 } else {
7590 result = 0;
7591 }
7592 } else {
7593 result = currentUnformattedValue;
7594 }
7595
7596 result = +result; // Typecast to a number needed for the following addition/subtraction
7597
7598 // 2) Increment/Decrement the value
7599 // But first, choose the increment/decrement method ; fixed or progressive
7600 if (AutoNumericHelper.isNumber(step)) {
7601 const stepToUse = +step; // Typecast to a number needed for the following addition/subtraction
7602 // Fixed method
7603 // This is the simplest method, where a fixed offset in added/subtracted from the current value
7604 if (isUp) { // Increment
7605 result += stepToUse;
7606 } else if (isDown) { // Decrement
7607 result -= stepToUse;
7608 }
7609 } else {
7610 // Progressive method
7611 // For this method, we calculate an offset that is in relation to the size of the current number (using only the integer part size).
7612 // The bigger the number, the bigger the offset (usually the number count in the integer part minus 3, except for small numbers where a different behavior is better for the user experience).
7613 //TODO Known limitation : The progressive method does not play well with numbers between 0 and 1 where to modify the decimal places the rawValue first has to go from '1' to '0'
7614 if (isUp) { // Increment
7615 result = AutoNumericHelper.addAndRoundToNearestAuto(result, this.settings.decimalPlacesRawValue);
7616 } else if (isDown) { // Decrement
7617 result = AutoNumericHelper.subtractAndRoundToNearestAuto(result, this.settings.decimalPlacesRawValue);
7618 }
7619 }
7620
7621 // 3) Set the new value so it gets formatted
7622 // First clamp the result if needed
7623 result = AutoNumericHelper.clampToRangeLimits(result, this.settings);
7624 if (result !== +currentUnformattedValue) {
7625 // Only 'set' the value if it has changed. For instance 'set' should not happen if the user hits a limit and continue to try to go past it since we clamp the value.
7626 this.set(result);
7627
7628 // Since we changed the input value, we send a native `input` event

Callers 2

upDownArrowActionMethod · 0.95
wheelActionMethod · 0.95

Calls 8

setMethod · 0.95
_triggerEventMethod · 0.95
_setSelectionMethod · 0.95
isNumberMethod · 0.80
clampToRangeLimitsMethod · 0.80

Tested by

no test coverage detected