micro-ui-y
Version:
231 lines (207 loc) • 9.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var styles = {
tipBox: {
maxWidth: "30rem",
minWidth: "10rem",
display: 'flex',
position: 'absolute',
borderRadius: '0.25rem',
textAlign: 'justify',
padding: '0.3125rem',
boxSizing: "border-box",
boxShadow: "0 0.3125rem 0.625rem rgba(255,151,59,0.3)",
// webkitBoxShadow: "0 0.3125rem 0.625rem rgba(255,151,59,0.3)",
zIndex: "1000"
// cursor:"text"
},
bg: {
width: "100%",
height: "100%",
background: "rgba(0, 0, 0, 0)",
position: "fixed",
top: "0",
left: "0",
zIndex: "999"
},
trigger: {
position: 'absolute',
borderStyle: 'solid',
borderWidth: "0.625rem 0.5rem"
}
};
var Tip = function (_Component) {
_inherits(Tip, _Component);
function Tip(props) {
_classCallCheck(this, Tip);
var _this = _possibleConstructorReturn(this, (Tip.__proto__ || Object.getPrototypeOf(Tip)).call(this, props));
_this.proportionFn = function () {
var browserInfo = _this.browser();
var chromeVersion = browserInfo.chrome;
var proportion = window.innerWidth * 16 / 1920;
if (chromeVersion < 79) {
var devicePixelRatio = window.devicePixelRatio;
proportion = proportion * devicePixelRatio <= 12 ? 12 : proportion * devicePixelRatio;
}
return proportion;
};
_this.browser = function () {
var ua = navigator.userAgent;
var ret = {};
var webkit = ua.match(/WebKit\/([\d.]+)/);
var chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/);
// eslint-disable-next-line no-useless-escape
var ie = ua.match(/MSIE\s([\d\.]+)/) || ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i);
var firefox = ua.match(/Firefox\/([\d.]+)/);
var safari = ua.match(/Safari\/([\d.]+)/);
var opera = ua.match(/OPR\/([\d.]+)/);
webkit && (ret.webkit = parseFloat(webkit[1]));
chrome && (ret.chrome = parseFloat(chrome[1]));
ie && (ret.ie = parseFloat(ie[1]));
firefox && (ret.firefox = parseFloat(firefox[1]));
safari && (ret.safari = parseFloat(safari[1]));
opera && (ret.opera = parseFloat(opera[1]));
return ret;
};
_this.showTipBox = function (type, e) {
e.nativeEvent.stopImmediatePropagation();
var formBox = _this.refs.formBox;
// const tipBoxHeight = parseInt(this.state.tipBoxHeight, 10) * 16;
var marginTop = e.nativeEvent.clientY - e.nativeEvent.offsetY;
var marginBottom = window.innerHeight - marginTop - formBox.offsetHeight;
if (marginBottom > marginTop) {
_this.setState({
selLocation: "bottom"
});
} else {
_this.setState({
selLocation: "top"
});
}
_this.setState({
isClick: type === 'hover' ? true : !_this.state.isClick,
demoHeight: formBox.offsetHeight,
demoWidth: formBox.offsetWidth
});
};
_this.closeTipBox = function () {
console.log("aaaaaa");
_this.setState({
isClick: false
});
};
_this.state = {
isClick: false,
// tipBoxWidth: '',
tipBoxHeight: '',
selLocation: false,
demoHeight: ''
};
return _this;
}
_createClass(Tip, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
// tipBoxWidth: this.props.tipBoxWidth ? this.props.tipBoxWidth : '12.5rem',
// tipBoxHeight: this.props.tipBoxHeight ? this.props.tipBoxHeight : '5rem',
proportion: this.proportionFn()
});
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(preProps, preState) {
//加判断防止一直循环渲染
if (this.state.isClick && !preState.isClick) {
var tipBox = this.refs.tipBox;
console.log(tipBox.offsetHeight);
this.setState({
tipBoxHeight: tipBox.offsetHeight,
tipBoxWidth: tipBox.offsetWidth
});
}
} //计算录入表单最大高度
}, {
key: "render",
value: function render() {
var proportion = this.state.proportion;
var marginHeiht = this.state.selLocation === "bottom" ? this.state.demoHeight : this.state.tipBoxHeight; //marginTop的值
var marginTop = this.state.selLocation === "bottom" ? marginHeiht / proportion + 0.625 + 'rem' : -(marginHeiht / proportion + 0.625) + 'rem';
var marinLeft = this.props.placement === "left" ? -((this.state.tipBoxWidth * 0.9 - this.state.demoWidth / 2) / proportion - 0.5) + 'rem' : (this.state.demoWidth / 2 - this.state.tipBoxWidth * 0.1) / proportion - 0.5 + 'rem';
var tipBoxStyle = _extends({}, styles.tipBox, {
transform: "translate(" + marinLeft + "," + (this.state.isClick ? marginTop : "-100rem") + ")",
display: "" + (this.state.isClick ? "flex" : "none"),
// transition: `opacity 0.5s ease`,
border: "0.0625rem solid " + (this.props.borderColor ? this.props.borderColor : "#ff973b"),
background: this.props.backgroundColor ? this.props.backgroundColor : "#faded1"
});
var triggerStyle = this.state.selLocation === "bottom" ? _extends({}, styles.trigger, {
borderColor: "transparent transparent " + (this.props.borderColor ? this.props.borderColor : "#ff973b") + " transparent",
bottom: this.state.tipBoxHeight / proportion - 0.125 + 'rem'
}) : _extends({}, styles.trigger, {
borderColor: (this.props.borderColor ? this.props.borderColor : "#ff973b") + " transparent transparent transparent",
top: this.state.tipBoxHeight / proportion - 0.125 + 'rem'
});
this.props.placement === "left" ? Object.assign(triggerStyle, { right: "10%" }) : Object.assign(triggerStyle, { left: "10%" });
return _react2.default.createElement(
"div",
null,
_react2.default.createElement(
"div",
{
style: { position: 'absolute', cursor: "pointer" },
ref: "formBox",
onClick: this.props.showType === 'click' ? this.showTipBox.bind(this, 'click') : null,
onMouseEnter: this.props.showType === 'hover' ? this.showTipBox.bind(this, 'hover') : null,
onMouseLeave: this.props.showType === 'hover' ? this.closeTipBox : null
//style={{
// border: '1px solid black',
// whiteSpace: 'nowrap',
// overflow: 'hidden',
// textOverflow: 'ellipsis',
// cursor: 'pointer',
//}}
},
this.props.children
),
_react2.default.createElement(
"div",
{ ref: "tipBox",
onMouseEnter: this.props.showType === 'hover' ? this.showTipBox.bind(this, 'hover') : null,
onMouseLeave: this.props.showType === 'hover' ? this.closeTipBox : null,
style: tipBoxStyle },
_react2.default.createElement("div", { style: triggerStyle }),
_react2.default.createElement(
"div",
{ style: {
// width: this.state.tipBoxWidth,
// height: (this.state.tipBoxHeight)/16+'rem',
// padding: '0.3125rem',
fontSize: "0.75rem"
} },
this.props.title
)
),
this.state.isClick && this.props.showType === "click" ? _react2.default.createElement("div", { onClick: this.closeTipBox.bind(this),
style: styles.bg }) : null
);
}
}]);
return Tip;
}(_react.Component);
Tip.defaultProps = {
placement: "left",
showType: "hover"
};
exports.default = Tip;
//# sourceMappingURL=tip.js.map