Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate keydown #675

Closed
ghost opened this issue Nov 12, 2016 · 4 comments
Closed

Simulate keydown #675

ghost opened this issue Nov 12, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Nov 12, 2016

Hi I try simulate keydown on draft-js Editor I want

it('Should be called editNote and selectNote', () => {
    _component.find('.DraftEditor-root').simulate('keyDown', {
      which: 83,
      ctrlKey: true
    })
    expect(editNote.calledOnce).to.be.equal(true)
    expect(selectNote.calledOnce).to.be.equal(true)
  })

I try diferent level select in editor and different keyDown but not working same in other part of kode I try with keyDown simulate enter Key but same without effect could you help me?

Component:

   <Editor ref="editor" 
    editorState={editorState} 
    onChange={this.onChange}
    onTab={this.onTab} 
    keyBindingFn= {this.keyBindingFn}
    handleKeyCommand={this.handleKeyCommand}/> 

HANDLING FOR KEYPRESS

  keyBindingFn (e) {
      const { editorState } = this.state
      const { hasCommandModifier }= KeyBindingUtil
      let command
      if (e.keyCode === 83 /* `S` key */ && hasCommandModifier(e)) {
        return 'myeditor-save'
      } 

      if (CodeUtils.hasSelectionInBlock(editorState)) {
          command = CodeUtils.getKeyBinding(e)
      }
      if (command) {
          return command
      }

      return getDefaultKeyBinding(e)
  }

 handleKeyCommand (command) {
      let newState
      const { editorState } = this.state

      if (command === 'myeditor-save') {
        const note = Object.assign({}, this.props.note, {
          desc: JSON.stringify(convertToRaw(editorState.getCurrentContent()))
        })
        this.props.editNote(note).then((data) => {
          this.props.selectNote(note)
        })
      } 

      if (CodeUtils.hasSelectionInBlock(editorState)) {
          newState = CodeUtils.handleKeyCommand(editorState, command)
      }

      if (!newState) {
          newState = RichUtils.handleKeyCommand(editorState, command)
      }

      if (newState) {
          this.onChange(newState);
          return true
      }
      return false
  }

Thank you

@neville1355
Copy link

The way that simulate is written appears to expect that onKeyDown is mapped to the component you are trying to simulate an event on.

E.g. simulate('change') will call Component.onChange if it's defined. Or, simulate('keyDown') will call Component.onKeyDown

Unfortunately, I don't think simulate will work for your use case

@kenju
Copy link
Contributor

kenju commented May 10, 2017

@jordann-nulogy
@Simproduction

FYI
I have faced with the same problem with you, and found a better solution to test Keyboard Event simulation for draft.js. Have a look at ->
facebookarchive/draft-js#325 (comment)

@ghost
Copy link

ghost commented May 17, 2017

@kenju your suggestion was helpful, thank you for leaving the note!

@kenju
Copy link
Contributor

kenju commented Jul 25, 2017

@Simproduction Does my comment helps your case?

FYI @ljharb I think you can close this issue 😉

@ljharb ljharb closed this as completed Jul 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants