Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix textarea input ref #254

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/form/Textarea/Textarea.story.tsx
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -32,3 +33,14 @@ export const Sizes = () => (
<Textarea value="Large" size="large" />
</Stack>
);

export const OutsideFocus = () => {
const inputRef = useRef(null);

return (
<Stack direction="column">
<Button onClick={() => inputRef?.current.focus()}>Click to focus</Button>
<Textarea ref={inputRef} />
</Stack>
);
};
32 changes: 19 additions & 13 deletions src/form/Textarea/Textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, {
FC,
import {
forwardRef,
RefObject,
useImperativeHandle,
useLayoutEffect,
useRef
} from 'react';
import TextareaAutosize, {
Expand Down Expand Up @@ -61,32 +61,37 @@ export interface TextAreaRef {
focus?: () => void;
}

export const Textarea: FC<TextareaProps & TextAreaRef> = forwardRef<
TextAreaRef,
TextareaProps
>(
export const Textarea = forwardRef<TextAreaRef, TextareaProps>(
(
{
fullWidth,
size = 'medium',
containerClassName,
className,
error,
autoFocus,
theme: customTheme,
...rest
},
ref
inputRef
) => {
const containerRef = useRef<HTMLDivElement | null>(null);
const inputRef = useRef<HTMLTextAreaElement | null>(null);
const textareaRef = useRef<HTMLTextAreaElement | null>(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 (
Expand All @@ -100,14 +105,15 @@ export const Textarea: FC<TextareaProps & TextAreaRef> = forwardRef<
ref={containerRef}
>
<TextareaAutosize
ref={inputRef}
ref={textareaRef}
className={twMerge(
theme.input,
fullWidth && theme.fullWidth,
rest.disabled && theme.disabled,
theme.sizes[size],
className
)}
autoFocus={autoFocus}
{...rest}
/>
</div>
Expand Down
Loading