@skbkontur/react-ui-validations 2.1.3-66a28a30 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -255,11 +255,19 @@ var isReactUITestEnv = Boolean(REACT_UI_TEST) || // for cases when NODE_ENV is n
255
255
  Boolean(enableReactTesting); // deprecated, legacy variable
256
256
  var isTestEnv = NODE_ENV === 'test' || isReactUITestEnv;
257
257
  var isBrowser = typeof window !== 'undefined';
258
+ var canUseDOM = isBrowser && window.document && window.document.createElement;
258
259
  function isElement(el) {
259
- return !!el && typeof el === 'object' && 'nodeType' in el && el.nodeType === Node.ELEMENT_NODE;
260
+ if (isBrowser) {
261
+ return el instanceof Element;
262
+ }
263
+ return false;
260
264
  }
261
- var getDoc = function (node) { var _a; return (_a = node.ownerDocument) !== null && _a !== void 0 ? _a : node; };
262
- var getWin = function (node) { return getDoc(node).defaultView; };
265
+ function isNode(node) {
266
+ if (isBrowser) {
267
+ return node instanceof Node;
268
+ }
269
+ return false;
270
+ }
263
271
 
264
272
  /**
265
273
  * Checks if the value `null` or `undefined`.
@@ -490,7 +498,7 @@ var getRootNode = function (instance) {
490
498
  * 3. class Component instance (object)
491
499
  * 4. literally anything, returned from useImperativeHandle
492
500
  */
