-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
359 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { forwardRef, useCallback, useEffect, useRef, useImperativeHandle } from 'react'; | ||
import introJs from 'intro.js'; | ||
import { isEmpty, isNumber, isUndefined } from 'lodash'; | ||
import 'intro.js/introjs.css'; | ||
|
||
import './intro.css'; | ||
|
||
export interface IntroRefType extends introJs.IntroJs {} | ||
|
||
interface Props { | ||
enabled: boolean; // 是否启用 | ||
steps: introJs.Step[]; // 步骤 | ||
options?: introJs.Options; // 配置 | ||
initialStep?: number; // 初始化时的所在步骤 | ||
onExit?: Parameters<introJs.IntroJs['onexit']>[0]; | ||
onBeforeExit?: Parameters<introJs.IntroJs['onbeforeexit']>[0]; | ||
onBeforeChange?: Parameters<introJs.IntroJs['onbeforechange']>[0]; | ||
onAfterChange?: Parameters<introJs.IntroJs['onafterchange']>[0]; | ||
onChange?: Parameters<introJs.IntroJs['onchange']>[0]; | ||
onComplete?: Parameters<introJs.IntroJs['oncomplete']>[0]; | ||
} | ||
|
||
/** | ||
* 用户指引组件 | ||
* @constructor | ||
*/ | ||
const Intro = forwardRef<IntroRefType, Props>(( { | ||
enabled, | ||
steps, | ||
options = {}, | ||
initialStep, | ||
onExit, | ||
onBeforeExit, | ||
onBeforeChange, | ||
onAfterChange, | ||
onChange, | ||
onComplete, | ||
}, | ||
ref) => { | ||
const introJsRef = useRef<introJs.IntroJs>(introJs()); | ||
|
||
useImperativeHandle(ref, () => introJsRef.current); | ||
|
||
useEffect(() => { | ||
if (enabled) { | ||
// 写入配置 | ||
configureIntroJs(); | ||
// 开始渲染 | ||
renderSteps(); | ||
} else { | ||
introJsRef.current.exit(); | ||
} | ||
}, [enabled, steps, initialStep]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
introJsRef.current.exit(); | ||
}; | ||
}, []); | ||
|
||
// 初始化配置 | ||
const configureIntroJs = () => { | ||
const defaultOptions: introJs.Options = { | ||
nextLabel: '<span style="display: inline-block">下一步</span>', | ||
prevLabel: '<span style="display: inline-block">上一步</span>', | ||
doneLabel: '<span style="display: inline-block">完成</span>', | ||
buttonClass: 'c7n-pro-btn-wrapper c7n-pro-btn c7n-pro-btn-raised c7n-pro-btn-default', | ||
}; | ||
|
||
introJsRef.current.setOptions({ ...defaultOptions, ...options, steps }); | ||
onExit && introJsRef.current.onexit(onExit); | ||
if (!isUndefined(onBeforeExit)) { | ||
introJsRef.current.onbeforeexit(onBeforeExit); | ||
} | ||
onBeforeChange && introJsRef.current.onbeforechange(onBeforeChange); | ||
onAfterChange && introJsRef.current.onafterchange(onAfterChange); | ||
onChange && introJsRef.current.onchange(onChange); | ||
onComplete && introJsRef.current.oncomplete(onComplete); | ||
}; | ||
|
||
const renderSteps = useCallback(() => { | ||
if (enabled && !isEmpty(steps)) { | ||
introJsRef.current.start(); | ||
} | ||
if (isNumber(initialStep)) { | ||
introJsRef.current.goToStepNumber(initialStep + 1); | ||
} | ||
}, [enabled, steps, initialStep]); | ||
|
||
return null; | ||
}); | ||
|
||
export default Intro; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import introJs from "intro.js"; | ||
|
||
import Intro from '../../components/Intro'; | ||
// Add react-live imports you need here | ||
const ReactLiveScope = { | ||
React, | ||
...React, | ||
Intro, | ||
introJs | ||
}; | ||
export default ReactLiveScope; |
Oops, something went wrong.