Skip to content

Commit

Permalink
fix: initial autoResize functionality (openedx#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
monteri authored and pedromartello committed Aug 10, 2023
1 parent d4afe68 commit 217bc06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/Form/FormControl.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import RBFormControl from 'react-bootstrap/FormControl';
Expand Down Expand Up @@ -34,13 +34,7 @@ const FormControl = React.forwardRef(({
value: props.value,
});

const controlProps = getControlProps({
...props,
// eslint-disable-next-line react/prop-types
onBlur: callAllHandlers(checkInputEventValue, props.onBlur),
});

const handleOnChange = (e) => {
const handleResize = useCallback(() => {
if (as === 'textarea' && autoResize) {
if (!resolvedRef.current.initialHeight && !resolvedRef.current.offsets) {
resolvedRef.current.initialHeight = resolvedRef.current.offsetHeight;
Expand All @@ -49,6 +43,20 @@ const FormControl = React.forwardRef(({
resolvedRef.current.style.height = `${resolvedRef.current.initialHeight}px`;
resolvedRef.current.style.height = `${resolvedRef.current.scrollHeight + resolvedRef.current.offsets}px`;
}
}, [as, autoResize, resolvedRef]);

useEffect(() => {
handleResize();
}, [handleResize]);

const controlProps = getControlProps({
...props,
// eslint-disable-next-line react/prop-types
onBlur: callAllHandlers(checkInputEventValue, props.onBlur),
});

const handleOnChange = (e) => {
handleResize();
if (onChange) {
onChange(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/tests/FormControl.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('FormControl', () => {
ref.offsetHeight = 90;
ref.clientHeight = 88;
expect(useReferenceSpy).toHaveBeenCalledTimes(1);
expect(ref.current.style.height).toBeFalsy();
expect(ref.current.style.height).toBe('0px');
wrapper.find('textarea').simulate('change');
expect(onChangeFunc).toHaveBeenCalledTimes(1);
expect(ref.current.style.height).toEqual(`${ref.current.scrollHeight + ref.current.offsets}px`);
Expand Down

0 comments on commit 217bc06

Please sign in to comment.