@thecb/components 10.9.0-beta.9 → 10.9.0

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/dist/index.cjs.js CHANGED
@@ -26403,13 +26403,26 @@ var useToastNotification = function useToastNotification() {
26403
26403
  };
26404
26404
  };
26405
26405
 
26406
+ function useConditionallyAddValidator(condition, validatorFn, addValidator, removeValidator) {
26407
+ React.useEffect(function () {
26408
+ if (condition) {
26409
+ addValidator(validatorFn());
26410
+ }
26411
+ return function () {
26412
+ // Remove validator when component unmounts
26413
+ removeValidator(validatorFn());
26414
+ };
26415
+ }, [condition, addValidator, removeValidator]);
26416
+ }
26417
+
26406
26418
 
26407
26419
 
26408
26420
  var index$5 = /*#__PURE__*/Object.freeze({
26409
26421
  __proto__: null,
26410
26422
  useOutsideClick: useOutsideClickHook,
26411
26423
  useScrollTo: useScrollTo,
26412
- useToastNotification: useToastNotification
26424
+ useToastNotification: useToastNotification,
26425
+ useConditionallyAddValidator: useConditionallyAddValidator
26413
26426
  });
26414
26427
 
26415
26428
  var hoverColor$4 = "#116285";
@@ -26638,34 +26651,45 @@ function _extends$2() {
26638
26651
  _extends$2 = Object.assign || function (target) {
26639
26652
  for (var i = 1; i < arguments.length; i++) {
26640
26653
  var source = arguments[i];
26654
+
26641
26655
  for (var key in source) {
26642
26656
  if (Object.prototype.hasOwnProperty.call(source, key)) {
26643
26657
  target[key] = source[key];
26644
26658
  }
26645
26659
  }
26646
26660
  }
26661
+
26647
26662
  return target;
26648
26663
  };
26664
+
26649
26665
  return _extends$2.apply(this, arguments);
26650
26666
  }
26667
+
26651
26668
  function _objectWithoutPropertiesLoose$1(source, excluded) {
26652
26669
  if (source == null) return {};
26653
26670
  var target = {};
26654
26671
  var sourceKeys = Object.keys(source);
26655
26672
  var key, i;
26673
+
26656
26674
  for (i = 0; i < sourceKeys.length; i++) {
26657
26675
  key = sourceKeys[i];
26658
26676
  if (excluded.indexOf(key) >= 0) continue;
26659
26677
  target[key] = source[key];
26660
26678
  }
26679
+
26661
26680
  return target;
26662
26681
  }
26682
+
26663
26683
  function _objectWithoutProperties$1(source, excluded) {
26664
26684
  if (source == null) return {};
26685
+
26665
26686
  var target = _objectWithoutPropertiesLoose$1(source, excluded);
26687
+
26666
26688
  var key, i;
26689
+
26667
26690
  if (Object.getOwnPropertySymbols) {
26668
26691
  var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
26692
+
26669
26693
  for (i = 0; i < sourceSymbolKeys.length; i++) {
26670
26694
  key = sourceSymbolKeys[i];
26671
26695
  if (excluded.indexOf(key) >= 0) continue;
@@ -26673,37 +26697,48 @@ function _objectWithoutProperties$1(source, excluded) {
26673
26697
  target[key] = source[key];
26674
26698
  }
26675
26699
  }
26700
+
26676
26701
  return target;
26677
26702
  }
26703
+
26678
26704
  function _slicedToArray$1(arr, i) {
26679
26705
  return _arrayWithHoles$1(arr) || _iterableToArrayLimit$1(arr, i) || _nonIterableRest$1();
26680
26706
  }
26707
+
26681
26708
  function _toConsumableArray$1(arr) {
26682
26709
  return _arrayWithoutHoles$1(arr) || _iterableToArray$1(arr) || _nonIterableSpread$1();
26683
26710
  }
26711
+
26684
26712
  function _arrayWithoutHoles$1(arr) {
26685
26713
  if (Array.isArray(arr)) {
26686
26714
  for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
26715
+
26687
26716
  return arr2;
26688
26717
  }
26689
26718
  }
26719
+
26690
26720
  function _arrayWithHoles$1(arr) {
26691
26721
  if (Array.isArray(arr)) return arr;
26692
26722
  }
26723
+
26693
26724
  function _iterableToArray$1(iter) {
26694
26725
  if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
26695
26726
  }
26727
+
26696
26728
  function _iterableToArrayLimit$1(arr, i) {
26697
26729
  if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
26698
26730
  return;
26699
26731
  }
26732
+
26700
26733
  var _arr = [];
26701
26734
  var _n = true;
26702
26735
  var _d = false;
26703
26736
  var _e = undefined;
26737
+
26704
26738
  try {
26705
26739
  for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
26706
26740
  _arr.push(_s.value);
26741
+
26707
26742
  if (i && _arr.length === i) break;
26708
26743
  }
26709
26744
  } catch (err) {
@@ -26716,23 +26751,29 @@ function _iterableToArrayLimit$1(arr, i) {
26716
26751
  if (_d) throw _e;
26717
26752
  }
26718
26753
  }
