@yandex/ui
Version:
Yandex UI components
51 lines (50 loc) • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withControl = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var getDisplayName_1 = require("../lib/getDisplayName");
function withControl(WrappedComponent) {
var WithControl = /** @class */ (function (_super) {
tslib_1.__extends(WithControl, _super);
function WithControl() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
hovered: _this.props.hovered,
};
_this.onMouseEnter = function (event) {
_this.setState({ hovered: true });
if (_this.props.onMouseEnter !== undefined) {
_this.props.onMouseEnter(event);
}
};
_this.onMouseLeave = function (event) {
_this.setState({ hovered: false });
if (_this.props.onMouseLeave !== undefined) {
_this.props.onMouseLeave(event);
}
};
return _this;
}
// TODO: https://st.yandex-team.ru/ISL-5863
// eslint-disable-next-line camelcase
WithControl.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) {
this.setState({
hovered: nextProps.disabled ? false : this.state.hovered || nextProps.hovered,
});
};
WithControl.prototype.render = function () {
var hovered = this.state.hovered;
var disabled = this.props.disabled;
// Оборачиваемый компонент явно может не реализовывать интерфейс для передаваемых свойств,
// но все компоненты обязаны поддержать передачу всех свойств на DOM узел, поэтому тут мы
// приводим тип оборачиваемого компонента к нужному виду.
var Component = WrappedComponent;
return (react_1.default.createElement(Component, tslib_1.__assign({}, this.props, { hovered: !disabled ? hovered : undefined, onMouseEnter: !disabled ? this.onMouseEnter : undefined, onMouseLeave: !disabled ? this.onMouseLeave : undefined })));
};
WithControl.displayName = "WithControl@desktop(" + getDisplayName_1.getDisplayName(WrappedComponent) + ")";
return WithControl;
}(react_1.PureComponent));
return WithControl;
}
exports.withControl = withControl;