handleDocumentKeyDown = (e) => {
    const isSlash = keyboardKey.getKey(e) === '/'
    const hasModifier = e.altKey || e.ctrlKey || e.metaKey
    const bodyHasFocus = document.activeElement === document.body

    if (!hasModifier && isSlash && bodyHasFocus) this._searchInput.focus()
  }
Beispiel #2
0
  selectItemOnEnter = (e) => {
    debug('selectItemOnEnter()', keyboardKey.getKey(e))
    const { search } = this.props

    if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
    e.preventDefault()

    const optionSize = _.size(this.getMenuOptions())
    if (search && optionSize === 0) return

    this.makeSelectedItemActive(e)
    this.closeOnChange(e)
    this.clearSearchQuery()
    if (search && this.searchRef) this.searchRef.focus()
  }
Beispiel #3
0
  moveSelectionOnKeyDown = (e) => {
    debug('moveSelectionOnKeyDown()', keyboardKey.getKey(e))

    const { multiple, selectOnNavigation } = this.props
    const moves = {
      [keyboardKey.ArrowDown]: 1,
      [keyboardKey.ArrowUp]: -1,
    }
    const move = moves[keyboardKey.getCode(e)]

    if (move === undefined) return
    e.preventDefault()
    this.moveSelectionBy(move)
    if (!multiple && selectOnNavigation) this.makeSelectedItemActive(e)
  }
Beispiel #4
0
 moveSelectionOnKeyDown = (e) => {
   debug('moveSelectionOnKeyDown()')
   debug(keyboardKey.getKey(e))
   switch (keyboardKey.getCode(e)) {
     case keyboardKey.ArrowDown:
       e.preventDefault()
       this.moveSelectionBy(e, 1)
       break
     case keyboardKey.ArrowUp:
       e.preventDefault()
       this.moveSelectionBy(e, -1)
       break
     default:
       break
   }
 }
Beispiel #5
0
  selectItemOnEnter = (e) => {
    debug('selectItemOnEnter()')
    debug(keyboardKey.getKey(e))
    if (keyboardKey.getCode(e) !== keyboardKey.Enter) return

    const result = this.getSelectedResult()

    // prevent selecting null if there was no selected item value
    if (!result) return

    e.preventDefault()

    // notify the onResultSelect prop that the user is trying to change value
    this.setValue(result.title)
    this.handleResultSelect(e, result)
    this.close()
  }
Beispiel #6
0
  removeItemOnBackspace = (e) => {
    debug('removeItemOnBackspace()', keyboardKey.getKey(e))

    const { multiple, search } = this.props
    const { searchQuery, value } = this.state

    if (keyboardKey.getCode(e) !== keyboardKey.Backspace) return
    if (searchQuery || !search || !multiple || _.isEmpty(value)) return
    e.preventDefault()

    // remove most recent value
    const newValue = _.dropRight(value)

    this.setValue(newValue)
    this.setSelectedIndex(newValue)
    this.handleChange(e, newValue)
  }