@startlibs/form 1.0.5 → 1.0.6

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.
Files changed (2) hide show
  1. package/lib/index.js +77 -98
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -37,6 +37,7 @@ import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstruct
37
37
  import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
38
38
  import _inherits from '@babel/runtime/helpers/inherits';
39
39
  import _concat from 'lodash/fp/concat';
40
+ import _last from 'lodash/fp/last';
40
41
  import _without from 'lodash/fp/without';
41
42
  import _isEqual from 'lodash/fp/isEqual';
42
43
  import ContentEditable from 'react-contenteditable';
@@ -51,7 +52,6 @@ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
51
52
  import _isBoolean from 'lodash/fp/isBoolean';
52
53
  import _negate from 'lodash/fp/negate';
53
54
  import _zipObject from 'lodash/fp/zipObject';
54
- import _last from 'lodash/fp/last';
55
55
  import _flatten from 'lodash/fp/flatten';
56
56
  import _fromPairs from 'lodash/fp/fromPairs';
57
57
  import _pick from 'lodash/fp/pick';
@@ -650,8 +650,9 @@ var formInput = function formInput(_ref) {
650
650
  return;
651
651
  }
652
652
 
653
- setFormValue(transform(value, opts));
654
- callIfFunction(onChange, callIfFunction(value, formValue), opts);
653
+ var transformedValue = transform(callIfFunction(value, formValue), opts);
654
+ setFormValue(transformedValue);
655
+ callIfFunction(onChange, transformedValue, opts);
655
656
  };
656
657
 
