yj-kikimore 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import './highlightError.scss'
2
+ import elementIsVisible from './elementIsVisible'
3
+
4
+ export default (
5
+ selectors: string | Element | NodeList = '.el-form .el-form-item.is-error',
6
+ container = window,
7
+ ): void => {
8
+ const scrollIntoView = (element) => {
9
+ element.scrollIntoView({
10
+ behavior: 'smooth',
11
+ block: 'center',
12
+ })
13
+ }
14
+
15
+ const animateCSS = (el, animationName) =>
16
+ new Promise<void>((resolve, reject) => {
17
+ if (el) {
18
+ // @ts-expect-error: none
19
+ for (const v of el instanceof NodeList ? el : [el]) {
20
+ v.classList.add('animate__animated', animationName)
21
+
22
+ const handleAnimationEnd = () => {
23
+ v.classList.remove('animate__animated', animationName)
24
+ v.removeEventListener('animationend', handleAnimationEnd)
25
+ resolve()
26
+ }
27
+
28
+ v.addEventListener('animationend', handleAnimationEnd)
29
+ }
30
+ } else {
31
+ reject()
32
+ }
33
+ })
34
+
35
+ // is-error类名需要异步才能获取到
36
+ setTimeout(() => {
37
+ const errFormItems =
38
+ typeof selectors === 'string' ? document.querySelectorAll(selectors) : selectors
39
+
40
+ // 打包后不生效
41
+ /* if (IntersectionObserver) {
42
+ const intersectionObserver = new IntersectionObserver((entries) => {
43
+ let [entry] = entries
44
+ if (entry.isIntersecting) {
45
+ console.log(entry)
46
+ // 对所有校验失败的表单项产生震动效果
47
+ setTimeout(() => {
48
+ animateCSS(errFormItems, 'animate__headShake').catch(e => {
49
+ console.warn(e)
50
+ })
51
+ }, 100)
52
+ }
53
+ })
54
+ intersectionObserver.observe(errFormItems[0])
55
+ } */
56
+
57
+ // 视图滚动至校验失败的第一个表单项
58
+ if (errFormItems[0]) {
59
+ if (elementIsVisible(errFormItems[0])) {
60
+ animateCSS(errFormItems, 'animate__headShake').catch((e) => {
61
+ console.warn(e)
62
+ })
63
+ } else {
64
+ let scrollTimeout: number
65
+
66
+ function shake() {
67
+ // 第二次触发会清除第一次,第三次触发会清除第二次...
68
+ // 直到最后一次超过100毫秒才清除,此时清除已经无效
69
+ // 100毫秒都没有触发,说明滚动停止
70
+ clearTimeout(scrollTimeout)
71
+ scrollTimeout = setTimeout(() => {
72
+ animateCSS(errFormItems, 'animate__headShake').catch((e) => {
73
+ console.warn(e)
74
+ })
75
+ container.removeEventListener('scroll', shake)
76
+ }, 100)
77
+ }
78
+
79
+ container.addEventListener('scroll', shake)
80
+ scrollIntoView(errFormItems[0])
81
+ }
82
+ }
83
+ }, 0)
84
+ }