react-input-emoji 5.0.2 → 5.1.1

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -46,7 +46,7 @@ export default function Example() {
46
46
  ## Props
47
47
 
48
48
  | Prop | Type | Default | Description |
49
- |----------------|----------|------------------|--------------------------------------------------------------------------------------------------------------------------|
49
+ | -------------- | -------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------ |
50
50
  | `value` | string | "" | The input value. |
51
51
  | `onChange` | function | - | This function is called when the value of the input changes. The first argument is the current value. |
52
52
  | `onResize` | function | - | This function is called when the width or the height of the input changes. The first argument is the current size value. |
@@ -62,6 +62,7 @@ export default function Example() {
62
62
  | `borderColor` | string | "#EAEAEA" | The border color of the input container. |
63
63
  | `fontSize` | number | 15 | The font size of the placeholder and input container. |
64
64
  | `fontFamily` | string | "sans-serif" | The font family of the placeholder and input container. |
65
+ | `shouldReturn` | boolean | - | Allows the user to use the `Shift + Enter` or `Ctrl + Enter` keyboard shortcut to create a new line. |
65
66
 
66
67
  ## License
67
68
 
package/dist/index.es.js CHANGED
@@ -732,6 +732,43 @@ function totalCharacters(_ref2) {
732
732
  var emojisCount = (html.match(/<img/g) || []).length;
733
733
  return textCount + emojisCount;
734
734
  } // eslint-disable-next-line valid-jsdoc
735
+ /**
736
+ * Set caret to the end of text value
737
+ * @param {React.MutableRefObject<HTMLDivElement| null>} input
738
+ */
739
+
740
+ function moveCaretToEnd(input) {
741
+ var range;
742
+ var selection;
743
+
744
+ if (document.createRange && input.current) {
745
+ range = document.createRange();
746
+ range.selectNodeContents(input.current);
747
+ range.collapse(false);
748
+ selection = window.getSelection();
749
+
750
+ if (selection) {
751
+ selection.removeAllRanges();
752
+ selection.addRange(range);
753
+ }
754
+ }
755
+ }
756
+ /**
757
+ *
758
+ * @param {HTMLDivElement} inputDiv
759
+ * @return {string}
760
+ */
761
+
762
+ function removeHtmlExceptBr(inputDiv) {
763
+ var temp = inputDiv.innerHTML.replace(/<br\s*\/?>/gi, '[BR]'); // temporarily replace <br> with placeholder
764
+
765
+ var stripped = temp.replace(/<[^>]+>/g, ''); // strip all html tags
766
+
767
+ var _final = stripped.replace(/\[BR\]/gi, '</br>'); // replace placeholders with <br>
768
+
769
+
770
+ return _final;
771
+ }
735
772
 
736
773
  // @ts-check
737
774
  /**
@@ -739,9 +776,11 @@ function totalCharacters(_ref2) {
739
776
  */
740
777
  // eslint-disable-next-line valid-jsdoc
741
778
 
742
- /** */
779
+ /**
780
+ * @param {boolean} shouldReturn
781
+ */
743
782
 
744
- function useSanitize() {
783
+ function useSanitize(shouldReturn) {
745
784
  /** @type {React.MutableRefObject<SanitizeFn[]>} */
746
785
  var sanitizeFnsRef = useRef([]);
747
786
  var sanitizedTextRef = useRef("");
@@ -756,7 +795,7 @@ function useSanitize() {
756
795
  var result = sanitizeFnsRef.current.reduce(function (acc, fn) {
757
796
  return fn(acc);
758
797
  }, html);
759
- result = replaceAllHtmlToString(result);
798
+ result = replaceAllHtmlToString(result, shouldReturn);
760
799
  sanitizedTextRef.current = result;
761
800
  return result;
762
801
  }, []);
@@ -769,13 +808,21 @@ function useSanitize() {
769
808
  /**
770
809
  *
771
810
  * @param {string} html
811
+ * @param {boolean} shouldReturn
772
812
  * @return {string}
773
813
  */
774
814
 
775
- function replaceAllHtmlToString(html) {
815
+ function replaceAllHtmlToString(html, shouldReturn) {
776
816
  var container = document.createElement("div");
777
817
  container.innerHTML = html;
778
- var text = container.innerText || ""; // remove all ↵ for safari
818
+ var text;
819
+
820
+ if (shouldReturn) {
821
+ text = removeHtmlExceptBr(container);
822
+ } else {
823
+ text = container.innerText || "";
824
+ } // remove all ↵ for safari
825
+
779
826
 
780
827
  text = text.replace(/\n/gi, "");
781
828
  return text;
@@ -883,6 +930,7 @@ var _excluded = ["placeholder", "style", "tabIndex", "className", "onChange"];
883
930
  * @property {(event: React.KeyboardEvent) => void} onArrowUp
884
931
  * @property {(event: React.KeyboardEvent) => void} onArrowDown
885
932
  * @property {(event: React.KeyboardEvent) => void} onEnter
933
+ * @property {boolean} shouldReturn
886
934
  * @property {(event: React.ClipboardEvent) => void} onCopy
887
935
  * @property {(event: React.ClipboardEvent) => void} onPaste
888
936
  * @property {string} placeholder
@@ -997,6 +1045,16 @@ var TextInput = function TextInput(_ref, ref) {
997
1045
  */
998
1046
 
999
1047
  function handleKeyDown(event) {
1048
+ if (event.key === "Enter" && (event.shiftKey === true || event.ctrlKey === true) && props.shouldReturn) {
1049
+ event.preventDefault();
1050
+
1051
+ if (textInputRef.current) {
1052
+ textInputRef.current.innerHTML = "".concat(textInputRef.current.innerHTML, "</br></br>");
1053
+ moveCaretToEnd(textInputRef);
1054
+ return;
1055
+ }
1056
+ }
1057
+
1000
1058
  if (event.key === "Enter") {
1001
1059
  props.onEnter(event);
1002
1060
  } else if (event.key === "ArrowUp") {
@@ -4720,6 +4778,7 @@ function usePollute() {
4720
4778
  * @property {() => void} onClick
4721
4779
  * @property {() => void} onFocus
4722
4780
  * @property {() => void=} onBlur
4781
+ * @property {boolean} shouldReturn
4723
4782
  * @property {number} maxLength
4724
4783
  * @property {boolean} keepOpened
4725
4784
  * @property {(event: KeyboardEvent) => void} onKeyDown
@@ -4746,6 +4805,7 @@ function usePollute() {
4746
4805
  function InputEmoji(props, ref) {
4747
4806
  var onChange = props.onChange,
4748
4807
  onEnter = props.onEnter,
4808
+ shouldReturn = props.shouldReturn,
4749
4809
  onResize = props.onResize,
4750
4810
  onClick = props.onClick,
4751
4811
  onFocus = props.onFocus,
@@ -4775,7 +4835,7 @@ function InputEmoji(props, ref) {
4775
4835
  addEventListener = _useEventListeners.addEventListener,
4776
4836
  listeners = _useEventListeners.listeners;
4777
4837
 
4778
- var _useSanitize = useSanitize(),
4838
+ var _useSanitize = useSanitize(props.shouldReturn),
4779
4839
  addSanitizeFn = _useSanitize.addSanitizeFn,
4780
4840
  sanitize = _useSanitize.sanitize,
4781
4841
  sanitizedTextRef = _useSanitize.sanitizedTextRef;
@@ -4943,6 +5003,7 @@ function InputEmoji(props, ref) {
4943
5003
  ref: textInputRef,
4944
5004
  onCopy: handleCopy,
4945
5005
  onPaste: handlePaste,
5006
+ shouldReturn: shouldReturn,
4946
5007
  onBlur: listeners.blur.publish,
4947
5008
  onFocus: listeners.focus.publish,
4948
5009
  onArrowUp: listeners.arrowUp.publish,
@@ -4984,6 +5045,7 @@ InputEmojiWithRef.defaultProps = {
4984
5045
  fontSize: 15,
4985
5046
  fontFamily: "sans-serif",
4986
5047
  tabIndex: 0,
5048
+ shouldReturn: false,
4987
5049
  customEmojis: []
4988
5050
  };
4989
5051