1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 05:01:17 +02:00

Handle autoFocus after mounting (#3226)

This commit is contained in:
Dylan Markow
2019-12-04 14:01:41 -06:00
committed by Ian Storm Taylor
parent 6627ba4b94
commit 75e5015cc5

View File

@@ -1,4 +1,10 @@
import React, { useLayoutEffect, useRef, useMemo, useCallback } from 'react' import React, {
useLayoutEffect,
useEffect,
useRef,
useMemo,
useCallback,
} from 'react'
import { Editor, Element, NodeEntry, Node, Range, Text, Mark } from 'slate' import { Editor, Element, NodeEntry, Node, Range, Text, Mark } from 'slate'
import debounce from 'debounce' import debounce from 'debounce'
import scrollIntoView from 'scroll-into-view-if-needed' import scrollIntoView from 'scroll-into-view-if-needed'
@@ -96,6 +102,7 @@ export const Editable = (
renderDecoration, renderDecoration,
renderElement, renderElement,
renderMark, renderMark,
autoFocus,
style = {}, style = {},
onDOMBeforeInput: propsOnDOMBeforeInput, onDOMBeforeInput: propsOnDOMBeforeInput,
...attributes ...attributes
@@ -210,6 +217,14 @@ export const Editable = (
}) })
}) })
// The autoFocus TextareaHTMLAttribute doesn't do anything on a div, so it
// needs to be manually focused.
useEffect(() => {
if (ref.current && autoFocus) {
ref.current.focus()
}
}, [autoFocus])
// Listen on the native `beforeinput` event to get real "Level 2" events. This // Listen on the native `beforeinput` event to get real "Level 2" events. This
// is required because React's `beforeinput` is fake and never really attaches // is required because React's `beforeinput` is fake and never really attaches
// to the real event sadly. (2019/11/01) // to the real event sadly. (2019/11/01)