Skip to content

Commit 0754155

Browse files
committed
autofocus
1 parent a5bb83e commit 0754155

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/components/field/field-name-content.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { styled } from 'storybook/internal/theming';
2-
import { useCallback, useState } from 'react';
2+
import { useCallback, useEffect, useRef, useState } from 'react';
33

44
import { ellipsisTruncation } from '@/styles/styles';
55
import { DEFAULT_FIELD_HEIGHT } from '@/utilities/constants';
@@ -28,14 +28,25 @@ interface FieldNameProps {
2828
export const FieldNameContent = ({ name, isEditable, onChange }: FieldNameProps) => {
2929
const [isEditing, setIsEditing] = useState(false);
3030
const [value, setValue] = useState(name);
31+
const textareaRef = useRef<HTMLTextAreaElement>(null);
3132

3233
const handleSubmit = useCallback(() => {
3334
setIsEditing(false);
3435
onChange?.(value);
3536
}, [value, onChange]);
3637

38+
useEffect(() => {
39+
if (isEditing) {
40+
setTimeout(() => {
41+
textareaRef.current?.focus();
42+
textareaRef.current?.select();
43+
});
44+
}
45+
}, [isEditing]);
46+
3747
return isEditing ? (
3848
<InlineTextarea
49+
ref={textareaRef}
3950
value={value}
4051
onChange={e => {
4152
setValue(e.target.value);
@@ -45,6 +56,7 @@ export const FieldNameContent = ({ name, isEditable, onChange }: FieldNameProps)
4556
if (e.key === 'Enter') handleSubmit();
4657
if (e.key === 'Escape') setIsEditing(false);
4758
}}
59+
title="Edit field name"
4860
/>
4961
) : (
5062
<InnerFieldName

0 commit comments

Comments
 (0)