draft-js icon indicating copy to clipboard operation
draft-js copied to clipboard

Is `onTab` working properly when pressing [Tab] + [Shift]?

Open gscottolson opened this issue 10 years ago • 13 comments

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:

  1. Promoting a block to a level above it (e.g. moving a subtitle to a title)
  2. 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.

gscottolson avatar Mar 04 '16 15:03 gscottolson

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
}

gscottolson avatar Mar 04 '16 16:03 gscottolson

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)

hellendag avatar Mar 04 '16 16:03 hellendag

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.

gscottolson avatar Mar 04 '16 16:03 gscottolson

I don't think the browser should be doing that. Can you demonstrate with a jsfiddle?

hellendag avatar Mar 04 '16 17:03 hellendag

I made too many assumptions. :disappointed:

Tab blurs the contentEditable in this jsfiddle.

gscottolson avatar Mar 04 '16 17:03 gscottolson

Let me investigate further and see if I can isolate what appears to a bug specific to my Draft implementation.

gscottolson avatar Mar 04 '16 17:03 gscottolson

Any luck isolating the issue? :)

hellendag avatar Mar 06 '16 02:03 hellendag

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.

gscottolson avatar Mar 06 '16 13:03 gscottolson

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...

icd2k3 avatar Jul 25 '17 15:07 icd2k3

Can you give examples for demoteBlockType & promoteBlockType?

ollyde avatar Dec 05 '18 15:12 ollyde

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.

camsjams avatar Mar 21 '19 21:03 camsjams

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();
		}
	}

camsjams avatar Mar 21 '19 21:03 camsjams

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
}

what is demoteBlockType and promoteBlockType doing

JanakiramMaddanala avatar Jul 02 '20 07:07 JanakiramMaddanala