diff --git a/src/form/Textarea/Textarea.story.tsx b/src/form/Textarea/Textarea.story.tsx
index 8c3c39ef..0e7fa96a 100644
--- a/src/form/Textarea/Textarea.story.tsx
+++ b/src/form/Textarea/Textarea.story.tsx
@@ -1,6 +1,7 @@
-import React from 'react';
+import { useRef } from 'react';
import { Stack } from '../../layout/Stack';
import { Textarea } from './Textarea';
+import { Button } from '@/elements';
export default {
title: 'Components/Form/Textarea',
@@ -32,3 +33,14 @@ export const Sizes = () => (
);
+
+export const OutsideFocus = () => {
+ const inputRef = useRef(null);
+
+ return (
+
+
+
+
+ );
+};
diff --git a/src/form/Textarea/Textarea.tsx b/src/form/Textarea/Textarea.tsx
index a3e4c8a8..3440d4b6 100644
--- a/src/form/Textarea/Textarea.tsx
+++ b/src/form/Textarea/Textarea.tsx
@@ -1,8 +1,8 @@
-import React, {
- FC,
+import {
forwardRef,
RefObject,
useImperativeHandle,
+ useLayoutEffect,
useRef
} from 'react';
import TextareaAutosize, {
@@ -61,10 +61,7 @@ export interface TextAreaRef {
focus?: () => void;
}
-export const Textarea: FC = forwardRef<
- TextAreaRef,
- TextareaProps
->(
+export const Textarea = forwardRef(
(
{
fullWidth,
@@ -72,21 +69,29 @@ export const Textarea: FC = forwardRef<
containerClassName,
className,
error,
+ autoFocus,
theme: customTheme,
...rest
},
- ref
+ inputRef
) => {
const containerRef = useRef(null);
- const inputRef = useRef(null);
+ const textareaRef = useRef(null);
- useImperativeHandle(ref, () => ({
- inputRef,
+ useImperativeHandle(inputRef, () => ({
+ textareaRef,
containerRef,
- blur: () => inputRef.current?.blur(),
- focus: () => inputRef.current?.focus()
+ blur: () => textareaRef.current?.blur(),
+ focus: () => textareaRef.current?.focus()
}));
+ useLayoutEffect(() => {
+ if (autoFocus) {
+ // Small timeout for page loading
+ requestAnimationFrame(() => textareaRef.current?.focus());
+ }
+ }, [autoFocus]);
+
const theme: TextareaTheme = useComponentTheme('textarea', customTheme);
return (
@@ -100,7 +105,7 @@ export const Textarea: FC = forwardRef<
ref={containerRef}
>
= forwardRef<
theme.sizes[size],
className
)}
+ autoFocus={autoFocus}
{...rest}
/>