"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * When triggered, automatically focus the first element ref passed to this object.
 *
 * To unconditionally focus a single element:
 *
 * ```
 * class SomeComponent extends React.Component {
 *   constructor(props) {
 *     super(props);
 *     this.autofocus = new Autofocus();
 *   }
 *
 *   render() {
 *     return (
 *       <div className="github-Form">
 *         <input ref={this.autofocus.target} type="text" />
 *         <input type="text" />
 *       </div>
 *     );
 *   }
 *
 *   componentDidMount() {
 *     this.autofocus.trigger();
 *   }
 * }
 * ```
 *
 * If multiple form elements are present, use `firstTarget` to create the ref instead. The rendered ref you assign the
 * lowest numeric index will be focused on trigger:
 *
 * ```
 * class SomeComponent extends React.Component {
 *   constructor(props) {
 *     super(props);
 *     this.autofocus = new Autofocus();
 *   }
 *
 *   render() {
 *     return (
 *       <div className="github-Form">
 *         {this.props.someProp && <input ref={this.autofocus.firstTarget(0)} />}
 *         <input ref={this.autofocus.firstTarget(1)} type="text" />
 *         <input type="text" />
 *       </div>
 *     );
 *   }
 *
 *   componentDidMount() {
 *     this.autofocus.trigger();
 *   }
 * }
 * ```
 *
 */
class AutoFocus {
  constructor() {
    _defineProperty(this, "target", element => this.firstTarget(0)(element));
    _defineProperty(this, "firstTarget", index => element => {
      if (index < this.index) {
        this.index = index;
        this.captured = element;
      }
    });
    this.index = Infinity;
    this.captured = null;
  }
  trigger() {
    if (this.captured !== null) {
      setTimeout(() => this.captured.focus(), 0);
    }
  }
}
exports.default = AutoFocus;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJBdXRvRm9jdXMiLCJjb25zdHJ1Y3RvciIsIl9kZWZpbmVQcm9wZXJ0eSIsImVsZW1lbnQiLCJmaXJzdFRhcmdldCIsImluZGV4IiwiY2FwdHVyZWQiLCJJbmZpbml0eSIsInRyaWdnZXIiLCJzZXRUaW1lb3V0IiwiZm9jdXMiLCJleHBvcnRzIiwiZGVmYXVsdCJdLCJzb3VyY2VzIjpbImF1dG9mb2N1cy5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyIvKipcbiAqIFdoZW4gdHJpZ2dlcmVkLCBhdXRvbWF0aWNhbGx5IGZvY3VzIHRoZSBmaXJzdCBlbGVtZW50IHJlZiBwYXNzZWQgdG8gdGhpcyBvYmplY3QuXG4gKlxuICogVG8gdW5jb25kaXRpb25hbGx5IGZvY3VzIGEgc2luZ2xlIGVsZW1lbnQ6XG4gKlxuICogYGBgXG4gKiBjbGFzcyBTb21lQ29tcG9uZW50IGV4dGVuZHMgUmVhY3QuQ29tcG9uZW50IHtcbiAqICAgY29uc3RydWN0b3IocHJvcHMpIHtcbiAqICAgICBzdXBlcihwcm9wcyk7XG4gKiAgICAgdGhpcy5hdXRvZm9jdXMgPSBuZXcgQXV0b2ZvY3VzKCk7XG4gKiAgIH1cbiAqXG4gKiAgIHJlbmRlcigpIHtcbiAqICAgICByZXR1cm4gKFxuICogICAgICAgPGRpdiBjbGFzc05hbWU9XCJnaXRodWItRm9ybVwiPlxuICogICAgICAgICA8aW5wdXQgcmVmPXt0aGlzLmF1dG9mb2N1cy50YXJnZXR9IHR5cGU9XCJ0ZXh0XCIgLz5cbiAqICAgICAgICAgPGlucHV0IHR5cGU9XCJ0ZXh0XCIgLz5cbiAqICAgICAgIDwvZGl2PlxuICogICAgICk7XG4gKiAgIH1cbiAqXG4gKiAgIGNvbXBvbmVudERpZE1vdW50KCkge1xuICogICAgI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