Skip to content

Commit

Permalink
remove ref as a prop, using use ref in textarea instead
Browse files Browse the repository at this point in the history
  • Loading branch information
srietkerk committed Mar 19, 2024
1 parent 6e6fe22 commit 0b8183f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 5 additions & 6 deletions react-common/components/controls/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface TextareaProps extends ControlProps {
resize?: "both" | "horizontal" | "vertical";
wrap?: "hard" | "soft" | "off";
autoResize?: boolean;
resizeRef?: React.RefObject<HTMLTextAreaElement>;

onChange?: (newValue: string) => void;
onEnterKey?: (value: string) => void;
Expand All @@ -41,12 +40,12 @@ export const Textarea = (props: TextareaProps) => {
resize,
wrap,
autoResize,
resizeRef,
onChange,
onEnterKey
} = props;

const [value, setValue] = React.useState(initialValue || "");
const textareaRef = React.useRef<HTMLTextAreaElement>(null);

React.useEffect(() => {
setValue(initialValue)
Expand All @@ -61,9 +60,9 @@ export const Textarea = (props: TextareaProps) => {
if (onChange) {
onChange(newValue);
}
if (autoResize && resizeRef.current) {
resizeRef.current.style.height = "1px";
resizeRef.current.style.height = `${25 + resizeRef.current.scrollHeight}px`;
if (autoResize && textareaRef.current) {
textareaRef.current.style.height = "1px";
textareaRef.current.style.height = `${25 + textareaRef.current.scrollHeight}px`;
}
}

Expand Down Expand Up @@ -98,7 +97,7 @@ export const Textarea = (props: TextareaProps) => {
minLength={minLength}
wrap={wrap}
readOnly={!!readOnly}
ref={resizeRef}
ref={textareaRef}
onChange={changeHandler}
onKeyDown={enterKeyHandler}
autoComplete={autoComplete ? "" : "off"}
Expand Down
3 changes: 1 addition & 2 deletions teachertool/src/components/DebouncedTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export interface DebouncedTextareaProps extends TextareaProps {
export const DebouncedTextarea: React.FC<DebouncedTextareaProps> = ({ intervalMs = 500, ...props }) => {
const timerId = useRef<NodeJS.Timeout | undefined>(undefined);
const latestValue = useRef<string>("");
const textareaRef = useRef<HTMLTextAreaElement>(null);

const sendChange = () => {
if (props.onChange) {
Expand Down Expand Up @@ -40,5 +39,5 @@ export const DebouncedTextarea: React.FC<DebouncedTextareaProps> = ({ intervalMs
timerId.current = setTimeout(sendChange, intervalMs);
};

return <Textarea {...props} autoResize={true} onChange={onChangeDebounce} resizeRef={textareaRef}/>
return <Textarea {...props} autoResize={true} onChange={onChangeDebounce} />
};

0 comments on commit 0b8183f

Please sign in to comment.