ResizableTextArea.js
7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import ResizeObserver from 'rc-resize-observer';
import omit from 'omit.js';
import classNames from 'classnames';
import calculateNodeHeight from './calculateNodeHeight'; // eslint-disable-next-line @typescript-eslint/naming-convention
var RESIZE_STATUS;
(function (RESIZE_STATUS) {
RESIZE_STATUS[RESIZE_STATUS["NONE"] = 0] = "NONE";
RESIZE_STATUS[RESIZE_STATUS["RESIZING"] = 1] = "RESIZING";
RESIZE_STATUS[RESIZE_STATUS["RESIZED"] = 2] = "RESIZED";
})(RESIZE_STATUS || (RESIZE_STATUS = {}));
var ResizableTextArea = /*#__PURE__*/function (_React$Component) {
_inherits(ResizableTextArea, _React$Component);
var _super = _createSuper(ResizableTextArea);
function ResizableTextArea(props) {
var _this;
_classCallCheck(this, ResizableTextArea);
_this = _super.call(this, props);
_this.saveTextArea = function (textArea) {
_this.textArea = textArea;
};
_this.handleResize = function (size) {
var resizeStatus = _this.state.resizeStatus;
var _this$props = _this.props,
autoSize = _this$props.autoSize,
onResize = _this$props.onResize;
if (resizeStatus !== RESIZE_STATUS.NONE) {
return;
}
if (typeof onResize === 'function') {
onResize(size);
}
if (autoSize) {
_this.resizeOnNextFrame();
}
};
_this.resizeOnNextFrame = function () {
cancelAnimationFrame(_this.nextFrameActionId);
_this.nextFrameActionId = requestAnimationFrame(_this.resizeTextarea);
};
_this.resizeTextarea = function () {
var autoSize = _this.props.autoSize;
if (!autoSize || !_this.textArea) {
return;
}
var minRows = autoSize.minRows,
maxRows = autoSize.maxRows;
var textareaStyles = calculateNodeHeight(_this.textArea, false, minRows, maxRows);
_this.setState({
textareaStyles: textareaStyles,
resizeStatus: RESIZE_STATUS.RESIZING
}, function () {
cancelAnimationFrame(_this.resizeFrameId);
_this.resizeFrameId = requestAnimationFrame(function () {
_this.setState({
resizeStatus: RESIZE_STATUS.RESIZED
}, function () {
_this.resizeFrameId = requestAnimationFrame(function () {
_this.setState({
resizeStatus: RESIZE_STATUS.NONE
});
_this.fixFirefoxAutoScroll();
});
});
});
});
};
_this.renderTextArea = function () {
var _this$props2 = _this.props,
_this$props2$prefixCl = _this$props2.prefixCls,
prefixCls = _this$props2$prefixCl === void 0 ? 'rc-textarea' : _this$props2$prefixCl,
autoSize = _this$props2.autoSize,
onResize = _this$props2.onResize,
className = _this$props2.className,
disabled = _this$props2.disabled;
var _this$state = _this.state,
textareaStyles = _this$state.textareaStyles,
resizeStatus = _this$state.resizeStatus;
var otherProps = omit(_this.props, ['prefixCls', 'onPressEnter', 'autoSize', 'defaultValue', 'onResize']);
var cls = classNames(prefixCls, className, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled)); // Fix https://github.com/ant-design/ant-design/issues/6776
// Make sure it could be reset when using form.getFieldDecorator
if ('value' in otherProps) {
otherProps.value = otherProps.value || '';
}
var style = _objectSpread(_objectSpread(_objectSpread({}, _this.props.style), textareaStyles), resizeStatus === RESIZE_STATUS.RESIZING ? // React will warning when mix `overflow` & `overflowY`.
// We need to define this separately.
{
overflowX: 'hidden',
overflowY: 'hidden'
} : null);
return React.createElement(ResizeObserver, {
onResize: _this.handleResize,
disabled: !(autoSize || onResize)
}, React.createElement("textarea", Object.assign({}, otherProps, {
className: cls,
style: style,
ref: _this.saveTextArea
})));
};
_this.state = {
textareaStyles: {},
resizeStatus: RESIZE_STATUS.NONE
};
return _this;
}
_createClass(ResizableTextArea, [{
key: "componentDidMount",
value: function componentDidMount() {
this.resizeTextarea();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
// Re-render with the new content then recalculate the height as required.
if (prevProps.value !== this.props.value) {
this.resizeTextarea();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
cancelAnimationFrame(this.nextFrameActionId);
cancelAnimationFrame(this.resizeFrameId);
} // https://github.com/ant-design/ant-design/issues/21870
}, {
key: "fixFirefoxAutoScroll",
value: function fixFirefoxAutoScroll() {
try {
if (document.activeElement === this.textArea) {
var currentStart = this.textArea.selectionStart;
var currentEnd = this.textArea.selectionEnd;
this.textArea.setSelectionRange(currentStart, currentEnd);
}
} catch (e) {// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
}
}, {
key: "render",
value: function render() {
return this.renderTextArea();
}
}]);
return ResizableTextArea;
}(React.Component);
export default ResizableTextArea;