ResizableTextArea.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import { createVNode as _createVNode } from "vue";
  4. import { onMounted, getCurrentInstance, watch, onBeforeUnmount, ref, nextTick, defineComponent, withDirectives } from 'vue';
  5. import ResizeObserver from '../vc-resize-observer';
  6. import classNames from '../_util/classNames';
  7. import calculateNodeHeight from './calculateNodeHeight';
  8. import raf from '../_util/raf';
  9. import warning from '../_util/warning';
  10. import antInput from '../_util/antInputDirective';
  11. import omit from '../_util/omit';
  12. import { textAreaProps } from './inputProps';
  13. var RESIZE_STATUS_NONE = 0;
  14. var RESIZE_STATUS_RESIZING = 1;
  15. var RESIZE_STATUS_RESIZED = 2;
  16. var ResizableTextArea = defineComponent({
  17. compatConfig: {
  18. MODE: 3
  19. },
  20. name: 'ResizableTextArea',
  21. inheritAttrs: false,
  22. props: textAreaProps(),
  23. setup: function setup(props, _ref) {
  24. var attrs = _ref.attrs,
  25. emit = _ref.emit,
  26. expose = _ref.expose;
  27. var nextFrameActionId;
  28. var resizeFrameId;
  29. var textAreaRef = ref();
  30. var textareaStyles = ref({});
  31. var resizeStatus = ref(RESIZE_STATUS_NONE);
  32. onBeforeUnmount(function () {
  33. raf.cancel(nextFrameActionId);
  34. raf.cancel(resizeFrameId);
  35. });
  36. // https://github.com/ant-design/ant-design/issues/21870
  37. var fixFirefoxAutoScroll = function fixFirefoxAutoScroll() {
  38. try {
  39. if (document.activeElement === textAreaRef.value) {
  40. var currentStart = textAreaRef.value.selectionStart;
  41. var currentEnd = textAreaRef.value.selectionEnd;
  42. textAreaRef.value.setSelectionRange(currentStart, currentEnd);
  43. }
  44. } catch (e) {
  45. // Fix error in Chrome:
  46. // Failed to read the 'selectionStart' property from 'HTMLInputElement'
  47. // http://stackoverflow.com/q/21177489/3040605
  48. }
  49. };
  50. var resizeTextarea = function resizeTextarea() {
  51. var autoSize = props.autoSize || props.autosize;
  52. if (!autoSize || !textAreaRef.value) {
  53. return;
  54. }
  55. var minRows = autoSize.minRows,
  56. maxRows = autoSize.maxRows;
  57. textareaStyles.value = calculateNodeHeight(textAreaRef.value, false, minRows, maxRows);
  58. resizeStatus.value = RESIZE_STATUS_RESIZING;
  59. raf.cancel(resizeFrameId);
  60. resizeFrameId = raf(function () {
  61. resizeStatus.value = RESIZE_STATUS_RESIZED;
  62. resizeFrameId = raf(function () {
  63. resizeStatus.value = RESIZE_STATUS_NONE;
  64. fixFirefoxAutoScroll();
  65. });
  66. });
  67. };
  68. var resizeOnNextFrame = function resizeOnNextFrame() {
  69. raf.cancel(nextFrameActionId);
  70. nextFrameActionId = raf(resizeTextarea);
  71. };
  72. var handleResize = function handleResize(size) {
  73. if (resizeStatus.value !== RESIZE_STATUS_NONE) {
  74. return;
  75. }
  76. emit('resize', size);
  77. var autoSize = props.autoSize || props.autosize;
  78. if (autoSize) {
  79. resizeOnNextFrame();
  80. }
  81. };
  82. warning(props.autosize === undefined, 'Input.TextArea', 'autosize is deprecated, please use autoSize instead.');
  83. var renderTextArea = function renderTextArea() {
  84. var prefixCls = props.prefixCls,
  85. autoSize = props.autoSize,
  86. autosize = props.autosize,
  87. disabled = props.disabled;
  88. var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'autoSize', 'autosize', 'defaultValue', 'allowClear', 'type', 'lazy', 'maxlength', 'valueModifiers']);
  89. var cls = classNames(prefixCls, attrs.class, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled));
  90. var style = [attrs.style, textareaStyles.value, resizeStatus.value === RESIZE_STATUS_RESIZING ? {
  91. overflowX: 'hidden',
  92. overflowY: 'hidden'
  93. } : null];
  94. var textareaProps = _objectSpread(_objectSpread(_objectSpread({}, otherProps), attrs), {}, {
  95. style: style,
  96. class: cls
  97. });
  98. if (!textareaProps.autofocus) {
  99. delete textareaProps.autofocus;
  100. }
  101. if (textareaProps.rows === 0) {
  102. delete textareaProps.rows;
  103. }
  104. return _createVNode(ResizeObserver, {
  105. "onResize": handleResize,
  106. "disabled": !(autoSize || autosize)
  107. }, {
  108. default: function _default() {
  109. return [withDirectives(_createVNode("textarea", _objectSpread(_objectSpread({}, textareaProps), {}, {
  110. "ref": textAreaRef
  111. }), null), [[antInput]])];
  112. }
  113. });
  114. };
  115. watch(function () {
  116. return props.value;
  117. }, function () {
  118. nextTick(function () {
  119. resizeTextarea();
  120. });
  121. });
  122. onMounted(function () {
  123. nextTick(function () {
  124. resizeTextarea();
  125. });
  126. });
  127. var instance = getCurrentInstance();
  128. expose({
  129. resizeTextarea: resizeTextarea,
  130. textArea: textAreaRef,
  131. instance: instance
  132. });
  133. return function () {
  134. return renderTextArea();
  135. };
  136. }
  137. });
  138. export default ResizableTextArea;