public componentDidUpdate()

in packages/react/src/components/ComboBox/ComboBox.tsx [319:382]


  public componentDidUpdate(prevProps: IComboBoxInternalProps, prevState: IComboBoxState) {
    const {
      allowFreeform,
      text,
      onMenuOpen,
      onMenuDismissed,
      hoisted: { selectedIndices },
    } = this.props;
    const { isOpen, currentPendingValueValidIndex } = this.state;

    // If we are newly open or are open and the pending valid index changed,
    // make sure the currently selected/pending option is scrolled into view
    if (isOpen && (!prevState.isOpen || prevState.currentPendingValueValidIndex !== currentPendingValueValidIndex)) {
      // Need this timeout so that the selectedElement ref is correctly updated
      this._async.setTimeout(() => this._scrollIntoView(), 0);
    }

    // if an action is taken that put focus in the ComboBox
    // and If we are open or we are just closed, shouldFocusAfterClose is set,
    // but we are not the activeElement set focus on the input
    if (
      this._hasFocus() &&
      (isOpen ||
        (prevState.isOpen &&
          !isOpen &&
          this._focusInputAfterClose &&
          this._autofill.current &&
          document.activeElement !== this._autofill.current.inputElement))
    ) {
      this.focus(undefined /*shouldOpenOnFocus*/, true /*useFocusAsync*/);
    }

    // If we should focusAfterClose AND
    //   just opened/closed the menu OR
    //   are focused AND
    //     updated the selectedIndex with the menu closed OR
    //     are not allowing freeform OR
    //     the value changed
    // we need to set selection
    if (
      this._focusInputAfterClose &&
      ((prevState.isOpen && !isOpen) ||
        (this._hasFocus() &&
          ((!isOpen &&
            !this.props.multiSelect &&
            prevProps.hoisted.selectedIndices &&
            selectedIndices &&
            prevProps.hoisted.selectedIndices[0] !== selectedIndices[0]) ||
            !allowFreeform ||
            text !== prevProps.text)))
    ) {
      this._onFocus();
    }

    this._notifyPendingValueChanged(prevState);

    if (isOpen && !prevState.isOpen && onMenuOpen) {
      onMenuOpen();
    }

    if (!isOpen && prevState.isOpen && onMenuDismissed) {
      onMenuDismissed();
    }
  }