@yandex/ui
Version:
Yandex UI components
30 lines (29 loc) • 1.4 kB
JavaScript
import { __assign, __extends } from "tslib";
import React, { PureComponent, createRef } from 'react';
import { getDisplayName } from '../lib/getDisplayName';
import { mergeAllRefs } from '../lib/mergeRefs';
export var withAutoFocus = function (WrappedComponent) { var _a; return _a = /** @class */ (function (_super) {
__extends(WithAutoFocus, _super);
function WithAutoFocus() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.controlRef = createRef();
return _this;
}
WithAutoFocus.prototype.componentDidUpdate = function (prevProps) {
if (this.controlRef.current !== null && this.props.focused !== prevProps.focused) {
if (this.props.focused) {
this.controlRef.current.focus();
}
else {
this.controlRef.current.blur();
}
}
};
WithAutoFocus.prototype.render = function () {
var _a = this.props.controlRef, controlRef = _a === void 0 ? null : _a;
return React.createElement(WrappedComponent, __assign({}, this.props, { controlRef: mergeAllRefs(this.controlRef, controlRef) }));
};
return WithAutoFocus;
}(PureComponent)),
_a.displayName = "withAutoFocus(" + getDisplayName(WrappedComponent) + ")",
_a; };