scniro / react-codemirror2

Codemirror integrated components for React
MIT License
1.65k stars 192 forks source link

Can not get autoFocus to work #75

Closed fiws closed 6 years ago

fiws commented 6 years ago

The prop simply does nothing for me:

Edit 3r3yx41kq6

121watts commented 6 years ago

Can confirm. Same issue here

scniro commented 6 years ago

@fiws @121watts sorry for the late response. I'll definitely look into this over the weekend and either get a fix up or clarify the documentation.

Adam13531 commented 6 years ago

I did some investigation into this just now.

The cm class is not being constructed with options passed in (which is where autofocus would actually take effect):

react-codemirror2 side of things:

https://github.com/scniro/react-codemirror2/blob/3250f5833f48e65a1b560dc3059f0ce4ed0771fd/src/index.tsx#L443

CodeMirror side of things:

if (options.autofocus && !mobile) display.input.focus()

Instead, hydrate is called later in componentDidMount, which triggers a call to setOption in CodeMirror directly (source here). This just doesn't have a handler for autofocus, so nothing is done to focus the input.

I hope that's helpful. I can't prioritize making a pull request for this right now, so I haven't looked into the best solution. 🙁

scniro commented 6 years ago

@Adam13531 thanks for doing some investigative work on this, it'll surely help. I'm planning on knocking this out over the weekend and will post an update here 👍

scniro commented 6 years ago

@Adam13531 @121watts @fiws This should now be fixed with the 5.0.0 release. I've done away with the autoFocus prop entirely in favor of the codemirror options={{ autofocus: true }}. As @Adam13531 pointed out, there simply wasn't a handler for this, so we just trigger it on componentDidMount

if (this.editor.getOption('autofocus')) {
  this.editor.focus();
}
121watts commented 6 years ago

awesome! thanks for the speedy response / resolution

Adam13531 commented 6 years ago

@scniro thanks a ton for the fix! I tried it out and it's working. 👍