react-input-emoji 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useRef, useCallback, useImperativeHandle, useEffect, forwardRef, memo, useMemo, useState } from 'react';
1
+ import React, { useRef, useCallback, useImperativeHandle, useEffect, forwardRef, useMemo, memo, useState } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
 
4
4
  function styleInject(css, ref) {
@@ -156,6 +156,7 @@ function handlePasteHtmlAtCaret(html) {
156
156
  if (window.getSelection) {
157
157
  // IE9 and non-IE
158
158
  sel = window.getSelection();
159
+ if (sel === null) return;
159
160
 
160
161
  if (sel.getRangeAt && sel.rangeCount) {
161
162
  range = sel.getRangeAt(0);
@@ -251,7 +252,7 @@ function replaceAllHtmlToString(html) {
251
252
  /**
252
253
  * @typedef {Object} Props
253
254
  * @property {React.Ref<any>} ref
254
- * @property {React.MutableRefObject<import('../text-input').Ref>} textInputRef
255
+ * @property {React.MutableRefObject<import('../text-input').Ref | null>} textInputRef
255
256
  * @property {(value: string) => void} setValue
256
257
  * @property {() => void} emitChange
257
258
  */
@@ -281,10 +282,14 @@ function useExpose({
281
282
  },
282
283
 
283
284
  focus: () => {
285
+ if (textInputRef.current === null) return;
284
286
  textInputRef.current.focus();
285
287
  },
286
288
  blur: () => {
287
- sanitize(textInputRef.current.html);
289
+ if (textInputRef.current !== null) {
290
+ sanitize(textInputRef.current.html);
291
+ }
292
+
288
293
  emitChange();
289
294
  }
290
295
  }));
@@ -294,16 +299,17 @@ function useExpose({
294
299
 
295
300
  /**
296
301
  * useEmit
297
- * @param {React.MutableRefObject<import('../text-input').Ref>} textInputRef
302
+ * @param {React.MutableRefObject<import('../text-input').Ref | null>} textInputRef
298
303
  * @param {(size: {width: number, height: number}) => void} onResize
299
304
  * @param {(text: string) => void} onChange
300
305
  */
301
306
 
302
307
  function useEmit(textInputRef, onResize, onChange) {
308
+ /** @type {React.MutableRefObject<{width: number; height: number} | null>} */
303
309
  const currentSizeRef = useRef(null);
304
310
  const onChangeFn = useRef(onChange);
305
311
  const checkAndEmitResize = useCallback(() => {
306
- if (textInputRef.current) {
312
+ if (textInputRef.current !== null) {
307
313
  const currentSize = currentSizeRef.current;
308
314
  const nextSize = textInputRef.current.size;
309
315
 
@@ -337,7 +343,7 @@ function useEmit(textInputRef, onResize, onChange) {
337
343
  * @property {(event: React.KeyboardEvent) => void} onKeyDown
338
344
  * @property {(event: React.KeyboardEvent) => void} onKeyUp
339
345
  * @property {() => void} onFocus
340
- * @property {() => void=} onBlur
346
+ * @property {() => void} onBlur
341
347
  * @property {(sanitizedText: string) => void=} onChange
342
348
  * @property {(event: React.KeyboardEvent) => void} onArrowUp
343
349
  * @property {(event: React.KeyboardEvent) => void} onArrowDown
@@ -374,40 +380,63 @@ const TextInput = ({
374
380
  }, ref) => {
375
381
  useImperativeHandle(ref, () => ({
376
382
  appendContent: html => {
377
- textInputRef.current.focus();
383
+ if (textInputRef.current) {
384
+ textInputRef.current.focus();
385
+ }
386
+
378
387
  handlePasteHtmlAtCaret(html);
379
- textInputRef.current.focus();
380
388
 
381
- if (textInputRef.current.innerHTML.trim() === "") {
389
+ if (textInputRef.current) {
390
+ textInputRef.current.focus();
391
+ }
392
+
393
+ if (textInputRef.current && placeholderRef.current && textInputRef.current.innerHTML.trim() === "") {
382
394
  placeholderRef.current.style.visibility = "visible";
383
- } else {
395
+ } else if (placeholderRef.current) {
384
396
  placeholderRef.current.style.visibility = "hidden";
385
397
  }
386
398
 
387
- onChange(textInputRef.current.innerHTML);
399
+ if (textInputRef.current && typeof onChange === 'function') {
400
+ onChange(textInputRef.current.innerHTML);
401
+ }
388
402
  },
389
403
 
390
404
  set html(value) {
391
- textInputRef.current.innerHTML = value;
405
+ if (textInputRef.current) {
406
+ textInputRef.current.innerHTML = value;
407
+ }
392
408
 
393
- if (value.trim() === "") {
394
- placeholderRef.current.style.visibility = "visible";
395
- } else {
396
- placeholderRef.current.style.visibility = "hidden";
409
+ if (placeholderRef.current) {
410
+ if (value.trim() === "") {
411
+ placeholderRef.current.style.visibility = "visible";
412
+ } else {
413
+ placeholderRef.current.style.visibility = "hidden";
414
+ }
397
415
  }
398
416
 
399
- onChange(textInputRef.current.innerHTML);
417
+ if (typeof onChange === 'function' && textInputRef.current) {
418
+ onChange(textInputRef.current.innerHTML);
419
+ }
400
420
  },
401
421
 
402
422
  get html() {
423
+ if (!textInputRef.current) return '';
403
424
  return textInputRef.current.innerHTML;
404
425
  },
405
426
 
406
427
  get text() {
428
+ if (!textInputRef.current) return '';
407
429
  return textInputRef.current.innerText;
408
430
  },
409
431
 
410
432
  get size() {
433
+ if (!textInputRef.current) {
434
+ return {
435
+ width: 0,
436
+ height: 0
437
+ };
438
+ }
439
+
411
440
  return {
412
441
  width: textInputRef.current.offsetWidth,
413
442
  height: textInputRef.current.offsetHeight
@@ -415,14 +444,15 @@ const TextInput = ({
415
444
  },
416
445
 
417
446
  focus() {
447
+ if (!textInputRef.current) return;
418
448
  textInputRef.current.focus();
419
449
  }
420
450
 
421
451
  }));
422
- /** @type {React.MutableRefObject<HTMLDivElement>} */
452
+ /** @type {React.MutableRefObject<HTMLDivElement | null>} */
423
453
 
424
454
  const placeholderRef = useRef(null);
425
- /** @type {React.MutableRefObject<HTMLDivElement>} */
455
+ /** @type {React.MutableRefObject<HTMLDivElement | null>} */
426
456
 
427
457
  const textInputRef = useRef(null);
428
458
  /**
@@ -438,7 +468,7 @@ const TextInput = ({
438
468
  } else if (event.key === "ArrowDown") {
439
469
  props.onArrowDown(event);
440
470
  } else {
441
- if (event.key.length === 1) {
471
+ if (event.key.length === 1 && placeholderRef.current) {
442
472
  placeholderRef.current.style.visibility = "hidden";
443
473
  }
444
474
  }
@@ -458,18 +488,20 @@ const TextInput = ({
458
488
 
459
489
 
460
490
  function handleKeyUp(event) {
461
- var _input$innerText;
462
-
463
491
  props.onKeyUp(event);
464
492
  const input = textInputRef.current;
465
493
 
466
- if (((_input$innerText = input.innerText) === null || _input$innerText === void 0 ? void 0 : _input$innerText.trim()) === "") {
467
- placeholderRef.current.style.visibility = "visible";
468
- } else {
469
- placeholderRef.current.style.visibility = "hidden";
494
+ if (placeholderRef.current) {
495
+ if (input?.innerText?.trim() === "") {
496
+ placeholderRef.current.style.visibility = "visible";
497
+ } else {
498
+ placeholderRef.current.style.visibility = "hidden";
499
+ }
470
500
  }
471
501
 
472
- onChange(textInputRef.current.innerHTML);
502
+ if (typeof onChange === 'function' && textInputRef.current) {
503
+ onChange(textInputRef.current.innerHTML);
504
+ }
473
505
  }
474
506
 
475
507
  return /*#__PURE__*/React.createElement("div", {
@@ -49301,23 +49333,20 @@ function _defineProperties(target, props) {
49301
49333
  function _createClass(Constructor, protoProps, staticProps) {
49302
49334
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
49303
49335
  if (staticProps) _defineProperties(Constructor, staticProps);
49336
+ Object.defineProperty(Constructor, "prototype", {
49337
+ writable: false
49338
+ });
49304
49339
  return Constructor;
49305
49340
  }
49306
49341
 
49307
49342
  function _typeof(obj) {
49308
49343
  "@babel/helpers - typeof";
49309
49344
 
49310
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
49311
- _typeof = function _typeof(obj) {
49312
- return typeof obj;
49313
- };
49314
- } else {
49315
- _typeof = function _typeof(obj) {
49316
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
49317
- };
49318
- }
49319
-
49320
- return _typeof(obj);
49345
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
49346
+ return typeof obj;
49347
+ } : function (obj) {
49348
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
49349
+ }, _typeof(obj);
49321
49350
  }
49322
49351
 
49323
49352
  var mapping = {
@@ -49407,8 +49436,8 @@ var stringFromCodePoint = _String.fromCodePoint || function stringFromCodePoint(
49407
49436
  codePoint > 0x10ffff || // not a valid Unicode code point
49408
49437
  Math.floor(codePoint) != codePoint // not an integer
49409
49438
  ) {
49410
- throw RangeError('Invalid code point: ' + codePoint);
49411
- }
49439
+ throw RangeError('Invalid code point: ' + codePoint);
49440
+ }
49412
49441
 
49413
49442
  if (codePoint <= 0xffff) {
49414
49443
  // BMP code point
@@ -50011,7 +50040,7 @@ function _defineProperty(obj, key, value) {
50011
50040
  }
50012
50041
 
50013
50042
  function _extends() {
50014
- _extends = Object.assign || function (target) {
50043
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
50015
50044
  for (var i = 1; i < arguments.length; i++) {
50016
50045
  var source = arguments[i];
50017
50046
 
@@ -50024,7 +50053,6 @@ function _extends() {
50024
50053
 
50025
50054
  return target;
50026
50055
  };
50027
-
50028
50056
  return _extends.apply(this, arguments);
50029
50057
  }
50030
50058
 
@@ -50039,24 +50067,25 @@ function _assertThisInitialized(self) {
50039
50067
  function _possibleConstructorReturn(self, call) {
50040
50068
  if (call && (_typeof(call) === "object" || typeof call === "function")) {
50041
50069
  return call;
50070
+ } else if (call !== void 0) {
50071
+ throw new TypeError("Derived constructors may only return object or undefined");
50042
50072
  }
50043
50073
 
50044
50074
  return _assertThisInitialized(self);
50045
50075
  }
50046
50076
 
50047
50077
  function _getPrototypeOf(o) {
50048
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
50078
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
50049
50079
  return o.__proto__ || Object.getPrototypeOf(o);
50050
50080
  };
50051
50081
  return _getPrototypeOf(o);
50052
50082
  }
50053
50083
 
50054
50084
  function _setPrototypeOf(o, p) {
50055
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
50085
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
50056
50086
  o.__proto__ = p;
50057
50087
  return o;
50058
50088
  };
50059
-
50060
50089
  return _setPrototypeOf(o, p);
50061
50090
  }
50062
50091
 
@@ -50072,6 +50101,9 @@ function _inherits(subClass, superClass) {
50072
50101
  configurable: true
50073
50102
  }
50074
50103
  });
50104
+ Object.defineProperty(subClass, "prototype", {
50105
+ writable: false
50106
+ });
50075
50107
  if (superClass) _setPrototypeOf(subClass, superClass);
50076
50108
  }
50077
50109
 
@@ -52606,7 +52638,7 @@ function getElementWithFocus() {
52606
52638
 
52607
52639
  function getSelectionStart() {
52608
52640
  const node = document.getSelection().anchorNode;
52609
- return (node === null || node === void 0 ? void 0 : node.nodeType) == 3 ? node : null;
52641
+ return node?.nodeType == 3 ? node : null;
52610
52642
  }
52611
52643
 
52612
52644
  // @ts-check
@@ -52651,11 +52683,9 @@ function useMention(searchMention) {
52651
52683
  /** @type {(event: React.KeyboardEvent) => void} */
52652
52684
 
52653
52685
  const onKeyUp = useCallback(async event => {
52654
- var _getElementWithFocus;
52655
-
52656
52686
  if (typeof searchMention !== "function") return;
52657
52687
 
52658
- if (event.key === "Backspace" && (_getElementWithFocus = getElementWithFocus()) !== null && _getElementWithFocus !== void 0 && _getElementWithFocus.element.parentElement.hasAttribute("data-mention-id")) {
52688
+ if (event.key === "Backspace" && getElementWithFocus()?.element.parentElement.hasAttribute("data-mention-id")) {
52659
52689
  const elementWithFocus = getElementWithFocus();
52660
52690
  elementWithFocus.element.parentElement.remove();
52661
52691
  } else if (!["ArrowUp", "ArrowDown", "Esc", "Escape"].includes(event.key)) {
@@ -53006,7 +53036,8 @@ function useEventListeners() {
53006
53036
  arrowUp: createObserver(),
53007
53037
  arrowDown: createObserver(),
53008
53038
  enter: createObserver(),
53009
- focus: createObserver()
53039
+ focus: createObserver(),
53040
+ blur: createObserver()
53010
53041
  }), []);
53011
53042
  /**
53012
53043
  * @template {keyof TextInputListeners} T, K
@@ -53072,6 +53103,7 @@ function usePollute() {
53072
53103
  * @property {(size: {width: number, height: number}) => void} onResize
53073
53104
  * @property {() => void} onClick
53074
53105
  * @property {() => void} onFocus
53106
+ * @property {() => void=} onBlur
53075
53107
  * @property {number} maxLength
53076
53108
  * @property {boolean} keepOpened
53077
53109
  * @property {(event: KeyboardEvent) => void} onKeyDown
@@ -53101,6 +53133,7 @@ function InputEmoji(props, ref) {
53101
53133
  onResize,
53102
53134
  onClick,
53103
53135
  onFocus,
53136
+ onBlur,
53104
53137
  onKeyDown,
53105
53138
  theme,
53106
53139
  cleanOnEnter,
@@ -53119,7 +53152,7 @@ function InputEmoji(props, ref) {
53119
53152
  fontSize,
53120
53153
  fontFamily
53121
53154
  } = props;
53122
- /** @type {React.MutableRefObject<import('./text-input').Ref>} */
53155
+ /** @type {React.MutableRefObject<import('./text-input').Ref | null>} */
53123
53156
 
53124
53157
  const textInputRef = useRef(null);
53125
53158
  const {
@@ -53136,6 +53169,7 @@ function InputEmoji(props, ref) {
53136
53169
  pollute
53137
53170
  } = usePollute();
53138
53171
  const updateHTML = useCallback((nextValue = "") => {
53172
+ if (textInputRef.current === null) return;
53139
53173
  textInputRef.current.html = replaceAllTextEmojis(nextValue);
53140
53174
  sanitizedTextRef.current = nextValue;
53141
53175
  }, [sanitizedTextRef]);
@@ -53164,11 +53198,11 @@ function InputEmoji(props, ref) {
53164
53198
  * @return {boolean}
53165
53199
  */
53166
53200
  function handleKeydown(event) {
53167
- if (typeof maxLength !== "undefined" && event.key !== "Backspace" && totalCharacters(textInputRef.current) >= maxLength) {
53201
+ if (typeof maxLength !== "undefined" && event.key !== "Backspace" && textInputRef.current !== null && totalCharacters(textInputRef.current) >= maxLength) {
53168
53202
  event.preventDefault();
53169
53203
  }
53170
53204
 
53171
- if (event.key === "Enter") {
53205
+ if (event.key === "Enter" && textInputRef.current) {
53172
53206
  event.preventDefault();
53173
53207
  const text = sanitize(textInputRef.current.html);
53174
53208
  emitChange(sanitizedTextRef.current);
@@ -53217,6 +53251,19 @@ function InputEmoji(props, ref) {
53217
53251
  unsubscribe();
53218
53252
  };
53219
53253
  }, [addEventListener, onClick, onFocus]);
53254
+ useEffect(() => {
53255
+ /** */
53256
+ function handleBlur() {
53257
+ if (typeof onBlur === 'function') {
53258
+ onBlur();
53259
+ }
53260
+ }
53261
+
53262
+ const unsubscribe = addEventListener('blur', handleBlur);
53263
+ return () => {
53264
+ unsubscribe();
53265
+ };
53266
+ }, [addEventListener, onBlur]);
53220
53267
  /**
53221
53268
  *
53222
53269
  * @param {string} html
@@ -53233,11 +53280,13 @@ function InputEmoji(props, ref) {
53233
53280
 
53234
53281
 
53235
53282
  function appendContent(html) {
53236
- if (typeof maxLength !== "undefined" && totalCharacters(textInputRef.current) >= maxLength) {
53283
+ if (typeof maxLength !== "undefined" && textInputRef.current !== null && totalCharacters(textInputRef.current) >= maxLength) {
53237
53284
  return;
53238
53285
  }
53239
53286
 
53240
- textInputRef.current.appendContent(html);
53287
+ if (textInputRef.current !== null) {
53288
+ textInputRef.current.appendContent(html);
53289
+ }
53241
53290
  }
53242
53291
  /**
53243
53292
  * Handle copy of current selected text
@@ -53277,6 +53326,7 @@ function InputEmoji(props, ref) {
53277
53326
  ref: textInputRef,
53278
53327
  onCopy: handleCopy,
53279
53328
  onPaste: handlePaste,
53329
+ onBlur: listeners.blur.publish,
53280
53330
  onFocus: listeners.focus.publish,
53281
53331
  onArrowUp: listeners.arrowUp.publish,
53282
53332
  onArrowDown: listeners.arrowDown.publish,
@@ -53319,5 +53369,5 @@ InputEmojiWithRef.defaultProps = {
53319
53369
  customEmojis: []
53320
53370
  };
53321
53371
 
53322
- export default InputEmojiWithRef;
53372
+ export { InputEmojiWithRef as default };
53323
53373
  //# sourceMappingURL=index.es.js.map