@yandex/ui
Version:
Yandex UI components
34 lines (33 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAutoFocus = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var getDisplayName_1 = require("../lib/getDisplayName");
var mergeRefs_1 = require("../lib/mergeRefs");
var withAutoFocus = function (WrappedComponent) { var _a; return _a = /** @class */ (function (_super) {
tslib_1.__extends(WithAutoFocus, _super);
function WithAutoFocus() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.controlRef = react_1.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_1.default.createElement(WrappedComponent, tslib_1.__assign({}, this.props, { controlRef: mergeRefs_1.mergeAllRefs(this.controlRef, controlRef) }));
};
return WithAutoFocus;
}(react_1.PureComponent)),
_a.displayName = "withAutoFocus(" + getDisplayName_1.getDisplayName(WrappedComponent) + ")",
_a; };
exports.withAutoFocus = withAutoFocus;