calculateNewValue.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.calculateNewValue = calculateNewValue;
  6. var _selectionRange = require("./selectionRange");
  7. var _getValue2 = require("./getValue");
  8. var _isValidDateValue = require("./isValidDateValue");
  9. var _isValidInputTimeValue = require("./isValidInputTimeValue");
  10. function calculateNewValue(newEntry, element, value = (() => {
  11. var _getValue;
  12. return (_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
  13. /* istanbul ignore next */
  14. '';
  15. })(), selectionRange = (0, _selectionRange.getSelectionRange)(element), deleteContent) {
  16. const selectionStart = selectionRange.selectionStart === null ? value.length : selectionRange.selectionStart;
  17. const selectionEnd = selectionRange.selectionEnd === null ? value.length : selectionRange.selectionEnd;
  18. const prologEnd = Math.max(0, selectionStart === selectionEnd && deleteContent === 'backward' ? selectionStart - 1 : selectionStart);
  19. const prolog = value.substring(0, prologEnd);
  20. const epilogStart = Math.min(value.length, selectionStart === selectionEnd && deleteContent === 'forward' ? selectionEnd + 1 : selectionEnd);
  21. const epilog = value.substring(epilogStart, value.length);
  22. let newValue = `${prolog}${newEntry}${epilog}`;
  23. const newSelectionStart = prologEnd + newEntry.length;
  24. if (element.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(element, newValue)) {
  25. newValue = value;
  26. }
  27. if (element.type === 'time' && !(0, _isValidInputTimeValue.isValidInputTimeValue)(element, newValue)) {
  28. if ((0, _isValidInputTimeValue.isValidInputTimeValue)(element, newEntry)) {
  29. newValue = newEntry;
  30. } else {
  31. newValue = value;
  32. }
  33. }
  34. return {
  35. newValue,
  36. newSelectionStart
  37. };
  38. }