Repository: element-plus/element-plus
File: packages/components/select/src/useSelect.ts (TypeScript)

Code before:
    }
    return null
  }

  const focusOption = (targetIndex: number, mode: 'up' | 'down') => {
    const len = states.options.size
    if (len === 0) return
    const idx = Math.min(Math.max(Math.floor(targetIndex), 0), len - 1)
    let newIndex = null
    switch (mode) {
      case 'up':
        newIndex =
          findFocusableIndex(optionsArray.value, idx, -1, len) ??
          findFocusableIndex(optionsArray.value, idx + 1, 1, len)
        break

      case 'down':
        newIndex =
          findFocusableIndex(optionsArray.value, idx, 1, len) ??
          findFocusableIndex(optionsArray.value, idx - 1, -1, len)
        break

      default:
        newIndex = findFocusableIndex(optionsArray.value, idx, 1, len)
        break
    }
    if (newIndex != null) {
      states.hoveringIndex = newIndex
      nextTick(() => scrollToOption(hoverOption.value))
    }
  }

  const handleKeydown = (e: KeyboardEvent) => {
    const code = getEventCode(e)
    let isPreventDefault = true
    switch (code) {
      case EVENT_CODE.up:
        navigateOptions('prev')
        break
      case EVENT_CODE.down:
        navigateOptions('next')
        break
      case EVENT_CODE.enter:
        selectOption()
        break
      case EVENT_CODE.esc:
        handleEsc()
        break
      case EVENT_CODE.backspace:
        isPreventDefault = false
        deletePrevTag(e)

Review comment (suggestion):
Just wanted to make the code look a bit cleaner.

```suggestion
    const start = clamp(targetIndex, 0, len - 1)
    const options = optionsArray.value
    const direction = mode === 'up' ? -1 : 1
    const newIndex =
      findFocusableIndex(options, start, direction, len) ??
      findFocusableIndex(options, start - direction, -direction, len)

```

Code after:
      }
    }
    return null
  }

  const focusOption = (targetIndex: number, mode: 'up' | 'down') => {
    const len = states.options.size
    if (len === 0) return
    const start = clamp(targetIndex, 0, len - 1)
    const options = optionsArray.value
    const direction = mode === 'up' ? -1 : 1
    const newIndex =
      findFocusableIndex(options, start, direction, len) ??
      findFocusableIndex(options, start - direction, -direction, len)

    if (newIndex != null) {
      states.hoveringIndex = newIndex
      nextTick(() => scrollToOption(hoverOption.value))
    }
  }

  const handleKeydown = (e: KeyboardEvent) => {
    const code = getEventCode(e)
    let isPreventDefault = true
    switch (code) {
      case EVENT_CODE.up:
        navigateOptions('prev')
        break
      case EVENT_CODE.down:
        navigateOptions('next')
        break
      case EVENT_CODE.enter:
        selectOption()
        break
      case EVENT_CODE.esc:
        handleEsc()
        break
      case EVENT_CODE.backspace:
        isPreventDefault = false
        deletePrevTag(e)
        return
      case EVENT_CODE.home:
        if (!expanded.value) return
        focusOption(0, 'down')
        break
      case EVENT_CODE.end:
        if (!expanded.value) return
        focusOption(states.options.size - 1, 'up')
        break
      case EVENT_CODE.pageUp:
        if (!expanded.value) return