493
- if (!instance) {
501
+ if (!canUseDOM || !instance) {
494
502
  // instance can be `null` if component was unmounted
495
503
  // also checking undefined for convenient usage
496
504
  return null;
@@ -502,7 +510,13 @@ var getRootNode = function (instance) {
502
510
  }
503
511
  var rootNode;
504
512
  if (isInstanceWithRootNode(instance)) {
505
- rootNode = instance.getRootNode();
513
+ // it happened to be that native Node interface also has
514
+ // the "getRootNode" method, but we can ignore it here
515
+ // because we'd already checked the instance on being an Element
516
+ // which is a subclass of Node, so, just fixing types here
517
+ if (!isNode(instance)) {
518
+ rootNode = instance.getRootNode();
519
+ }
506
520
  }
507
521
  if (rootNode !== undefined) {
508
522
  // the getter exists and has returned something, it should be what we are looking for
@@ -533,21 +547,19 @@ var getRootNode = function (instance) {
533
547
 
534
548
  function smoothScrollIntoView(element, scrollOffset) {
535
549
  return __awaiter(this, void 0, void 0, function () {
536
- var activeDocument, activeWindow, scrollableParent, parentRects, clientRects, topOffset, bottomOffset, html, viewportHeight, viewportWidth, isElementInViewport;
550
+ var scrollableParent, parentRects, clientRects, topOffset, bottomOffset, html, viewportHeight, viewportWidth, isElementInViewport;
537
551
  return __generator(this, function (_a) {
538
552
  switch (_a.label) {
539
553
  case 0:
540
- activeDocument = getDoc(element);
541
- activeWindow = getWin(element);
542
554
  scrollableParent = findScrollableParent(element);
543
555
  parentRects = scrollableParent.getBoundingClientRect();
544
556
  clientRects = element.getBoundingClientRect();
545
557
  topOffset = scrollOffset.top || 0;
546
558
  bottomOffset = scrollOffset.bottom || 0;
547
- if (!(scrollableParent === activeDocument.body)) return [3 /*break*/, 2];
548
- html = activeDocument.documentElement || { clientHeight: 0, clientWidth: 0 };
549
- viewportHeight = (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.innerHeight) || html.clientHeight;
550
- viewportWidth = (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.innerWidth) || html.clientWidth;
559
+ if (!(scrollableParent === document.body)) return [3 /*break*/, 2];
560
+ html = document.documentElement || { clientHeight: 0, clientWidth: 0 };
561
+ viewportHeight = window.innerHeight || html.clientHeight;
562
+ viewportWidth = window.innerWidth || html.clientWidth;
551
563
  isElementInViewport = clientRects.top >= topOffset &&
552
564
  clientRects.left >= 0 &&
553
565
  clientRects.bottom <= viewportHeight - bottomOffset &&
@@ -556,7 +568,6 @@ function smoothScrollIntoView(element, scrollOffset) {
556
568
  return [2 /*return*/];
557
569
  }
558
570
  return [4 /*yield*/, scrollBy({
559
- element: element,
560
571
  left: clientRects.left,
561
572
  top: clientRects.top - topOffset,
562
573
  })];
@@ -571,7 +582,6 @@ function smoothScrollIntoView(element, scrollOffset) {
571
582
  case 3:
572
583
  _a.sent();
573
584
  return [4 /*yield*/, scrollBy({
574
- element: element,
575
585
  left: parentRects.left,
576
586
  top: parentRects.top,
577
587
  })];
@@ -584,68 +594,59 @@ function smoothScrollIntoView(element, scrollOffset) {
584
594
  });
585
595
  }
586
596
  function smoothScroll(element, x, y) {
587
- var activeDocument = getDoc(element);
588
- var activeWindow = getWin(element);
589
597
  var context;
590
- if (activeWindow) {
591
- if (element === getDocumentBodyStrict(activeDocument)) {
592
- context = {
593
- scrollable: element,
594
- startX: activeWindow.scrollX || activeWindow.pageXOffset,
595
- startY: activeWindow.scrollY || activeWindow.pageYOffset,
596
- method: getScrollWindow(element),
597
- startTime: getNow(element)(),
598
- x: x,
599
- y: y,
600
- };
601
- }
602
- else {
603
- context = {
604
- scrollable: element,
605
- startX: element.scrollLeft,
606
- startY: element.scrollTop,
607
- method: scrollElement,
608
- startTime: getNow(element)(),
609
- x: x,
610
- y: y,
611
- };
612
- }
598
+ if (element === getDocumentBodyStrict()) {
599
+ context = {
600
+ scrollable: window,
601
+ startX: window.scrollX || window.pageXOffset,
602
+ startY: window.scrollY || window.pageYOffset,
603
+ method: scrollWindow,
604
+ startTime: now(),
605
+ x: x,
606
+ y: y,
607
+ };
608
+ }
609
+ else {
610
+ context = {
611
+ scrollable: element,
612
+ startX: element.scrollLeft,
613
+ startY: element.scrollTop,
614
+ method: scrollElement,
615
+ startTime: now(),
616
+ x: x,
617
+ y: y,
618
+ };
613
619
  }
614
620
  return new Promise(function (resolve) { return step(__assign(__assign({}, context), { resolve: resolve })); });
615
621
  }
616
- function step(_a) {
617
- var scrollable = _a.scrollable, startX = _a.startX, startY = _a.startY, x = _a.x, y = _a.y, method = _a.method, resolve = _a.resolve, startTime = _a.startTime;
618
- var time = getNow(scrollable)();
619
- var scrollTime = 468;
620
- var elapsed = Math.min(1, (time - startTime) / scrollTime);
622
+ function step(context) {
623
+ var time = now();
624
+ var elapsed = Math.min(1, (time - context.startTime) / ScrollTime);
621
625
  var value = ease(elapsed);
622
- var activeWindow = getWin(scrollable);
623
- var currentX = startX + (x - startX) * value;
624
- var currentY = startY + (y - startY) * value;
625
- method(scrollable, currentX, currentY);
626
- if (currentX !== x || currentY !== y) {
627
- !!activeWindow &&
628
- activeWindow.requestAnimationFrame(function () { return step({ scrollable: scrollable, startX: startX, startY: startY, x: x, y: y, method: method, resolve: resolve, startTime: startTime }); });
626
+ var currentX = context.startX + (context.x - context.startX) * value;
627
+ var currentY = context.startY + (context.y - context.startY) * value;
628
+ context.method(context.scrollable, currentX, currentY);
629
+ if (currentX !== context.x || currentY !== context.y) {
630
+ window.requestAnimationFrame(function () { return step(context); });
629
631
  }
630
632
  else {
631
- resolve();
633
+ context.resolve();
632
634
  }
633
635
  }
634
- var getScrollWindow = function (el) {
635
- var activeWindow = getWin(el);
636
+ var ScrollTime = 468;
637
+ var getScrollWindow = function (isBrowser) {
636
638
  if (isBrowser) {
637
- if (typeof (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.scroll) === 'function') {
638
- return function (_, x, y) { return activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.scroll(x, y); };
639
+ if (typeof window.scroll === 'function') {
640
+ return function (_, x, y) { return window.scroll(x, y); };
639
641
  }
642
+ return function (_, x, y) { return window.scrollTo(x, y); };
640
643
  }
641
644
  return function () { return undefined; };
642
645
  };
643
- var getNow = function (el) {
644
- var activeWindow = getWin(el);
645
- return isBrowser && (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.performance) && (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.performance.now)
646
- ? activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.performance.now.bind(activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.performance)
647
- : Date.now;
648
- };
646
+ var scrollWindow = getScrollWindow(isBrowser);
647
+ var now = isBrowser && window.performance && window.performance.now
648
+ ? window.performance.now.bind(window.performance)
649
+ : Date.now;
649
650
  function scrollElement(element, x, y) {
650
651
  element.scrollLeft = x;
651
652
  element.scrollTop = y;
@@ -653,28 +654,27 @@ function scrollElement(element, x, y) {
653
654
  function ease(time) {
654
655
  return 0.5 * (1 - Math.cos(Math.PI * time));
655
656
  }
656
- function getDocumentBodyStrict(activeDocument) {
657
- if (isNullable(activeDocument.body)) {
657
+ function getDocumentBodyStrict() {
658
+ if (isNullable(document.body)) {
658
659
  throw new Error('Scrolling can be used only in browser');
659
660
  }
660
- return activeDocument.body;
661
+ return document.body;
661
662
  }
662
663
  function findScrollableParent(el) {
663
- var _a;
664
664
  var isBody;
665
665
  var hasScrollableSpace;
666
666
  var hasVisibleOverflow;
667
667
  var currentElement = el;
668
668
  do {
669
- if (isNullable(currentElement.parentElement) || !isElement(currentElement.parentElement)) {
670
- return getDocumentBodyStrict(getDoc(currentElement));
669
+ if (isNullable(currentElement.parentElement) || !(currentElement.parentElement instanceof HTMLElement)) {
670
+ return getDocumentBodyStrict();
671
671
  }
672
672
  currentElement = currentElement.parentElement;
673
- isBody = currentElement === getDoc(currentElement).body;
673
+ isBody = currentElement === document.body;
674
674
  hasScrollableSpace =
675
675
  currentElement.clientHeight < currentElement.scrollHeight ||
676
676
  currentElement.clientWidth < currentElement.scrollWidth;
677
- hasVisibleOverflow = ((_a = getWin(currentElement)) === null || _a === void 0 ? void 0 : _a.getComputedStyle(currentElement, null).overflow) === 'visible';
677
+ hasVisibleOverflow = window.getComputedStyle(currentElement, null).overflow === 'visible';
678
678
  } while (!isBody && !(hasScrollableSpace && !hasVisibleOverflow));
679
679
  isBody = null;
680
680
  hasScrollableSpace = null;
@@ -682,15 +682,8 @@ function findScrollableParent(el) {
682
682
  return currentElement;
683
683
  }
684
684
  function scrollBy(_a) {
685
- var element = _a.element, left = _a.left, top = _a.top;
686
- var activeWindow = getWin(element);
687
- var activeDocument = getDoc(element);
688
- if (!activeWindow) {
689
- return new Promise(function (resolve) { return resolve(); });
690
- }
691
- var x = Math.floor(left) + ((activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.scrollX) || (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.pageXOffset));
692
- var y = Math.floor(top) + ((activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.scrollY) || (activeWindow === null || activeWindow === void 0 ? void 0 : activeWindow.pageYOffset));
693
- return smoothScroll(getDocumentBodyStrict(activeDocument), x, y);
685
+ var left = _a.left, top = _a.top;
686
+ return smoothScroll(getDocumentBodyStrict(), Math.floor(left) + (window.scrollX || window.pageXOffset), Math.floor(top) + (window.scrollY || window.pageYOffset));
694
687
  }
695
688
 
696
689
  function getVisibleValidation(visible, actual, changing) {
@@ -746,6 +739,10 @@ var getFullValidationsFlagsContext = function (flags) {
746
739
  return __assign(__assign({}, validationsFeatureFlagsDefault), flags);
747
740
  };
748
741
 
742
+ if (isBrowser && typeof HTMLElement === 'undefined') {
743
+ var w = window;
744
+ w.HTMLElement = w.Element;
745
+ }
749
746
  var ValidationWrapperInternal = /** @class */ (function (_super) {
750
747
  __extends(ValidationWrapperInternal, _super);
751
748
  function ValidationWrapperInternal() {
@@ -798,7 +795,7 @@ var ValidationWrapperInternal = /** @class */ (function (_super) {
798
795
  switch (_b.label) {
799
796
  case 0:
800
797
  htmlElement = this.getRootNode();
801
- if (!isElement(htmlElement)) return [3 /*break*/, 3];
798
+ if (!(htmlElement instanceof HTMLElement)) return [3 /*break*/, 3];
802
799
  _a = this.context.getSettings(), disableSmoothScroll = _a.disableSmoothScroll, scrollOffset = _a.scrollOffset;
803
800
  if (!!disableSmoothScroll) return [3 /*break*/, 2];
804
801
  return [4 /*yield*/, smoothScrollIntoView(htmlElement, scrollOffset)];
@@ -887,7 +884,7 @@ var ValidationWrapperInternal = /** @class */ (function (_super) {
887
884
  };
888
885
  ValidationWrapperInternal.prototype.getControlPosition = function () {
889
886
  var htmlElement = this.getRootNode();
890
- if (isElement(htmlElement)) {
887
+ if (htmlElement instanceof HTMLElement) {
891
888
  var rect = htmlElement.getBoundingClientRect();
892
889
  return { x: rect.top, y: rect.left };
893
890
  }