MCPcopy
hub / github.com/autoNumeric/autoNumeric / _setValueParts

Method _setValueParts

src/AutoNumeric.js:8836–8901  ·  view source on GitHub ↗

* Set the formatted element value as well as the `rawValue`. * This returns `true` if the element and raw value have been modified, `false` otherwise. * This method also adjust the caret position according to the `leadingZero` option and the normalized value. //TODO What about the cursor *

(left, right, isPaste = false)

Source from the content-addressed store, hash-verified

8834 * @private
8835 */
8836 _setValueParts(left, right, isPaste = false) {
8837 const [normalizedLeft, normalizedRight, normalizedNewValue] = this._normalizeParts(left, right);
8838 const [minTest, maxTest] = AutoNumeric._checkIfInRangeWithOverrideOption(normalizedNewValue, this.settings);
8839
8840 if (minTest && maxTest) {
8841 // First, set the raw value
8842 const roundedRawValue = AutoNumeric._truncateDecimalPlaces(normalizedNewValue, this.settings, isPaste, this.settings.decimalPlacesRawValue);
8843 const testValue = roundedRawValue.replace(this.settings.decimalCharacter, '.');
8844
8845 if (testValue === '' || testValue === this.settings.negativeSignCharacter) {
8846 let valueToSetOnEmpty;
8847 switch (this.settings.emptyInputBehavior) {
8848 case AutoNumeric.options.emptyInputBehavior.focus:
8849 case AutoNumeric.options.emptyInputBehavior.press:
8850 case AutoNumeric.options.emptyInputBehavior.always:
8851 valueToSetOnEmpty = '';
8852 break;
8853 case AutoNumeric.options.emptyInputBehavior.min:
8854 valueToSetOnEmpty = this.settings.minimumValue;
8855 break;
8856 case AutoNumeric.options.emptyInputBehavior.max:
8857 valueToSetOnEmpty = this.settings.maximumValue;
8858 break;
8859 case AutoNumeric.options.emptyInputBehavior.zero:
8860 valueToSetOnEmpty = '0';
8861 break;
8862 case AutoNumeric.options.emptyInputBehavior.null:
8863 valueToSetOnEmpty = null;
8864 break;
8865 // When `emptyInputBehavior` is a number or a string representing a number
8866 default:
8867 valueToSetOnEmpty = this.settings.emptyInputBehavior;
8868 }
8869
8870 this._setRawValue(valueToSetOnEmpty);
8871 } else {
8872 this._setRawValue(this._trimLeadingAndTrailingZeros(testValue));
8873 }
8874
8875 // Then set the formatted value
8876 const roundedValueToShow = AutoNumeric._truncateDecimalPlaces(normalizedNewValue, this.settings, isPaste, this.settings.decimalPlacesShownOnFocus);
8877 let position = normalizedLeft.length;
8878 if (position > roundedValueToShow.length) {
8879 position = roundedValueToShow.length;
8880 }
8881
8882 // Make sure when the user enter a '0' on the far left with a leading zero option set to 'deny', that the caret does not move since the input is dropped (fix issue #283)
8883 if (position === 1 && normalizedLeft === '0' && this.settings.leadingZero === AutoNumeric.options.leadingZero.deny) {
8884 // If the user enter `0`, then the caret is put on the right side of it (Fix issue #299)
8885 if (normalizedRight === '' || normalizedLeft === '0' && normalizedRight !== '') {
8886 position = 1;
8887 } else {
8888 position = 0;
8889 }
8890 }
8891
8892 this._setElementValue(roundedValueToShow, false);
8893 this._setCaretPosition(position);

Callers 2

Calls 8

_normalizePartsMethod · 0.95
_setRawValueMethod · 0.95
_setElementValueMethod · 0.95
_setCaretPositionMethod · 0.95
_triggerRangeEventsMethod · 0.95

Tested by

no test coverage detected