@thecb/components 10.9.0-beta.8 → 10.9.0-beta.9

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
@@ -26767,9 +26767,7 @@ var inject = function inject(baseString) {
26767
26767
  };
26768
26768
  };
26769
26769
  var formattedToUnformattedIndex = function formattedToUnformattedIndex(formattedIndex, rawValue, formatter) {
26770
- // TODO: fix this
26771
26770
  var maxFormatExceeded = rawValue.length >= formatter.formats.length;
26772
- console.log("🚀 ~ maxFormatExceeded:", maxFormatExceeded);
26773
26771
  if (maxFormatExceeded) {
26774
26772
  return formattedIndex;
26775
26773
  } else {
@@ -26781,9 +26779,7 @@ var formattedToUnformattedIndex = function formattedToUnformattedIndex(formatted
26781
26779
  }
26782
26780
  };
26783
26781
  var unformattedToFormattedIndex = function unformattedToFormattedIndex(rawIndex, rawValue, formatter, del) {
26784
- // TODO: fix this
26785
- var maxFormatExceeded = rawValue.length >= formatter.formats.length;
26786
- console.log("🚀 ~ unformattedToFormattedIndex ~ maxFormatExceeded:", maxFormatExceeded); // If forced to stay formatted, offset by delims - 1
26782
+ var maxFormatExceeded = rawValue.length >= formatter.formats.length; // If forced to stay formatted, offset by delims - 1
26787
26783
  // This is done so the component doesn't assume that any added chars will be kept
26788
26784
  // (i.e. if an external constraint is applied)
26789
26785
 
@@ -26810,15 +26806,17 @@ var FormattedInput = function FormattedInput(_ref) {
26810
26806
  props = _objectWithoutProperties$1(_ref, ["value", "formatter", "onChange"]);
26811
26807
  var inputEl = React.useRef(null);
26812
26808
  var _useState = React.useState({
26813
- selectionStart: 0,
26814
- selectionEnd: 0,
26815
- rawValue: value,
26816
26809
  formattedValue: format$1(formatter)(value)
26817
26810
  }),
26818
26811
  _useState2 = _slicedToArray$1(_useState, 2),
26819
26812
  state = _useState2[0],
26820
26813
  setState = _useState2[1];
26821
- var isDeleteRef = React.useRef(false);
26814
+ var stateRefs = React.useRef({
26815
+ selectionStart: 0,
26816
+ selectionEnd: 0,
26817
+ isDelete: false,
26818
+ rawValue: ''
26819
+ });
26822
26820
  React.useLayoutEffect(function () {
26823
26821
  // A lot of the work here is cursor manipulation
26824
26822
  if (inputEl.current && inputEl.current === document.activeElement) {
@@ -26826,52 +26824,54 @@ var FormattedInput = function FormattedInput(_ref) {
26826
26824
  }
26827
26825
  });
26828
26826
  var handleChange = function handleChange(event) {
26829
- // Detect the key event before the value change
26830
- // const deleteKeyPressed = isDeleteRef.current;
26831
- debugger;
26832
- var deleteKeyPressed = event.key === "Backspace" || event.key === "Delete";
26833
26827
  /* At the beginning of onChange, event.target.value is a concat of the previous formatted value
26834
- * and an unformatted injection at the start, end, or in the middle (maybe a deletion). To prepare
26835
- * the unformatted value for the user's onChange, the formatted string and unformatted injection need
26836
- * to be separated, then unformat the formatted string, then insert (or delete) the injection from the
26837
- * old unformatted value.
26838
- */
26839
-
26828
+ * and an unformatted injection at the start, end, or in the middle (maybe a deletion). To prepare
26829
+ * the unformatted value for the user's onChange, the formatted string and unformatted injection need
26830
+ * to be separated, then unformat the formatted string, then insert (or delete) the injection from the
26831
+ * old unformatted value.
26832
+ */
26840
26833
  var injectionLength = event.target.value.length - state.formattedValue.length;
26841
- var end = state.selectionStart === state.selectionEnd ? state.selectionStart + injectionLength : state.selectionEnd - 1;
26842
- var injection = event.target.value.substring(state.selectionStart, end); // Injection is the new unformatted piece of the input
26834
+ var end = stateRefs.current.selectionStart === stateRefs.current.selectionEnd ? stateRefs.current.selectionStart + injectionLength : stateRefs.current.selectionEnd - 1;
26835
+ var injection = event.target.value.substring(stateRefs.current.selectionStart, end); // Injection is the new unformatted piece of the input
26843
26836
  // Need to find where to put it
26844
26837
 
26845
- var rawInjectionPointStart = formattedToUnformattedIndex(state.selectionStart, state.rawValue, formatter);
26846
- var rawInjectionPointEnd = formattedToUnformattedIndex(state.selectionEnd, state.rawValue, formatter); // Unformat the previous formatted value for injection
26838
+ var rawInjectionPointStart = formattedToUnformattedIndex(stateRefs.current.selectionStart, stateRefs.current.rawValue, formatter);
26839
+ var rawInjectionPointEnd = formattedToUnformattedIndex(stateRefs.current.selectionEnd, stateRefs.current.rawValue, formatter); // Unformat the previous formatted value for injection
26847
26840
  // Using the relevant format string, strips away chars not marked with the formatChar
26848
26841
 
26849
- var unformattedOldValue = unformat(formatter)(state.formattedValue, state.rawValue.length); // Edit the previous unformatted value (either add, update or delete)
26842
+ var unformattedOldValue = unformat(formatter)(state.formattedValue, stateRefs.current.rawValue.length); // Edit the previous unformatted value (either add, update or delete)
26850
26843
 
26851
26844
  var injectIntoOldValue = inject(unformattedOldValue);
26845
+ var deleteKeyPressed = stateRefs.current.isDelete;
26852
26846
  var unformattedNewValue = deleteKeyPressed ? rawInjectionPointStart === rawInjectionPointEnd ? injectIntoOldValue(rawInjectionPointStart - 1, rawInjectionPointStart, "") : injectIntoOldValue(rawInjectionPointStart, rawInjectionPointEnd, "") : injectIntoOldValue(rawInjectionPointStart, rawInjectionPointEnd, injection);
26853
- var lengthDifference = unformattedNewValue.length - state.rawValue.length;
26854
- var rawIndex = formattedToUnformattedIndex(state.selectionStart, state.rawValue, formatter) + lengthDifference; // Find the new cursor position for the potential formatted value
26847
+ var lengthDifference = unformattedNewValue.length - stateRefs.current.rawValue.length;
26848
+ var rawIndex = formattedToUnformattedIndex(stateRefs.current.selectionStart, stateRefs.current.rawValue, formatter) + lengthDifference; // Find the new cursor position for the potential formatted value
26855
26849
  // Applied by useLayoutEffect
26856
26850
 
26857
- var newFormattedCursorPosition = state.selectionStart == state.selectionEnd ? unformattedToFormattedIndex(rawIndex, unformattedNewValue, formatter, deleteKeyPressed) : deleteKeyPressed ? state.selectionStart : state.selectionEnd;
26851
+ var newFormattedCursorPosition = stateRefs.current.selectionStart === stateRefs.current.selectionEnd ? unformattedToFormattedIndex(rawIndex, unformattedNewValue, formatter, deleteKeyPressed) : deleteKeyPressed ? stateRefs.current.selectionStart : stateRefs.current.selectionEnd;
26858
26852
  setState({
26859
26853
  selectionStart: newFormattedCursorPosition,
26860
26854
  selectionEnd: newFormattedCursorPosition,
26861
26855
  rawValue: unformattedNewValue,
26862
26856
  formattedValue: format$1(formatter)(unformattedNewValue)
26863
- });
26857
+ }); // Apply the external onChange function to the raw underlying string
26858
+ // This is where the user generally updates the input value
26859
+
26864
26860
  if (onChange) {
26865
26861
  onChange(unformattedNewValue);
26866
- } // Reset delete flag
26867
-
26868
- isDeleteRef.current = false;
26862
+ }
26869
26863
  };
26870
26864
  return /*#__PURE__*/React__default.createElement("input", _extends$2({}, props, {
26871
26865
  ref: inputEl,
26872
- value: state.formattedValue,
26866
+ value: format$1(formatter)(value),
26873
26867
  onKeyDown: function onKeyDown(event) {
26874
- isDeleteRef.current = event.key === "Backspace" || event.key === "Delete";
26868
+ // Keep track of the state of the input before onChange
26869
+ stateRefs.current = {
26870
+ isDelete: event.key === "Backspace" || event.key === "Delete",
26871
+ selectionStart: event.target.selectionStart,
26872
+ selectionEnd: event.target.selectionEnd,
26873
+ rawValue: value
26874
+ };
26875
26875
  },
26876
26876
  onChange: handleChange
26877
26877
  }));