Skip to content

Commit ac6b6e4

Browse files
committed
feat: add autoFocus prop
1 parent e5af4fc commit ac6b6e4

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/ContentEditable.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface ContentEditableProps {
99
disabled?: boolean
1010
updatedContent?: string
1111
maxLength?: number
12+
autoFocus?: boolean
1213
onChange?: (content: string) => void
1314
onKeyUp?: (e: React.KeyboardEvent) => void
1415
onKeyDown?: (e: React.KeyboardEvent) => void
@@ -35,6 +36,7 @@ const ContentEditable: React.FC<ContentEditableProps> = ({
3536
disabled,
3637
updatedContent,
3738
maxLength,
39+
autoFocus,
3840
onChange,
3941
onKeyUp,
4042
onKeyDown,
@@ -62,6 +64,12 @@ const ContentEditable: React.FC<ContentEditableProps> = ({
6264
}
6365
}, [content, onChange, maxLength])
6466

67+
useEffect(() => {
68+
if (divRef.current && autoFocus) {
69+
divRef.current.focus()
70+
}
71+
}, [autoFocus])
72+
6573
/**
6674
* Checks if the caret is on the last line of a contenteditable element
6775
* @param element - The HTMLDivElement to check

src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const App = () => {
4646
updatedContent={emptyContent}
4747
onChange={(content) => setContent(content)}
4848
maxLength={100}
49+
autoFocus
4950
onFocus={() => {
5051
setIsFocused(true)
5152
setIsBlurred(false)

0 commit comments

Comments
 (0)