funda-ui 1.0.516 → 1.0.517
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.
- package/Input/index.js +6 -4
- package/Textarea/index.d.ts +3 -0
- package/Textarea/index.js +163 -22
- package/lib/cjs/Input/index.js +6 -4
- package/lib/cjs/Textarea/index.d.ts +3 -0
- package/lib/cjs/Textarea/index.js +163 -22
- package/lib/esm/Input/index.tsx +7 -4
- package/lib/esm/Textarea/index.tsx +21 -1
- package/lib/esm/Textarea/utils/useAutosizeTextArea.js +124 -0
- package/package.json +1 -1
package/Input/index.js
CHANGED
|
@@ -178,10 +178,6 @@ var Input = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(funct
|
|
|
178
178
|
|
|
179
179
|
//
|
|
180
180
|
onChange === null || onChange === void 0 ? void 0 : onChange(event, onComposition);
|
|
181
|
-
if (typeof onChangeCallback === 'function') {
|
|
182
|
-
var newData = onChangeCallback(event);
|
|
183
|
-
setChangedVal(newData);
|
|
184
|
-
}
|
|
185
181
|
}
|
|
186
182
|
function handleBlur(event) {
|
|
187
183
|
var el = event.target;
|
|
@@ -195,6 +191,12 @@ var Input = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(funct
|
|
|
195
191
|
|
|
196
192
|
//
|
|
197
193
|
onBlur === null || onBlur === void 0 ? void 0 : onBlur(event, onComposition);
|
|
194
|
+
|
|
195
|
+
//
|
|
196
|
+
if (typeof onChangeCallback === 'function') {
|
|
197
|
+
var newData = onChangeCallback(event);
|
|
198
|
+
setChangedVal(newData);
|
|
199
|
+
}
|
|
198
200
|
}
|
|
199
201
|
(0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(function () {
|
|
200
202
|
// update default value
|
package/Textarea/index.d.ts
CHANGED
|
@@ -17,11 +17,14 @@ interface TextareaProps extends React.ComponentPropsWithoutRef<"textarea"> {
|
|
|
17
17
|
required?: any;
|
|
18
18
|
readOnly?: any;
|
|
19
19
|
placeholder?: string;
|
|
20
|
+
autoSize?: boolean;
|
|
21
|
+
autoSizeBeginOneline?: boolean;
|
|
20
22
|
/** -- */
|
|
21
23
|
id?: string;
|
|
22
24
|
style?: React.CSSProperties;
|
|
23
25
|
tabIndex?: number;
|
|
24
26
|
[key: `data-${string}`]: string | undefined;
|
|
27
|
+
onChangeCallback?: (e: any) => void;
|
|
25
28
|
onChange?: (e: any) => void;
|
|
26
29
|
onBlur?: (e: any) => void;
|
|
27
30
|
onFocus?: (e: any) => void;
|
package/Textarea/index.js
CHANGED
|
@@ -90,25 +90,154 @@ module.exports = __WEBPACK_EXTERNAL_MODULE__787__;
|
|
|
90
90
|
var __webpack_exports__ = {};
|
|
91
91
|
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
|
|
92
92
|
(() => {
|
|
93
|
+
// ESM COMPAT FLAG
|
|
93
94
|
__webpack_require__.r(__webpack_exports__);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
|
|
96
|
+
// EXPORTS
|
|
97
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
98
|
+
"default": () => (/* binding */ src)
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// EXTERNAL MODULE: external {"root":"React","commonjs2":"react","commonjs":"react","amd":"react"}
|
|
102
|
+
var external_root_React_commonjs2_react_commonjs_react_amd_react_ = __webpack_require__(787);
|
|
103
|
+
var external_root_React_commonjs2_react_commonjs_react_amd_react_default = /*#__PURE__*/__webpack_require__.n(external_root_React_commonjs2_react_commonjs_react_amd_react_);
|
|
104
|
+
;// CONCATENATED MODULE: ./src/utils/useAutosizeTextArea.js
|
|
101
105
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
102
106
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
103
107
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
104
108
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
105
109
|
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
106
110
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
111
|
+
/**
|
|
112
|
+
* Creating a Textarea with dynamic height
|
|
113
|
+
*
|
|
114
|
+
* @usage:
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
const App = () => {
|
|
118
|
+
const [value, setValue] = useState("");
|
|
119
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
120
|
+
|
|
121
|
+
useAutosizeTextArea(el.current, value, false);
|
|
122
|
+
|
|
123
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
124
|
+
const val = evt.target?.value;
|
|
125
|
+
setValue(val);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<div className="App">
|
|
130
|
+
<textarea
|
|
131
|
+
onChange={handleChange}
|
|
132
|
+
ref={el}
|
|
133
|
+
rows={3}
|
|
134
|
+
value={value}
|
|
135
|
+
/>
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
//--------
|
|
142
|
+
|
|
143
|
+
const App = () => {
|
|
144
|
+
const [value, setValue] = useState("");
|
|
145
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
146
|
+
|
|
147
|
+
useAutosizeTextArea(el.current, value, true);
|
|
148
|
+
|
|
149
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
150
|
+
const val = evt.target?.value;
|
|
151
|
+
setValue(val);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<div className="App">
|
|
156
|
+
<textarea
|
|
157
|
+
onChange={handleChange}
|
|
158
|
+
ref={el}
|
|
159
|
+
rows={1}
|
|
160
|
+
value={value}
|
|
161
|
+
/>
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
// Updates the height of a <textarea> when the value changes.
|
|
173
|
+
var useAutosizeTextArea = function useAutosizeTextArea(el, value, autoSizeBeginOneline) {
|
|
174
|
+
var _useState = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(0),
|
|
175
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
176
|
+
defaultRowHeight = _useState2[0],
|
|
177
|
+
setDefaultRowHeight = _useState2[1];
|
|
178
|
+
var _useState3 = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(0),
|
|
179
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
180
|
+
dynamicDefaultRowHeight = _useState4[0],
|
|
181
|
+
setDynamicDefaultRowHeight = _useState4[1];
|
|
182
|
+
var _useState5 = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(false),
|
|
183
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
184
|
+
defaultRowHeightInit = _useState6[0],
|
|
185
|
+
setDefaultRowHeightInit = _useState6[1];
|
|
186
|
+
(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useEffect)(function () {
|
|
187
|
+
if (el) {
|
|
188
|
+
if (autoSizeBeginOneline) {
|
|
189
|
+
el.style.height = "0px";
|
|
190
|
+
var beginOnelineScrollHeight = el.scrollHeight;
|
|
191
|
+
el.style.height = beginOnelineScrollHeight + "px";
|
|
192
|
+
} else {
|
|
193
|
+
// initialize default row height
|
|
194
|
+
if (el.scrollHeight > 0 && !defaultRowHeightInit) {
|
|
195
|
+
setDefaultRowHeight(el.scrollHeight);
|
|
196
|
+
setDynamicDefaultRowHeight(el.scrollHeight);
|
|
197
|
+
setDefaultRowHeightInit(true);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// reset the height momentarily to get the correct scrollHeight for the textarea
|
|
201
|
+
var scrollHeight = el.scrollHeight;
|
|
202
|
+
|
|
203
|
+
// reset row height
|
|
204
|
+
if (typeof value !== 'undefined' && value.length === 0) {
|
|
205
|
+
el.style.height = dynamicDefaultRowHeight + "px";
|
|
206
|
+
|
|
207
|
+
// update default row height
|
|
208
|
+
setDefaultRowHeight(dynamicDefaultRowHeight);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// then set the height directly, outside of the render loop
|
|
212
|
+
// Trying to set this with state or a ref will product an incorrect value.
|
|
213
|
+
|
|
214
|
+
// !!! Compare initial height and changed height
|
|
215
|
+
if (scrollHeight > defaultRowHeight && defaultRowHeight > 0) {
|
|
216
|
+
el.style.height = scrollHeight + "px";
|
|
217
|
+
|
|
218
|
+
// update default row height
|
|
219
|
+
setDefaultRowHeight(scrollHeight);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}, [el, value]);
|
|
224
|
+
};
|
|
225
|
+
/* harmony default export */ const utils_useAutosizeTextArea = (useAutosizeTextArea);
|
|
226
|
+
;// CONCATENATED MODULE: ./src/index.tsx
|
|
227
|
+
var _excluded = ["wrapperClassName", "controlClassName", "cols", "rows", "disabled", "required", "placeholder", "autoSize", "autoSizeBeginOneline", "readOnly", "value", "label", "name", "id", "maxLength", "style", "tabIndex", "onChangeCallback", "onChange", "onBlur", "onFocus"];
|
|
228
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
229
|
+
function src_slicedToArray(arr, i) { return src_arrayWithHoles(arr) || src_iterableToArrayLimit(arr, i) || src_unsupportedIterableToArray(arr, i) || src_nonIterableRest(); }
|
|
230
|
+
function src_nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
231
|
+
function src_unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return src_arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return src_arrayLikeToArray(o, minLen); }
|
|
232
|
+
function src_arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
233
|
+
function src_iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
234
|
+
function src_arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
107
235
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
108
236
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
109
237
|
|
|
238
|
+
|
|
110
239
|
;
|
|
111
|
-
var Textarea = /*#__PURE__*/(0,
|
|
240
|
+
var Textarea = /*#__PURE__*/(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.forwardRef)(function (props, _ref) {
|
|
112
241
|
var wrapperClassName = props.wrapperClassName,
|
|
113
242
|
controlClassName = props.controlClassName,
|
|
114
243
|
cols = props.cols,
|
|
@@ -116,6 +245,8 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
116
245
|
disabled = props.disabled,
|
|
117
246
|
required = props.required,
|
|
118
247
|
placeholder = props.placeholder,
|
|
248
|
+
autoSize = props.autoSize,
|
|
249
|
+
autoSizeBeginOneline = props.autoSizeBeginOneline,
|
|
119
250
|
readOnly = props.readOnly,
|
|
120
251
|
value = props.value,
|
|
121
252
|
label = props.label,
|
|
@@ -124,18 +255,22 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
124
255
|
maxLength = props.maxLength,
|
|
125
256
|
style = props.style,
|
|
126
257
|
tabIndex = props.tabIndex,
|
|
258
|
+
onChangeCallback = props.onChangeCallback,
|
|
127
259
|
onChange = props.onChange,
|
|
128
260
|
onBlur = props.onBlur,
|
|
129
261
|
onFocus = props.onFocus,
|
|
130
262
|
attributes = _objectWithoutProperties(props, _excluded);
|
|
131
|
-
var uniqueID = (0,
|
|
263
|
+
var uniqueID = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useId)();
|
|
132
264
|
var idRes = id || uniqueID;
|
|
133
|
-
var rootRef = (0,
|
|
134
|
-
var valRef = (0,
|
|
135
|
-
var _useState = (0,
|
|
136
|
-
_useState2 =
|
|
265
|
+
var rootRef = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useRef)(null);
|
|
266
|
+
var valRef = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useRef)(null);
|
|
267
|
+
var _useState = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(value || ''),
|
|
268
|
+
_useState2 = src_slicedToArray(_useState, 2),
|
|
137
269
|
changedVal = _useState2[0],
|
|
138
270
|
setChangedVal = _useState2[1];
|
|
271
|
+
|
|
272
|
+
// auto size
|
|
273
|
+
utils_useAutosizeTextArea(autoSize ? valRef.current : null, autoSize ? changedVal : '', autoSizeBeginOneline);
|
|
139
274
|
function handleFocus(event) {
|
|
140
275
|
var el = event.target;
|
|
141
276
|
rootRef.current.classList.add('focus');
|
|
@@ -168,24 +303,30 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
168
303
|
|
|
169
304
|
//
|
|
170
305
|
onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
|
|
306
|
+
|
|
307
|
+
//
|
|
308
|
+
if (typeof onChangeCallback === 'function') {
|
|
309
|
+
var newData = onChangeCallback(event);
|
|
310
|
+
setChangedVal(newData);
|
|
311
|
+
}
|
|
171
312
|
}
|
|
172
|
-
(0,
|
|
313
|
+
(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useEffect)(function () {
|
|
173
314
|
// update default value
|
|
174
315
|
//--------------
|
|
175
316
|
setChangedVal(value || '');
|
|
176
317
|
}, [value]);
|
|
177
|
-
return /*#__PURE__*/
|
|
318
|
+
return /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("div", {
|
|
178
319
|
className: wrapperClassName || wrapperClassName === '' ? wrapperClassName : "mb-3 position-relative",
|
|
179
320
|
ref: rootRef
|
|
180
|
-
}, label ? /*#__PURE__*/
|
|
321
|
+
}, label ? /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("label", {
|
|
181
322
|
htmlFor: idRes,
|
|
182
323
|
className: "form-label",
|
|
183
324
|
dangerouslySetInnerHTML: {
|
|
184
325
|
__html: "".concat(label)
|
|
185
326
|
}
|
|
186
|
-
})) : null, /*#__PURE__*/
|
|
327
|
+
})) : null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("div", {
|
|
187
328
|
className: "input-group"
|
|
188
|
-
}, /*#__PURE__*/
|
|
329
|
+
}, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("textarea", _extends({
|
|
189
330
|
ref: function ref(node) {
|
|
190
331
|
valRef.current = node;
|
|
191
332
|
if (typeof _ref === 'function') {
|
|
@@ -208,15 +349,15 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
208
349
|
required: required || null,
|
|
209
350
|
readOnly: readOnly || null,
|
|
210
351
|
cols: cols || 20,
|
|
211
|
-
rows: rows ||
|
|
352
|
+
rows: autoSizeBeginOneline ? 1 : rows || 1,
|
|
212
353
|
style: style
|
|
213
|
-
}, attributes))), required ? /*#__PURE__*/
|
|
354
|
+
}, attributes))), required ? /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("span", {
|
|
214
355
|
className: "position-absolute end-0 top-0 my-2 mx-2"
|
|
215
|
-
}, /*#__PURE__*/
|
|
356
|
+
}, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("span", {
|
|
216
357
|
className: "text-danger"
|
|
217
358
|
}, "*"))) : ''));
|
|
218
359
|
});
|
|
219
|
-
/* harmony default export */ const
|
|
360
|
+
/* harmony default export */ const src = (Textarea);
|
|
220
361
|
})();
|
|
221
362
|
|
|
222
363
|
/******/ return __webpack_exports__;
|
package/lib/cjs/Input/index.js
CHANGED
|
@@ -178,10 +178,6 @@ var Input = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(funct
|
|
|
178
178
|
|
|
179
179
|
//
|
|
180
180
|
onChange === null || onChange === void 0 ? void 0 : onChange(event, onComposition);
|
|
181
|
-
if (typeof onChangeCallback === 'function') {
|
|
182
|
-
var newData = onChangeCallback(event);
|
|
183
|
-
setChangedVal(newData);
|
|
184
|
-
}
|
|
185
181
|
}
|
|
186
182
|
function handleBlur(event) {
|
|
187
183
|
var el = event.target;
|
|
@@ -195,6 +191,12 @@ var Input = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(funct
|
|
|
195
191
|
|
|
196
192
|
//
|
|
197
193
|
onBlur === null || onBlur === void 0 ? void 0 : onBlur(event, onComposition);
|
|
194
|
+
|
|
195
|
+
//
|
|
196
|
+
if (typeof onChangeCallback === 'function') {
|
|
197
|
+
var newData = onChangeCallback(event);
|
|
198
|
+
setChangedVal(newData);
|
|
199
|
+
}
|
|
198
200
|
}
|
|
199
201
|
(0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(function () {
|
|
200
202
|
// update default value
|
|
@@ -17,11 +17,14 @@ interface TextareaProps extends React.ComponentPropsWithoutRef<"textarea"> {
|
|
|
17
17
|
required?: any;
|
|
18
18
|
readOnly?: any;
|
|
19
19
|
placeholder?: string;
|
|
20
|
+
autoSize?: boolean;
|
|
21
|
+
autoSizeBeginOneline?: boolean;
|
|
20
22
|
/** -- */
|
|
21
23
|
id?: string;
|
|
22
24
|
style?: React.CSSProperties;
|
|
23
25
|
tabIndex?: number;
|
|
24
26
|
[key: `data-${string}`]: string | undefined;
|
|
27
|
+
onChangeCallback?: (e: any) => void;
|
|
25
28
|
onChange?: (e: any) => void;
|
|
26
29
|
onBlur?: (e: any) => void;
|
|
27
30
|
onFocus?: (e: any) => void;
|
|
@@ -90,25 +90,154 @@ module.exports = __WEBPACK_EXTERNAL_MODULE__787__;
|
|
|
90
90
|
var __webpack_exports__ = {};
|
|
91
91
|
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
|
|
92
92
|
(() => {
|
|
93
|
+
// ESM COMPAT FLAG
|
|
93
94
|
__webpack_require__.r(__webpack_exports__);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
|
|
96
|
+
// EXPORTS
|
|
97
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
98
|
+
"default": () => (/* binding */ src)
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// EXTERNAL MODULE: external {"root":"React","commonjs2":"react","commonjs":"react","amd":"react"}
|
|
102
|
+
var external_root_React_commonjs2_react_commonjs_react_amd_react_ = __webpack_require__(787);
|
|
103
|
+
var external_root_React_commonjs2_react_commonjs_react_amd_react_default = /*#__PURE__*/__webpack_require__.n(external_root_React_commonjs2_react_commonjs_react_amd_react_);
|
|
104
|
+
;// CONCATENATED MODULE: ./src/utils/useAutosizeTextArea.js
|
|
101
105
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
102
106
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
103
107
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
104
108
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
105
109
|
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
106
110
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
111
|
+
/**
|
|
112
|
+
* Creating a Textarea with dynamic height
|
|
113
|
+
*
|
|
114
|
+
* @usage:
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
const App = () => {
|
|
118
|
+
const [value, setValue] = useState("");
|
|
119
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
120
|
+
|
|
121
|
+
useAutosizeTextArea(el.current, value, false);
|
|
122
|
+
|
|
123
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
124
|
+
const val = evt.target?.value;
|
|
125
|
+
setValue(val);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<div className="App">
|
|
130
|
+
<textarea
|
|
131
|
+
onChange={handleChange}
|
|
132
|
+
ref={el}
|
|
133
|
+
rows={3}
|
|
134
|
+
value={value}
|
|
135
|
+
/>
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
//--------
|
|
142
|
+
|
|
143
|
+
const App = () => {
|
|
144
|
+
const [value, setValue] = useState("");
|
|
145
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
146
|
+
|
|
147
|
+
useAutosizeTextArea(el.current, value, true);
|
|
148
|
+
|
|
149
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
150
|
+
const val = evt.target?.value;
|
|
151
|
+
setValue(val);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<div className="App">
|
|
156
|
+
<textarea
|
|
157
|
+
onChange={handleChange}
|
|
158
|
+
ref={el}
|
|
159
|
+
rows={1}
|
|
160
|
+
value={value}
|
|
161
|
+
/>
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
// Updates the height of a <textarea> when the value changes.
|
|
173
|
+
var useAutosizeTextArea = function useAutosizeTextArea(el, value, autoSizeBeginOneline) {
|
|
174
|
+
var _useState = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(0),
|
|
175
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
176
|
+
defaultRowHeight = _useState2[0],
|
|
177
|
+
setDefaultRowHeight = _useState2[1];
|
|
178
|
+
var _useState3 = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(0),
|
|
179
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
180
|
+
dynamicDefaultRowHeight = _useState4[0],
|
|
181
|
+
setDynamicDefaultRowHeight = _useState4[1];
|
|
182
|
+
var _useState5 = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(false),
|
|
183
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
184
|
+
defaultRowHeightInit = _useState6[0],
|
|
185
|
+
setDefaultRowHeightInit = _useState6[1];
|
|
186
|
+
(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useEffect)(function () {
|
|
187
|
+
if (el) {
|
|
188
|
+
if (autoSizeBeginOneline) {
|
|
189
|
+
el.style.height = "0px";
|
|
190
|
+
var beginOnelineScrollHeight = el.scrollHeight;
|
|
191
|
+
el.style.height = beginOnelineScrollHeight + "px";
|
|
192
|
+
} else {
|
|
193
|
+
// initialize default row height
|
|
194
|
+
if (el.scrollHeight > 0 && !defaultRowHeightInit) {
|
|
195
|
+
setDefaultRowHeight(el.scrollHeight);
|
|
196
|
+
setDynamicDefaultRowHeight(el.scrollHeight);
|
|
197
|
+
setDefaultRowHeightInit(true);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// reset the height momentarily to get the correct scrollHeight for the textarea
|
|
201
|
+
var scrollHeight = el.scrollHeight;
|
|
202
|
+
|
|
203
|
+
// reset row height
|
|
204
|
+
if (typeof value !== 'undefined' && value.length === 0) {
|
|
205
|
+
el.style.height = dynamicDefaultRowHeight + "px";
|
|
206
|
+
|
|
207
|
+
// update default row height
|
|
208
|
+
setDefaultRowHeight(dynamicDefaultRowHeight);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// then set the height directly, outside of the render loop
|
|
212
|
+
// Trying to set this with state or a ref will product an incorrect value.
|
|
213
|
+
|
|
214
|
+
// !!! Compare initial height and changed height
|
|
215
|
+
if (scrollHeight > defaultRowHeight && defaultRowHeight > 0) {
|
|
216
|
+
el.style.height = scrollHeight + "px";
|
|
217
|
+
|
|
218
|
+
// update default row height
|
|
219
|
+
setDefaultRowHeight(scrollHeight);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}, [el, value]);
|
|
224
|
+
};
|
|
225
|
+
/* harmony default export */ const utils_useAutosizeTextArea = (useAutosizeTextArea);
|
|
226
|
+
;// CONCATENATED MODULE: ./src/index.tsx
|
|
227
|
+
var _excluded = ["wrapperClassName", "controlClassName", "cols", "rows", "disabled", "required", "placeholder", "autoSize", "autoSizeBeginOneline", "readOnly", "value", "label", "name", "id", "maxLength", "style", "tabIndex", "onChangeCallback", "onChange", "onBlur", "onFocus"];
|
|
228
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
229
|
+
function src_slicedToArray(arr, i) { return src_arrayWithHoles(arr) || src_iterableToArrayLimit(arr, i) || src_unsupportedIterableToArray(arr, i) || src_nonIterableRest(); }
|
|
230
|
+
function src_nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
231
|
+
function src_unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return src_arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return src_arrayLikeToArray(o, minLen); }
|
|
232
|
+
function src_arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
233
|
+
function src_iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
234
|
+
function src_arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
107
235
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
108
236
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
109
237
|
|
|
238
|
+
|
|
110
239
|
;
|
|
111
|
-
var Textarea = /*#__PURE__*/(0,
|
|
240
|
+
var Textarea = /*#__PURE__*/(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.forwardRef)(function (props, _ref) {
|
|
112
241
|
var wrapperClassName = props.wrapperClassName,
|
|
113
242
|
controlClassName = props.controlClassName,
|
|
114
243
|
cols = props.cols,
|
|
@@ -116,6 +245,8 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
116
245
|
disabled = props.disabled,
|
|
117
246
|
required = props.required,
|
|
118
247
|
placeholder = props.placeholder,
|
|
248
|
+
autoSize = props.autoSize,
|
|
249
|
+
autoSizeBeginOneline = props.autoSizeBeginOneline,
|
|
119
250
|
readOnly = props.readOnly,
|
|
120
251
|
value = props.value,
|
|
121
252
|
label = props.label,
|
|
@@ -124,18 +255,22 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
124
255
|
maxLength = props.maxLength,
|
|
125
256
|
style = props.style,
|
|
126
257
|
tabIndex = props.tabIndex,
|
|
258
|
+
onChangeCallback = props.onChangeCallback,
|
|
127
259
|
onChange = props.onChange,
|
|
128
260
|
onBlur = props.onBlur,
|
|
129
261
|
onFocus = props.onFocus,
|
|
130
262
|
attributes = _objectWithoutProperties(props, _excluded);
|
|
131
|
-
var uniqueID = (0,
|
|
263
|
+
var uniqueID = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useId)();
|
|
132
264
|
var idRes = id || uniqueID;
|
|
133
|
-
var rootRef = (0,
|
|
134
|
-
var valRef = (0,
|
|
135
|
-
var _useState = (0,
|
|
136
|
-
_useState2 =
|
|
265
|
+
var rootRef = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useRef)(null);
|
|
266
|
+
var valRef = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useRef)(null);
|
|
267
|
+
var _useState = (0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useState)(value || ''),
|
|
268
|
+
_useState2 = src_slicedToArray(_useState, 2),
|
|
137
269
|
changedVal = _useState2[0],
|
|
138
270
|
setChangedVal = _useState2[1];
|
|
271
|
+
|
|
272
|
+
// auto size
|
|
273
|
+
utils_useAutosizeTextArea(autoSize ? valRef.current : null, autoSize ? changedVal : '', autoSizeBeginOneline);
|
|
139
274
|
function handleFocus(event) {
|
|
140
275
|
var el = event.target;
|
|
141
276
|
rootRef.current.classList.add('focus');
|
|
@@ -168,24 +303,30 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
168
303
|
|
|
169
304
|
//
|
|
170
305
|
onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
|
|
306
|
+
|
|
307
|
+
//
|
|
308
|
+
if (typeof onChangeCallback === 'function') {
|
|
309
|
+
var newData = onChangeCallback(event);
|
|
310
|
+
setChangedVal(newData);
|
|
311
|
+
}
|
|
171
312
|
}
|
|
172
|
-
(0,
|
|
313
|
+
(0,external_root_React_commonjs2_react_commonjs_react_amd_react_.useEffect)(function () {
|
|
173
314
|
// update default value
|
|
174
315
|
//--------------
|
|
175
316
|
setChangedVal(value || '');
|
|
176
317
|
}, [value]);
|
|
177
|
-
return /*#__PURE__*/
|
|
318
|
+
return /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("div", {
|
|
178
319
|
className: wrapperClassName || wrapperClassName === '' ? wrapperClassName : "mb-3 position-relative",
|
|
179
320
|
ref: rootRef
|
|
180
|
-
}, label ? /*#__PURE__*/
|
|
321
|
+
}, label ? /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("label", {
|
|
181
322
|
htmlFor: idRes,
|
|
182
323
|
className: "form-label",
|
|
183
324
|
dangerouslySetInnerHTML: {
|
|
184
325
|
__html: "".concat(label)
|
|
185
326
|
}
|
|
186
|
-
})) : null, /*#__PURE__*/
|
|
327
|
+
})) : null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("div", {
|
|
187
328
|
className: "input-group"
|
|
188
|
-
}, /*#__PURE__*/
|
|
329
|
+
}, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("textarea", _extends({
|
|
189
330
|
ref: function ref(node) {
|
|
190
331
|
valRef.current = node;
|
|
191
332
|
if (typeof _ref === 'function') {
|
|
@@ -208,15 +349,15 @@ var Textarea = /*#__PURE__*/(0,react__WEBPACK_IMPORTED_MODULE_0__.forwardRef)(fu
|
|
|
208
349
|
required: required || null,
|
|
209
350
|
readOnly: readOnly || null,
|
|
210
351
|
cols: cols || 20,
|
|
211
|
-
rows: rows ||
|
|
352
|
+
rows: autoSizeBeginOneline ? 1 : rows || 1,
|
|
212
353
|
style: style
|
|
213
|
-
}, attributes))), required ? /*#__PURE__*/
|
|
354
|
+
}, attributes))), required ? /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement((external_root_React_commonjs2_react_commonjs_react_amd_react_default()).Fragment, null, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("span", {
|
|
214
355
|
className: "position-absolute end-0 top-0 my-2 mx-2"
|
|
215
|
-
}, /*#__PURE__*/
|
|
356
|
+
}, /*#__PURE__*/external_root_React_commonjs2_react_commonjs_react_amd_react_default().createElement("span", {
|
|
216
357
|
className: "text-danger"
|
|
217
358
|
}, "*"))) : ''));
|
|
218
359
|
});
|
|
219
|
-
/* harmony default export */ const
|
|
360
|
+
/* harmony default export */ const src = (Textarea);
|
|
220
361
|
})();
|
|
221
362
|
|
|
222
363
|
/******/ return __webpack_exports__;
|
package/lib/esm/Input/index.tsx
CHANGED
|
@@ -118,10 +118,7 @@ const Input = forwardRef((props: InputProps, ref: any) => {
|
|
|
118
118
|
|
|
119
119
|
//
|
|
120
120
|
onChange?.(event, onComposition);
|
|
121
|
-
|
|
122
|
-
const newData: any = onChangeCallback(event);
|
|
123
|
-
setChangedVal(newData);
|
|
124
|
-
}
|
|
121
|
+
|
|
125
122
|
|
|
126
123
|
}
|
|
127
124
|
|
|
@@ -138,6 +135,12 @@ const Input = forwardRef((props: InputProps, ref: any) => {
|
|
|
138
135
|
|
|
139
136
|
//
|
|
140
137
|
onBlur?.(event, onComposition);
|
|
138
|
+
|
|
139
|
+
//
|
|
140
|
+
if (typeof (onChangeCallback) === 'function') {
|
|
141
|
+
const newData: any = onChangeCallback(event);
|
|
142
|
+
setChangedVal(newData);
|
|
143
|
+
}
|
|
141
144
|
}
|
|
142
145
|
|
|
143
146
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React, { useId, useState, useEffect, useRef, forwardRef } from 'react';
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
import useAutosizeTextArea from './utils/useAutosizeTextArea';
|
|
5
|
+
|
|
4
6
|
declare module 'react' {
|
|
5
7
|
interface ReactI18NextChildren<T> {
|
|
6
8
|
children?: any;
|
|
@@ -19,11 +21,14 @@ interface TextareaProps extends React.ComponentPropsWithoutRef<"textarea"> {
|
|
|
19
21
|
required?: any;
|
|
20
22
|
readOnly?: any;
|
|
21
23
|
placeholder?: string;
|
|
24
|
+
autoSize?: boolean;
|
|
25
|
+
autoSizeBeginOneline?: boolean;
|
|
22
26
|
/** -- */
|
|
23
27
|
id?: string;
|
|
24
28
|
style?: React.CSSProperties;
|
|
25
29
|
tabIndex?: number;
|
|
26
30
|
[key: `data-${string}`]: string | undefined;
|
|
31
|
+
onChangeCallback?: (e: any) => void;
|
|
27
32
|
onChange?: (e: any) => void;
|
|
28
33
|
onBlur?: (e: any) => void;
|
|
29
34
|
onFocus?: (e: any) => void;
|
|
@@ -39,6 +44,8 @@ const Textarea = forwardRef((props: TextareaProps, ref: any) => {
|
|
|
39
44
|
disabled,
|
|
40
45
|
required,
|
|
41
46
|
placeholder,
|
|
47
|
+
autoSize,
|
|
48
|
+
autoSizeBeginOneline,
|
|
42
49
|
readOnly,
|
|
43
50
|
value,
|
|
44
51
|
label,
|
|
@@ -47,6 +54,7 @@ const Textarea = forwardRef((props: TextareaProps, ref: any) => {
|
|
|
47
54
|
maxLength,
|
|
48
55
|
style,
|
|
49
56
|
tabIndex,
|
|
57
|
+
onChangeCallback,
|
|
50
58
|
onChange,
|
|
51
59
|
onBlur,
|
|
52
60
|
onFocus,
|
|
@@ -60,6 +68,10 @@ const Textarea = forwardRef((props: TextareaProps, ref: any) => {
|
|
|
60
68
|
const valRef = useRef<any>(null);
|
|
61
69
|
const [changedVal, setChangedVal] = useState<string>(value || '');
|
|
62
70
|
|
|
71
|
+
|
|
72
|
+
// auto size
|
|
73
|
+
useAutosizeTextArea(autoSize ? valRef.current : null, autoSize ? changedVal : '', autoSizeBeginOneline);
|
|
74
|
+
|
|
63
75
|
function handleFocus(event: any) {
|
|
64
76
|
const el = event.target;
|
|
65
77
|
rootRef.current.classList.add('focus');
|
|
@@ -96,6 +108,14 @@ const Textarea = forwardRef((props: TextareaProps, ref: any) => {
|
|
|
96
108
|
|
|
97
109
|
//
|
|
98
110
|
onBlur?.(event);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
//
|
|
114
|
+
if (typeof (onChangeCallback) === 'function') {
|
|
115
|
+
const newData: any = onChangeCallback(event);
|
|
116
|
+
setChangedVal(newData);
|
|
117
|
+
}
|
|
118
|
+
|
|
99
119
|
}
|
|
100
120
|
|
|
101
121
|
useEffect(() => {
|
|
@@ -138,7 +158,7 @@ const Textarea = forwardRef((props: TextareaProps, ref: any) => {
|
|
|
138
158
|
required={required || null}
|
|
139
159
|
readOnly={readOnly || null}
|
|
140
160
|
cols={cols || 20}
|
|
141
|
-
rows={rows ||
|
|
161
|
+
rows={autoSizeBeginOneline ? 1 : (rows || 1)}
|
|
142
162
|
style={style}
|
|
143
163
|
{...attributes}
|
|
144
164
|
/>
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creating a Textarea with dynamic height
|
|
3
|
+
*
|
|
4
|
+
* @usage:
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const App = () => {
|
|
8
|
+
const [value, setValue] = useState("");
|
|
9
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
10
|
+
|
|
11
|
+
useAutosizeTextArea(el.current, value, false);
|
|
12
|
+
|
|
13
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
14
|
+
const val = evt.target?.value;
|
|
15
|
+
setValue(val);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<div className="App">
|
|
20
|
+
<textarea
|
|
21
|
+
onChange={handleChange}
|
|
22
|
+
ref={el}
|
|
23
|
+
rows={3}
|
|
24
|
+
value={value}
|
|
25
|
+
/>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
//--------
|
|
32
|
+
|
|
33
|
+
const App = () => {
|
|
34
|
+
const [value, setValue] = useState("");
|
|
35
|
+
const el = useRef<HTMLTextAreaElement>(null);
|
|
36
|
+
|
|
37
|
+
useAutosizeTextArea(el.current, value, true);
|
|
38
|
+
|
|
39
|
+
const handleChange = (evt: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
40
|
+
const val = evt.target?.value;
|
|
41
|
+
setValue(val);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div className="App">
|
|
46
|
+
<textarea
|
|
47
|
+
onChange={handleChange}
|
|
48
|
+
ref={el}
|
|
49
|
+
rows={1}
|
|
50
|
+
value={value}
|
|
51
|
+
/>
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
import { useEffect, useState } from "react";
|
|
62
|
+
|
|
63
|
+
// Updates the height of a <textarea> when the value changes.
|
|
64
|
+
const useAutosizeTextArea = (
|
|
65
|
+
el,
|
|
66
|
+
value,
|
|
67
|
+
autoSizeBeginOneline
|
|
68
|
+
) => {
|
|
69
|
+
|
|
70
|
+
const [defaultRowHeight, setDefaultRowHeight] = useState(0);
|
|
71
|
+
const [dynamicDefaultRowHeight, setDynamicDefaultRowHeight] = useState(0);
|
|
72
|
+
const [defaultRowHeightInit, setDefaultRowHeightInit] = useState(false);
|
|
73
|
+
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
|
|
76
|
+
if (el) {
|
|
77
|
+
|
|
78
|
+
if (autoSizeBeginOneline) {
|
|
79
|
+
el.style.height = "0px";
|
|
80
|
+
const beginOnelineScrollHeight = el.scrollHeight;
|
|
81
|
+
el.style.height = beginOnelineScrollHeight + "px";
|
|
82
|
+
} else {
|
|
83
|
+
|
|
84
|
+
// initialize default row height
|
|
85
|
+
if (el.scrollHeight > 0 && !defaultRowHeightInit) {
|
|
86
|
+
setDefaultRowHeight(el.scrollHeight);
|
|
87
|
+
setDynamicDefaultRowHeight(el.scrollHeight);
|
|
88
|
+
setDefaultRowHeightInit(true);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// reset the height momentarily to get the correct scrollHeight for the textarea
|
|
92
|
+
const scrollHeight = el.scrollHeight;
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
// reset row height
|
|
96
|
+
if (typeof value !== 'undefined' && value.length === 0) {
|
|
97
|
+
el.style.height = dynamicDefaultRowHeight + "px";
|
|
98
|
+
|
|
99
|
+
// update default row height
|
|
100
|
+
setDefaultRowHeight(dynamicDefaultRowHeight);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
// then set the height directly, outside of the render loop
|
|
105
|
+
// Trying to set this with state or a ref will product an incorrect value.
|
|
106
|
+
|
|
107
|
+
// !!! Compare initial height and changed height
|
|
108
|
+
if (scrollHeight > defaultRowHeight && defaultRowHeight > 0) {
|
|
109
|
+
el.style.height = scrollHeight + "px";
|
|
110
|
+
|
|
111
|
+
// update default row height
|
|
112
|
+
setDefaultRowHeight(scrollHeight);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
}
|
|
121
|
+
}, [el, value]);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export default useAutosizeTextArea;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "UIUX Lab",
|
|
3
3
|
"email": "uiuxlab@gmail.com",
|
|
4
4
|
"name": "funda-ui",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.517",
|
|
6
6
|
"description": "React components using pure Bootstrap 5+ which does not contain any external style and script libraries.",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|