Is `onTab` working properly when pressing [Tab] + [Shift]?
My Draft editor implementation has five block types and they are hierarchical: title, subtitle, paragraph, bullet, sub-bullet. Cycling between block types can happen in one of two ways:
- Promoting a block to a level above it (e.g. moving a subtitle to a title)
- Demoting a block to a level below it (e.g. moving a paragraph to a bullet)
I’m using the onTab handler to change block types:
- Tab demotes the current block
- Tab + Shift promotes the current block
~~Currently, Draft calls preventDefault() when I Tab so the focus remains in the editor (ignoring the browser’s default behavior of moving focus to the next focusable element in the page).~~
However, Draft does not prevent the default behavior of Tab + Shift. The keypresses are received and responded to by Draft (nice!), but the editor blurs and focus moves to the previous focusable element on the page.
I can handle this using a custom key binding, but I’m wondering if this is a bug or designed purposefully.
I am happy to open a PR and make this change, but I want to understand the thinking behind this before I move ahead.
I can handle this using a custom key binding…
Within the custom key binding, event.key === 'Tab'is false but event.key === 'Shift' is true for Tab + Shift. Can the custom key binding handle a Tab keypress?
Within my onTab handler, I can detect Shift and call event.preventDefault(). My workaround looks like:
handleTabKey = (event) => {
const {editorState} = this.state
let blockType = null
if (event.shiftKey) {
event.preventDefault()
blockType = promoteBlockType(RichUtils.getCurrentBlockType(editorState))
} else {
blockType = demoteBlockType(RichUtils.getCurrentBlockType(editorState))
}
if (blockType) {
this.handleChange(RichUtils.toggleBlockType(editorState, blockType))
}
return true
}
Currently, Draft calls preventDefault() when I Tab so the focus remains in the editor
Hmm, I don't think Draft should be preventing default on tab. Where do you see that? (The onTab prop function is called here: https://github.com/facebook/draft-js/blob/06891490069d3ccfb89caf53f33a7545d39fc3ec/src/component/handlers/edit/editOnKeyDown.js#L96)
Where do you see that?
You’re right, I don’t. After trying a contentEditable example elsewhere, I believe that the browser is handling Tab natively.
I don't think the browser should be doing that. Can you demonstrate with a jsfiddle?
Let me investigate further and see if I can isolate what appears to a bug specific to my Draft implementation.
Any luck isolating the issue? :)
Thanks for following up. No luck yet. I am going to close this to reduce noise. I will add new details if I uncover anything useful.
I'm having an issue with the tab key where draft-js keeps focus and I see an error in the console
DOMException: Failed to execute 'extend' on 'Selection': This Selection object doesn't have any Ranges.
Has anyone ran into this before? I have multiple draftJS editors on my page and I want to be able to tab between them. I tried using key binding as described in the docs, but the tab event never gets to the keyBindingFn before the error is triggered...
Can you give examples for demoteBlockType & promoteBlockType?
This issue is occuring for me as well. In the same browser with just Draft JS examples, tab and shift+tab indent ul and ol blocks as expected.
Within my project however, and within a stripped down React storybook, I see the focus lost upon tabbing, much like the link @gscottolson posted on jsfiddle.
For anyone still interested in this, in the event that yours wasn't working like mine was.
Here is one solution that I found for properly capturing tabs inside DraftJS.
Add a wrapper div around your DraftJS Editor (if following examples, you likely already have one).
Then add the onKeyDown prop:
<div className={className} onKeyDown={this.onKeyPressed}>
<Editor .... />
</div>
then add this listener to handle the tab event:
onKeyPressed(event) {
if (event.key === 'Tab') {
const newEditorState = RichUtils.onTab(
event,
this.props.editorState,
4 /* maxDepth */
);
if (newEditorState !== this.props.editorState) {
this.onChange(newEditorState);
}
event.preventDefault();
}
}
I can handle this using a custom key binding…
Within the custom key binding,
event.key === 'Tab'is false butevent.key === 'Shift'is true for Tab + Shift. Can the custom key binding handle a Tab keypress?Within my
onTabhandler, I can detect Shift and callevent.preventDefault(). My workaround looks like:handleTabKey = (event) => { const {editorState} = this.state let blockType = null if (event.shiftKey) { event.preventDefault() blockType = promoteBlockType(RichUtils.getCurrentBlockType(editorState)) } else { blockType = demoteBlockType(RichUtils.getCurrentBlockType(editorState)) } if (blockType) { this.handleChange(RichUtils.toggleBlockType(editorState, blockType)) } return true }
what is demoteBlockType and promoteBlockType doing