UNPKG

@yandex/ui

Version:

Yandex UI components

47 lines (46 loc) 2.46 kB
import { __assign, __extends } from "tslib"; import React, { PureComponent } from 'react'; import { getDisplayName } from '../lib/getDisplayName'; export function withControl(WrappedComponent) { var WithControl = /** @class */ (function (_super) { __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.createElement(Component, __assign({}, this.props, { hovered: !disabled ? hovered : undefined, onMouseEnter: !disabled ? this.onMouseEnter : undefined, onMouseLeave: !disabled ? this.onMouseLeave : undefined }))); }; WithControl.displayName = "WithControl@desktop(" + getDisplayName(WrappedComponent) + ")"; return WithControl; }(PureComponent)); return WithControl; }