All files Intro.jsx

53.33% Statements 16/30
42.86% Branches 3/7
62.5% Functions 5/8
51.72% Lines 15/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                2x 2x 2x 2x 2x 2x                                 2x   2x                   2x             2x 2x 2x 2x 2x   2x              
import React from 'react';
import ReactDom from 'react-dom';
import { newController, getController } from './Controller';
import { prefix } from "./config";
import DefaultToolTip from "./DefaultToolTip";
import { findParentDomHasZ, addFixClass, removeFixClass } from './utils';
 
class Intro extends React.Component {
	static propsTypes = {
		name: String,
		step: Number,
		placement: String,
		tooltip: React.dom
	}
	static defaultProps = {
		name: 'global',
		step: 1,
		placement: 'top',
		tooltip: <DefaultToolTip />
	}
	state = {
		fixList: []
	}
	constructor(props) {
		super(props);
		const { name = 'global', step } = props;
		this.name = name;
		this.step = step;
		this.checkProps(props)
		this.controller = newController(this);
	}
	setActive() {
		const fixList = findParentDomHasZ(this.el);
		addFixClass(fixList, `${prefix}-fixParent`)
		this.setState({
			fixList,
		});
	}
	unsetActive() {
		const { fixList } = this.state;
		removeFixClass(fixList, `${prefix}-fixParent`)
		this.setState({
			fixList: []
		});
	}
	checkProps(props) {
		const { step } = props;
		// 检测step
		Iif (step === undefined) {
			throw new Error('step属性不能为空');
		}
	}
	getTooltipProps = () => {
		const { controller } = this;
		const { step, count, start, end, goToStep, nextStep, prevStep, goToStepNumber } = controller;
		return { step, count, start, end, goToStep, nextStep, prevStep, goToStepNumber }
	}
	renderTooltip = (display) => {
		Eif (!display) return
		const { tooltip } = this.props;
		const { tooltipLayout } = this.controller;
		const tooltipProps = this.getTooltipProps();
		return ReactDom.createPortal(React.cloneElement(tooltip, tooltipProps), tooltipLayout)
	}
	render() {
		const { step: controllerStep } = this.controller;
		const { step } = this.props;
		const display = step === controllerStep;
		const className = display ? `${prefix}-relativePosition ${prefix}-showElement` : null;
		return <span
			style={{ display: 'inline-block' }}
			ref={(el) => this.el = el}
			className={className}>
			{this.props.children}
			{this.renderTooltip(display)}
		</span>;
	}
}
export default Intro;