657
658
  var setValueFromEventTarget = function setValueFromEventTarget(e) {
@@ -1136,6 +1137,7 @@ var useTextInput = function useTextInput(_ref, ref) {
1136
1137
  var onFocus = _ref.onFocus,
1137
1138
  onBlur = _ref.onBlur,
1138
1139
  autoResize = _ref.autoResize,
1140
+ autoSelectOnFocus = _ref.autoSelectOnFocus,
1139
1141
  constraint = _ref.constraint,
1140
1142
  pattern = _ref.pattern,
1141
1143
  path = _ref.path,
@@ -1146,7 +1148,7 @@ var useTextInput = function useTextInput(_ref, ref) {
1146
1148
  raw = _ref.raw,
1147
1149
  showValue = _ref.showValue,
1148
1150
  deleteTrailing = _ref.deleteTrailing,
1149
- rest = _objectWithoutProperties(_ref, ["onFocus", "onBlur", "autoResize", "constraint", "pattern", "path", "rawValue", "rawSetValue", "rawHasErrors", "transformOnBlur", "raw", "showValue", "deleteTrailing"]);
1151
+ rest = _objectWithoutProperties(_ref, ["onFocus", "onBlur", "autoResize", "autoSelectOnFocus", "constraint", "pattern", "path", "rawValue", "rawSetValue", "rawHasErrors", "transformOnBlur", "raw", "showValue", "deleteTrailing"]);
1150
1152
 
1151
1153
  var _useInput = useInput$1(_objectSpread({
1152
1154
  path: path,
@@ -1168,6 +1170,15 @@ var useTextInput = function useTextInput(_ref, ref) {
1168
1170
 
1169
1171
  var handleOnFocus = function handleOnFocus(e) {
1170
1172
  setFocused(true);
1173
+
1174
+ if (!!autoSelectOnFocus && !!ref.current) {
1175
+ setTimeout(function () {
1176
+ var _ref$current;
1177
+
1178
+ return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.select();
1179
+ }, 0);
1180
+ }
1181
+
1171
1182
  callIfFunction(onFocus, e);
1172
1183
  };
1173
1184
 
@@ -1190,6 +1201,15 @@ var useTextInput = function useTextInput(_ref, ref) {
1190
1201
  doAutoResize(ref.current);
1191
1202
  }
1192
1203
  }, []);
1204
+ useEffect(function () {
1205
+ if (!!autoSelectOnFocus && !!ref.current && document.activeElement === ref.current) {
1206
+ setTimeout(function () {
1207
+ var _ref$current2;
1208
+
1209
+ return (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.select();
1210
+ }, 0);
1211
+ }
1212
+ }, [autoSelectOnFocus]);
1193
1213
 
1194
1214
  var handleChange = function handleChange(e) {
1195
1215
  if (autoResize) {
@@ -1312,6 +1332,7 @@ var TextInput = styled(forwardRef(function (_ref2, forwardedRef) {
1312
1332
  onImage = _ref2$onImage === void 0 ? false : _ref2$onImage,
1313
1333
  autoFocus = _ref2.autoFocus,
1314
1334
  noFocus = _ref2.noFocus,
1335
+ autoSelectOnFocus = _ref2.autoSelectOnFocus,
1315
1336
  onFocus = _ref2.onFocus,
1316
1337
  onBlur = _ref2.onBlur,
1317
1338
  pattern = _ref2.pattern,
@@ -1321,7 +1342,7 @@ var TextInput = styled(forwardRef(function (_ref2, forwardedRef) {
1321
1342
  raw = _ref2.raw,
1322
1343
  _ref2$tabIndex = _ref2.tabIndex,
1323
1344
  tabIndex = _ref2$tabIndex === void 0 ? 1 : _ref2$tabIndex,
1324
- rest = _objectWithoutProperties(_ref2, ["showValue", "label", "type", "helpText", "descText", "bellowDescText", "mandatory", "isLoading", "locked", "textBoxBefore", "textBoxAfter", "className", "fieldClassName", "FieldType", "margin", "value", "setValue", "hasErrors", "textarea", "onImage", "autoFocus", "noFocus", "onFocus", "onBlur", "pattern", "autoResize", "constraint", "path", "raw", "tabIndex"]);
1345
+ rest = _objectWithoutProperties(_ref2, ["showValue", "label", "type", "helpText", "descText", "bellowDescText", "mandatory", "isLoading", "locked", "textBoxBefore", "textBoxAfter", "className", "fieldClassName", "FieldType", "margin", "value", "setValue", "hasErrors", "textarea", "onImage", "autoFocus", "noFocus", "autoSelectOnFocus", "onFocus", "onBlur", "pattern", "autoResize", "constraint", "path", "raw", "tabIndex"]);
1325
1346
 
1326
1347
  var ref = useEnsureRef(forwardedRef);
1327
1348
 
@@ -1330,6 +1351,7 @@ var TextInput = styled(forwardRef(function (_ref2, forwardedRef) {
1330
1351
  onFocus: onFocus,
1331
1352
  onBlur: onBlur,
1332
1353
  autoResize: autoResize,
1354
+ autoSelectOnFocus: autoSelectOnFocus,
1333
1355
  constraint: constraint,
1334
1356
  pattern: pattern,
1335
1357
  rawValue: rawValue,
@@ -2948,6 +2970,7 @@ var DropdownIconContainer = styled(Icon).withConfig({
2948
2970
  var focusBeforeLast = function focusBeforeLast(e) {
2949
2971
  if (e.target && e.target !== e.currentTarget) return;
2950
2972
  var el = [].slice.call(e.currentTarget.querySelectorAll('div[contenteditable]')).slice(-1)[0];
2973
+ if (!el) return;
2951
2974
  el.focus();
2952
2975
 
2953
2976
  if (!el.firstChild) {
@@ -3089,7 +3112,7 @@ var AutoComplete = React.forwardRef(function (_ref, outerRef) {
3089
3112
  };
3090
3113
 
3091
3114
  var shouldFocusInput = useToggle();
3092
- var recentlyAdded = useRefState();
3115
+ var recentlyAdded = useRefState(false);
3093
3116
  useEffect(function () {
3094
3117
  if (autoFocus) {
3095
3118
  focusInput();
@@ -3196,7 +3219,12 @@ var AutoComplete = React.forwardRef(function (_ref, outerRef) {
3196
3219
 
3197
3220
  useEffect(function () {
3198
3221
  if (!lastIsEditable) {
3199
- setFormValue(_without([_cancelEditable.current], formValue).concat(""));
3222
+ if (recentlyAdded.get() !== _cancelEditable.current) {
3223
+ setFormValue(_without([_cancelEditable.current], formValue).concat(""));
3224
+ } else {
3225
+ setLocalFormValue(_last(formValue) === "" ? formValue : formValue.concat(""));
3226
+ }
3227
+
3200
3228
  _cancelEditable.current = null;
3201
3229
  }
3202
3230
  }, [lastIsEditable]);
@@ -3259,8 +3287,8 @@ var AutoComplete = React.forwardRef(function (_ref, outerRef) {
3259
3287
 
3260
3288
  var addValue = function addValue(value, fromBlur) {
3261
3289
  var index = Math.max(lastIsEditable ? getValue().length - 1 : getValue().length, 0);
3290
+ recentlyAdded.set((formValue === null || formValue === void 0 ? void 0 : formValue[index]) || "");
3262
3291
  setFormValue(_flow(_set([index], value), _concat(_concat.placeholder, "")));
3263
- recentlyAdded.set(true);
3264
3292
 
3265
3293
  if (!fromBlur) {
3266
3294
  focusInput();
@@ -4210,7 +4238,7 @@ var EditableBox = function EditableBox(_ref17) {
4210
4238
  var internalBox = React.useRef();
4211
4239
 
4212
4240
  var resetField = function resetField() {
4213
- return path ? form.resetField([].concat(path)) : form.resetForm();
4241
+ return path ? form.resetField(_s.toPath(path)) : form.resetForm();
4214
4242
  };
4215
4243
 
4216
4244
  var _onClose = function _onClose(skipReset) {
@@ -4783,7 +4811,7 @@ var DatePicker = forwardRef(function (_ref, maybeInputRef) {
4783
4811
  }, 1);
4784
4812
  }
4785
4813
  }, [textValue]);
4786
- var isShortcutsEnabled = daysFromNowShortcuts && daysFromNowShortcuts.length > 0 || monthsFromNowShortcuts && monthsFromNowShortcuts.length > 0;
4814
+ var isShortcutsEnabled = !!daysFromNowShortcuts && daysFromNowShortcuts.length > 0 || !!monthsFromNowShortcuts && monthsFromNowShortcuts.length > 0;
4787
4815
 
4788
4816
  var setDateToNDaysFromNow = function setDateToNDaysFromNow(daysFromNow) {
4789
4817
  var date = new Date(now());
@@ -4937,7 +4965,7 @@ var DatePicker = forwardRef(function (_ref, maybeInputRef) {
4937
4965
  onClick: iconClick,
4938
4966
  icon: "calendar",
4939
4967
  className: isShortcutsEnabled ? "hasShortcuts" : ""
4940
- })), (daysFromNowShortcuts === null || daysFromNowShortcuts === void 0 ? void 0 : daysFromNowShortcuts.length) > 0 || (monthsFromNowShortcuts === null || monthsFromNowShortcuts === void 0 ? void 0 : monthsFromNowShortcuts.length) > 0 ? React.createElement(ShortcutsWrapper, null, React.createElement("span", null, "Em:"), daysFromNowShortcuts.map(function (daysFromNow) {
4968
+ })), (daysFromNowShortcuts === null || daysFromNowShortcuts === void 0 ? void 0 : daysFromNowShortcuts.length) > 0 || (monthsFromNowShortcuts === null || monthsFromNowShortcuts === void 0 ? void 0 : monthsFromNowShortcuts.length) > 0 ? React.createElement(ShortcutsWrapper, null, React.createElement("span", null, "Em:"), daysFromNowShortcuts === null || daysFromNowShortcuts === void 0 ? void 0 : daysFromNowShortcuts.map(function (daysFromNow) {
4941
4969
  return React.createElement("span", {
4942
4970
  class: "link",
4943
4971
  key: "days-from-now-".concat(daysFromNow),
@@ -4946,7 +4974,7 @@ var DatePicker = forwardRef(function (_ref, maybeInputRef) {
4946
4974
  },
4947
4975
  disabled: disabled
4948
4976
  }, daysFromNow, " dia", daysFromNow > 1 ? 's' : '');
4949
- }), monthsFromNowShortcuts.map(function (monthsFromNow) {
4977
+ }), monthsFromNowShortcuts === null || monthsFromNowShortcuts === void 0 ? void 0 : monthsFromNowShortcuts.map(function (monthsFromNow) {
4950
4978
  return React.createElement("span", {
4951
4979
  class: "link",
4952
4980
  key: "months-from-now".concat(monthsFromNow),
@@ -5709,7 +5737,7 @@ var RichTextContainer = styled.div.withConfig({
5709
5737
  var RichTextInput = styled.div.withConfig({
5710
5738
  displayName: "RichText__RichTextInput",
5711
5739
  componentId: "x8c1ig-2"
5712
- })(["width:100%;font-size:14px;outline:0;outline:none;color:", ";background:white;min-height:", "px;outline:0;padding:0.7rem;overflow:auto;border-radius:5px;b,strong{font-weight:600;}&[contenteditable=true]:empty:before{content:attr(placeholder);color:rgba(0,0,0,0.3);}", ""], getColor('gray60'), function (props) {
5740
+ })(["font-size:14px;color:", ";background:white;border-radius:5px;> div{overflow:auto;width:100%;outline:none;min-height:", "px;padding:0.7rem;}b,strong{font-weight:600;}&[contenteditable=true]:empty:before{content:attr(placeholder);color:rgba(0,0,0,0.3);}", ""], getColor('gray60'), function (props) {
5713
5741
  return props.minHeight || 78;
5714
5742
  }, function (props) {
5715
5743
  return props.disabled && "\n &[disabled] {\n background: rgba(0, 0, 0, 0.075) !important;\n opacity: 0.75 !important;\n }\n ";
@@ -5749,14 +5777,6 @@ var addHttp = function addHttp(s) {
5749
5777
  }
5750
5778
  };
5751
5779
 
5752
- var getRightMostElement = function getRightMostElement(node) {
5753
- if (node.childNodes.length) {
5754
- return getRightMostElement(node.childNodes[node.childNodes.length - 1]);
5755
- } else {
5756
- return node;
5757
- }
5758
- };
5759
-
5760
5780
  var setSelectionRange$1 = function setSelectionRange(input, selectionStart) {
5761
5781
  var selectionEnd = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
5762
5782
  if (!input) return;
@@ -5955,14 +5975,17 @@ var RichText = function RichText(_ref5) {
5955
5975
  }, raw),
5956
5976
  _useInput2 = _slicedToArray(_useInput, 3),
5957
5977
  formValue = _useInput2[0],
5958
- setFormValue = _useInput2[1],
5978
+ _setFormValue = _useInput2[1],
5959
5979
  hasErrors = _useInput2[2].hasErrors; // const tempValue = useConstant(formValue)
5960
5980
 
5961
5981
 
5962
- var _useState = useState(formValue),
5963
- _useState2 = _slicedToArray(_useState, 2),
5964
- tempValue = _useState2[0],
5965
- setTempValue = _useState2[1];
5982
+ var tempValue = useRef(formValue);
5983
+
5984
+ var setFormValue = function setFormValue(v) {
5985
+ tempValue.current = v;
5986
+
5987
+ _setFormValue(v);
5988
+ };
5966
5989
 
5967
5990
  useEffect(function () {
5968
5991
  if (value === formValue && formValue !== tempValue) {
@@ -5970,10 +5993,10 @@ var RichText = function RichText(_ref5) {
5970
5993
  }
5971
5994
  }, [path]);
5972
5995
 
5973
- var _useState3 = useState({}),
5974
- _useState4 = _slicedToArray(_useState3, 2),
5975
- activatedTools = _useState4[0],
5976
- setActivatedTools = _useState4[1];
5996
+ var _useState = useState({}),
5997
+ _useState2 = _slicedToArray(_useState, 2),
5998
+ activatedTools = _useState2[0],
5999
+ setActivatedTools = _useState2[1];
5977
6000
 
5978
6001
  var validElements = useLazyConstant(function () {
5979
6002
  return ['br', 'div', 'p', 'span'].concat(_flatten(formatTools.map(function (tool) {
@@ -6097,6 +6120,7 @@ var RichText = function RichText(_ref5) {
6097
6120
  }
6098
6121
  }
6099
6122
 
6123
+ tempValue.current = input.current.innerHTML;
6100
6124
  openDialog(nodeEl);
6101
6125
  } else {
6102
6126
  if (!isSelectionInsideRichTextEditor()) {
@@ -6230,67 +6254,24 @@ var RichText = function RichText(_ref5) {
6230
6254
  };
6231
6255
 
6232
6256
  var handleKeyDown = function handleKeyDown(e) {
6233
- if (linkJustCreated.current && e.which === 8) {
6234
- linkJustCreated.current.outerHTML = linkJustCreated.current.innerHTML;
6235
- input.current.normalize();
6236
- return;
6237
- }
6238
-
6239
- commands.set([]);
6240
- linkJustCreated.set(null);
6241
- var selectedRange = window.getSelection().getRangeAt(0);
6242
- if (!selectedRange) return;
6243
- var possibleTextNode = selectedRange.startContainer;
6244
- var node = possibleTextNode.nodeType === Node.TEXT_NODE ? possibleTextNode.parentNode : possibleTextNode;
6245
- var parent = node.closest('a,.' + JS_CLASS);
6246
-
6247
- if ((e.key === 'Enter' || e.which === 32) && parent && parent.matches('.' + JS_CLASS)) {
6248
- var range = document.createRange();
6249
- range.setEnd(selectedRange.startContainer, selectedRange.startOffset);
6250
- range.setStart(selectedRange.startContainer, 0);
6251
- traverseAndAddLink(range);
6252
- }
6253
- };
6254
-
6255
- var traverseAndAddLink = function traverseAndAddLink(range) {
6256
- var match = range.toString().match(/[\s\t\n\r]([^\s\t\n\r]*)$/);
6257
-
6258
- if (match) {
6259
- if (/^(https?:\/\/|www\.)/.test(match[1]) && linkRegex.test(match[1])) {
6260
- range.setStart(range.startContainer, match.index + 1);
6261
- range.endContainer.splitText(range.endContainer.textContent.length - 1);
6262
- var newNode = document.createElement('a');
6263
- newNode.href = match[1];
6264
- newNode.appendChild(range.extractContents());
6265
- range.insertNode(newNode);
6266
- window.getSelection().getRangeAt(0).setEndAfter(newNode);
6267
- window.getSelection().getRangeAt(0).setStartAfter(newNode);
6268
- linkJustCreated.set(newNode);
6269
- }
6270
- } else {
6271
- var previous = getPreviousElement(range.startContainer);
6272
-
6273
- if (previous) {
6274
- range.setStart(previous, 0);
6275
- traverseAndAddLink(range);
6276
- }
6277
- }
6278
- };
6279
-
6280
- var getPreviousElement = function getPreviousElement(node) {
6281
- if (node.previousSibling) {
6282
- var rightMost = getRightMostElement(node.previousSibling);
6283
-
6284
- if (rightMost.nodeType === Node.TEXT_NODE) {
6285
- return rightMost;
6286
- } else {
6287
- return getPreviousElement(rightMost);
6288
- }
6289
- } else if (node.parentNode && node !== input.current) {
6290
- return getPreviousElement(node.parentNode);
6291
- } else {
6292
- return null;
6293
- }
6257
+ /*if (linkJustCreated.current && e.which === 8) {
6258
+ linkJustCreated.current.outerHTML = linkJustCreated.current.innerHTML
6259
+ input.current.normalize()
6260
+ return
6261
+ }
6262
+ commands.set([])
6263
+ linkJustCreated.set(null)
6264
+ const selectedRange = window.getSelection().getRangeAt(0)
6265
+ if (!selectedRange) return
6266
+ const possibleTextNode = selectedRange.startContainer
6267
+ const node = possibleTextNode.nodeType === Node.TEXT_NODE ? possibleTextNode.parentNode : possibleTextNode
6268
+ const parent = node.closest('a,.'+JS_CLASS)
6269
+ if ((e.key === 'Enter' || e.which === 32) && parent && parent.matches('.'+JS_CLASS)) {
6270
+ const range = document.createRange()
6271
+ range.setEnd(selectedRange.startContainer, selectedRange.startOffset)
6272
+ range.setStart(selectedRange.startContainer, 0)
6273
+ traverseAndAddLink(range)
6274
+ }*/
6294
6275
  };
6295
6276
 
6296
6277
  var handlePasteDrop = function handlePasteDrop(e) {
@@ -6377,8 +6358,10 @@ var RichText = function RichText(_ref5) {
6377
6358
  key: "separator-" + i
6378
6359
  }) : toolButton(t);
6379
6360
  })), React.createElement(RichTextInput, {
6380
- contentEditable: !disabled,
6381
- ref: input,
6361
+ className: JS_CLASS
6362
+ }, React.createElement(ContentEditable, {
6363
+ innerRef: input,
6364
+ html: tempValue.current,
6382
6365
  onBlur: onBlur,
6383
6366
  onClick: handleClick,
6384
6367
  onKeyUp: handleKeyUp,
@@ -6386,14 +6369,10 @@ var RichText = function RichText(_ref5) {
6386
6369
  onPaste: handlePasteDrop,
6387
6370
  onDrop: handlePasteDrop,
6388
6371
  tabIndex: tabIndex,
6389
- dangerouslySetInnerHTML: {
6390
- __html: tempValue
6391
- },
6392
6372
  disabled: disabled,
6393
- className: JS_CLASS,
6394
6373
  minHeight: minHeight,
6395
6374
  placeholder: placeholder
6396
- }), locked && React.createElement(InputIcon, {
6375
+ })), locked && React.createElement(InputIcon, {
6397
6376
  icon: "lock"
6398
6377
  }), linkPopup.isOpen && React.createElement(EditLinkPopup, {
6399
6378
  input: input,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startlibs/form",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Startlibs Form",
5
5
  "main": "lib/index.js",
6
6
  "sideEffects": false,