26754
+
26719
26755
  return _arr;
26720
26756
  }
26757
+
26721
26758
  function _nonIterableSpread$1() {
26722
26759
  throw new TypeError("Invalid attempt to spread non-iterable instance");
26723
26760
  }
26761
+
26724
26762
  function _nonIterableRest$1() {
26725
26763
  throw new TypeError("Invalid attempt to destructure non-iterable instance");
26726
26764
  }
26765
+
26727
26766
  var getUniqueFormatDelimiters = function getUniqueFormatDelimiters(formats, formatChar) {
26728
26767
  return _toConsumableArray$1(new Set(formats.join("").split(formatChar).join("").split("")));
26729
26768
  };
26730
26769
  var format$1 = function format(formatter) {
26731
26770
  return function (value) {
26732
26771
  var usedFormat = formatter.formats[value.length];
26772
+
26733
26773
  if (!usedFormat) {
26734
26774
  return value;
26735
26775
  }
26776
+
26736
26777
  var formatPieces = usedFormat.split(formatter.formatChar);
26737
26778
  var valuePieces = value.split("");
26738
26779
  var zipped = formatPieces.map(function (v, i) {
@@ -26741,6 +26782,7 @@ var format$1 = function format(formatter) {
26741
26782
  return zipped.join("");
26742
26783
  };
26743
26784
  };
26785
+
26744
26786
  var countDelims = function countDelims(formatter, index) {
26745
26787
  var count = 0;
26746
26788
  var format = formatter.formats[index];
@@ -26750,11 +26792,13 @@ var countDelims = function countDelims(formatter, index) {
26750
26792
  });
26751
26793
  return count;
26752
26794
  };
26795
+
26753
26796
  var unformat = function unformat(formatter) {
26754
26797
  return function (formattedValue, formatIndex) {
26755
26798
  if (formatIndex >= formatter.formats.length) {
26756
26799
  return formattedValue;
26757
26800
  }
26801
+
26758
26802
  var format = formatter.formats[formatIndex];
26759
26803
  return formattedValue.split("").filter(function (_, i) {
26760
26804
  return !(format[i] != formatter.formatChar);
@@ -26768,6 +26812,7 @@ var inject = function inject(baseString) {
26768
26812
  };
26769
26813
  var formattedToUnformattedIndex = function formattedToUnformattedIndex(formattedIndex, rawValue, formatter) {
26770
26814
  var maxFormatExceeded = rawValue.length >= formatter.formats.length;
26815
+
26771
26816
  if (maxFormatExceeded) {
26772
26817
  return formattedIndex;
26773
26818
  } else {
@@ -26792,6 +26837,7 @@ var unformattedToFormattedIndex = function unformattedToFormattedIndex(rawIndex,
26792
26837
  }, 0) + rawIndex;
26793
26838
  }
26794
26839
  };
26840
+
26795
26841
  var createFormat = function createFormat(formats, formatChar) {
26796
26842
  return {
26797
26843
  uniqueDelimiters: getUniqueFormatDelimiters(formats, formatChar),
@@ -26799,18 +26845,19 @@ var createFormat = function createFormat(formats, formatChar) {
26799
26845
  formatChar: formatChar
26800
26846
  };
26801
26847
  };
26848
+
26802
26849
  var FormattedInput = function FormattedInput(_ref) {
26803
26850
  var value = _ref.value,
26804
- formatter = _ref.formatter,
26805
- onChange = _ref.onChange,
26806
- props = _objectWithoutProperties$1(_ref, ["value", "formatter", "onChange"]);
26851
+ formatter = _ref.formatter,
26852
+ onChange = _ref.onChange,
26853
+ props = _objectWithoutProperties$1(_ref, ["value", "formatter", "onChange"]);
26854
+
26855
+ var _useState = React.useState(format$1(formatter)(value)),
26856
+ _useState2 = _slicedToArray$1(_useState, 2),
26857
+ formattedValue = _useState2[0],
26858
+ setFormattedValue = _useState2[1];
26859
+
26807
26860
  var inputEl = React.useRef(null);
26808
- var _useState = React.useState({
26809
- formattedValue: format$1(formatter)(value)
26810
- }),
26811
- _useState2 = _slicedToArray$1(_useState, 2),
26812
- state = _useState2[0],
26813
- setState = _useState2[1];
26814
26861
  var stateRefs = React.useRef({
26815
26862
  selectionStart: 0,
26816
26863
  selectionEnd: 0,
@@ -26820,17 +26867,26 @@ var FormattedInput = function FormattedInput(_ref) {
26820
26867
  React.useLayoutEffect(function () {
26821
26868
  // A lot of the work here is cursor manipulation
26822
26869
  if (inputEl.current && inputEl.current === document.activeElement) {
26823
- inputEl.current.setSelectionRange(state.selectionStart, state.selectionEnd);
26870
+ inputEl.current.setSelectionRange(stateRefs.current.selectionStart, stateRefs.current.selectionEnd);
26824
26871
  }
26825
- });
26872
+ }, [stateRefs]);
26873
+
26826
26874
  var handleChange = function handleChange(event) {
26875
+ var deleteKeyPressed = stateRefs.current.isDelete;
26876
+ var maxFormatExceeded = stateRefs.current.rawValue.length >= formatter.formats.length - 1;
26877
+
26878
+ if (maxFormatExceeded && !deleteKeyPressed) {
26879
+ return;
26880
+ }
26827
26881
  /* At the beginning of onChange, event.target.value is a concat of the previous formatted value
26828
26882
  * and an unformatted injection at the start, end, or in the middle (maybe a deletion). To prepare
26829
26883
  * the unformatted value for the user's onChange, the formatted string and unformatted injection need
26830
26884
  * to be separated, then unformat the formatted string, then insert (or delete) the injection from the
26831
26885
  * old unformatted value.
26832
26886
  */
26833
- var injectionLength = event.target.value.length - state.formattedValue.length;
26887
+
26888
+
26889
+ var injectionLength = event.target.value.length - formattedValue.length;
26834
26890
  var end = stateRefs.current.selectionStart === stateRefs.current.selectionEnd ? stateRefs.current.selectionStart + injectionLength : stateRefs.current.selectionEnd - 1;
26835
26891
  var injection = event.target.value.substring(stateRefs.current.selectionStart, end); // Injection is the new unformatted piece of the input
26836
26892
  // Need to find where to put it
@@ -26839,29 +26895,25 @@ var FormattedInput = function FormattedInput(_ref) {
26839
26895
  var rawInjectionPointEnd = formattedToUnformattedIndex(stateRefs.current.selectionEnd, stateRefs.current.rawValue, formatter); // Unformat the previous formatted value for injection
26840
26896
  // Using the relevant format string, strips away chars not marked with the formatChar
26841
26897
 
26842
- var unformattedOldValue = unformat(formatter)(state.formattedValue, stateRefs.current.rawValue.length); // Edit the previous unformatted value (either add, update or delete)
26898
+ var unformattedOldValue = unformat(formatter)(formattedValue, stateRefs.current.rawValue.length); // Edit the previous unformatted value (either add, update or delete)
26843
26899
 
26844
26900
  var injectIntoOldValue = inject(unformattedOldValue);
26845
- var deleteKeyPressed = stateRefs.current.isDelete;
26846
26901
  var unformattedNewValue = deleteKeyPressed ? rawInjectionPointStart === rawInjectionPointEnd ? injectIntoOldValue(rawInjectionPointStart - 1, rawInjectionPointStart, "") : injectIntoOldValue(rawInjectionPointStart, rawInjectionPointEnd, "") : injectIntoOldValue(rawInjectionPointStart, rawInjectionPointEnd, injection);
26847
26902
  var lengthDifference = unformattedNewValue.length - stateRefs.current.rawValue.length;
26848
26903
  var rawIndex = formattedToUnformattedIndex(stateRefs.current.selectionStart, stateRefs.current.rawValue, formatter) + lengthDifference; // Find the new cursor position for the potential formatted value
26849
26904
  // Applied by useLayoutEffect
26850
26905
 
26851
26906
  var newFormattedCursorPosition = stateRefs.current.selectionStart === stateRefs.current.selectionEnd ? unformattedToFormattedIndex(rawIndex, unformattedNewValue, formatter, deleteKeyPressed) : deleteKeyPressed ? stateRefs.current.selectionStart : stateRefs.current.selectionEnd;
26852
- setState({
26853
- selectionStart: newFormattedCursorPosition,
26854
- selectionEnd: newFormattedCursorPosition,
26855
- rawValue: unformattedNewValue,
26856
- formattedValue: format$1(formatter)(unformattedNewValue)
26857
- }); // Apply the external onChange function to the raw underlying string
26907
+ var formattedNewValue = format$1(formatter)(unformattedNewValue);
26908
+ setFormattedValue(formattedNewValue); // Apply the external onChange function to the raw underlying string
26858
26909
  // This is where the user generally updates the input value
26859
26910
 
26860
26911
  if (onChange) {
26861
26912
  onChange(unformattedNewValue);
26862
26913
  }
26863
26914
  };
26864
- return /*#__PURE__*/React__default.createElement("input", _extends$2({}, props, {
26915
+
26916
+ return React__default.createElement("input", _extends$2({}, props, {
26865
26917
  ref: inputEl,
26866
26918
  value: format$1(formatter)(value),
26867
26919
  onKeyDown: function onKeyDown(event) {
@@ -41742,6 +41794,7 @@ var EmailForm = function EmailForm(_ref) {
41742
41794
  };
41743
41795
  }, []);
41744
41796
  }
41797
+ useConditionallyAddValidator(isRequired, required, actions.fields.email.addValidator, actions.fields.email.removeValidator);
41745
41798
  var emailFieldErrorMessages = _defineProperty(_defineProperty({}, required.error, "Email address is required"), isProbablyEmail.error, "Invalid email address");
41746
41799
  return /*#__PURE__*/React__default.createElement(FormContainer$1, {
41747
41800
  variant: variant,
@@ -41773,7 +41826,7 @@ var EmailForm = function EmailForm(_ref) {
41773
41826
 
41774
41827
  var formConfig$3 = {
41775
41828
  email: {
41776
- validators: [required(), isProbablyEmail()]
41829
+ validators: [isProbablyEmail()] // 'required' validator is conditionally added based on `isRequired` prop
41777
41830
  }
41778
41831
  };
41779
41832
  var _createFormState$3 = createFormState(formConfig$3),
@@ -48977,6 +49030,7 @@ var PhoneForm = function PhoneForm(_ref) {
48977
49030
  };
48978
49031
  }, []);
48979
49032
  }
49033
+ useConditionallyAddValidator(isRequired, required, actions.fields.phone.addValidator, actions.fields.phone.removeValidator);
48980
49034
  var phoneErrorMessage = _defineProperty(_defineProperty({}, required.error, "Phone number is required"), hasLength.error, "Phone number must be 10 digits");
48981
49035
  return /*#__PURE__*/React__default.createElement(FormContainer$1, {
48982
49036
  variant: variant,
@@ -49006,7 +49060,8 @@ var PhoneForm = function PhoneForm(_ref) {
49006
49060
 
49007
49061
  var formConfig$8 = {
49008
49062
  phone: {
49009
- validators: [required(), hasLength(10, 10)],
49063
+ validators: [hasLength(10, 10)],
49064
+ // 'required' validator is conditionally added based on `isRequired` prop
49010
49065
  constraints: [onlyIntegers(), hasLength(0, 10)]
49011
49066
  }
49012
49067
  };