@mpen/react-basic-inputs 0.1.5 → 0.1.7

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/dist/bundle.cjs CHANGED
@@ -215,35 +215,43 @@ function TextInput({formatOnChange = collapseWhitespace, ...otherProps}) {
215
215
  });
216
216
  }
217
217
 
218
- const TextArea = React.forwardRef((function TextArea({onInput, style, ...rest}, fwdRef) {
218
+ const TextArea = React.forwardRef((function TextArea({onInput, style, initialHeight = "auto", ...rest}, fwdRef) {
219
219
  const ref = React.useRef(null);
220
- const [height, setHeight] = React.useState("auto");
221
- const adjustHeight = () => {
220
+ const [height, setHeight] = React.useState(initialHeight);
221
+ const adjustHeight = React.useCallback((() => {
222
222
  const textarea = ref.current;
223
223
  if (!textarea) return;
224
- textarea.style.height = "auto";
224
+ textarea.style.height = initialHeight;
225
225
  const newHeight = `${textarea.scrollHeight}px`;
226
226
  setHeight(newHeight);
227
227
  textarea.style.height = newHeight;
228
- };
228
+ }), [ initialHeight ]);
229
229
  React.useImperativeHandle(fwdRef, (() => ({
230
230
  element: ref.current,
231
- resize: adjustHeight
232
- })), [ setHeight, ref.current ]);
231
+ adjustHeight
232
+ })), [ adjustHeight ]);
233
233
  const input = useEventHandler((ev => {
234
234
  adjustHeight();
235
235
  onInput?.(ev);
236
236
  }));
237
237
  React.useLayoutEffect((() => {
238
238
  adjustHeight();
239
- }), []);
239
+ const textarea = ref.current;
240
+ if (!textarea) return;
241
+ const resizeObserver = new ResizeObserver((entries => {
242
+ adjustHeight();
243
+ }));
244
+ resizeObserver.observe(textarea);
245
+ return () => {
246
+ resizeObserver.unobserve(textarea);
247
+ };
248
+ }), [ adjustHeight ]);
240
249
  return jsxRuntime.jsx("textarea", {
241
- rows: 1,
242
250
  ...rest,
243
251
  style: {
244
- ...style,
245
252
  overflow: "hidden",
246
253
  resize: "none",
254
+ ...style,
247
255
  height
248
256
  },
249
257
  onInput: input,
package/dist/bundle.mjs CHANGED
@@ -213,35 +213,43 @@ function TextInput({formatOnChange = collapseWhitespace, ...otherProps}) {
213
213
  });
214
214
  }
215
215
 
216
- const TextArea = forwardRef((function TextArea({onInput, style, ...rest}, fwdRef) {
216
+ const TextArea = forwardRef((function TextArea({onInput, style, initialHeight = "auto", ...rest}, fwdRef) {
217
217
  const ref = useRef(null);
218
- const [height, setHeight] = useState("auto");
219
- const adjustHeight = () => {
218
+ const [height, setHeight] = useState(initialHeight);
219
+ const adjustHeight = useCallback((() => {
220
220
  const textarea = ref.current;
221
221
  if (!textarea) return;
222
- textarea.style.height = "auto";
222
+ textarea.style.height = initialHeight;
223
223
  const newHeight = `${textarea.scrollHeight}px`;
224
224
  setHeight(newHeight);
225
225
  textarea.style.height = newHeight;
226
- };
226
+ }), [ initialHeight ]);
227
227
  useImperativeHandle(fwdRef, (() => ({
228
228
  element: ref.current,
229
- resize: adjustHeight
230
- })), [ setHeight, ref.current ]);
229
+ adjustHeight
230
+ })), [ adjustHeight ]);
231
231
  const input = useEventHandler((ev => {
232
232
  adjustHeight();
233
233
  onInput?.(ev);
234
234
  }));
235
235
  useLayoutEffect((() => {
236
236
  adjustHeight();
237
- }), []);
237
+ const textarea = ref.current;
238
+ if (!textarea) return;
239
+ const resizeObserver = new ResizeObserver((entries => {
240
+ adjustHeight();
241
+ }));
242
+ resizeObserver.observe(textarea);
243
+ return () => {
244
+ resizeObserver.unobserve(textarea);
245
+ };
246
+ }), [ adjustHeight ]);
238
247
  return jsx("textarea", {
239
- rows: 1,
240
248
  ...rest,
241
249
  style: {
242
- ...style,
243
250
  overflow: "hidden",
244
251
  resize: "none",
252
+ ...style,
245
253
  height
246
254
  },
247
255
  onInput: input,
@@ -2,7 +2,13 @@
2
2
  import { HtmlTextAreaElement, OverrideProps, VoidFn } from '../types/utility';
3
3
  export type TextAreaRef = {
4
4
  element: HtmlTextAreaElement;
5
- resize: VoidFn;
5
+ adjustHeight: VoidFn;
6
6
  };
7
- export type TextAreaProps = OverrideProps<'textarea', {}>;
8
- export declare const TextArea: import("react").ForwardRefExoticComponent<Omit<Omit<import("react").DetailedHTMLProps<import("react").TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "ref">, never> & import("react").RefAttributes<TextAreaRef>>;
7
+ export type TextAreaProps = OverrideProps<'textarea', {
8
+ /** Initial/minimum height. "0" or "auto" are good choices. Defaults to "auto" */
9
+ initialHeight?: string;
10
+ }>;
11
+ export declare const TextArea: import("react").ForwardRefExoticComponent<Omit<Omit<import("react").DetailedHTMLProps<import("react").TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "ref">, "initialHeight"> & {
12
+ /** Initial/minimum height. "0" or "auto" are good choices. Defaults to "auto" */
13
+ initialHeight?: string;
14
+ } & import("react").RefAttributes<TextAreaRef>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpen/react-basic-inputs",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "packageManager": "yarn@3.5.0",
5
5
  "exports": {
6
6
  ".": {