diff --git a/src/_common b/src/_common index 582fd8a8b6..e6d6e0697a 160000 --- a/src/_common +++ b/src/_common @@ -1 +1 @@ -Subproject commit 582fd8a8b62ea6357bee65fe0cdfd00cd0005bc0 +Subproject commit e6d6e0697afb9a13caab5a31bee8a0a5b4712911 diff --git a/src/watermark/Watermark.tsx b/src/watermark/Watermark.tsx index 384a3fbf7e..54e50cfdd1 100644 --- a/src/watermark/Watermark.tsx +++ b/src/watermark/Watermark.tsx @@ -1,5 +1,6 @@ /* eslint-disable no-nested-ternary */ import React, { useCallback, useState, useEffect, useRef } from 'react'; +import classNames from 'classnames'; import { StyledProps } from '../common'; import generateBase64Url from '../_common/js/watermark/generateBase64Url'; import randomMovingStyle from '../_common/js/watermark/randomMovingStyle'; @@ -45,7 +46,6 @@ const Watermark: React.FC = ({ const clsName = `${classPrefix}-watermark`; const [base64Url, setBase64Url] = useState(''); const styleStr = useRef(''); - const maskclassName = useRef(className); const watermarkRef = useRef(); const watermarkImgRef = useRef(); const stopObservation = useRef(false); @@ -82,8 +82,8 @@ const Watermark: React.FC = ({ right: 0, top: 0, bottom: 0, - width: '100%', - height: '100%', + width: movable ? `${width}px` : '100%', + height: movable ? `${height}px` : '100%', backgroundSize: `${gapX + width}px`, pointerEvents: 'none', backgroundRepeat: movable ? 'no-repeat' : isRepeat ? 'repeat' : 'no-repeat', @@ -91,12 +91,7 @@ const Watermark: React.FC = ({ animation: movable ? `watermark infinite ${(moveInterval * 4) / 60}s` : 'none', ...style, }); - }, [zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style]); - - // 水印节点 - className - useEffect(() => { - maskclassName.current = className; - }, [className]); + }, [zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, height]); // 水印节点 - 渲染 const renderWatermark = useCallback(() => { @@ -108,9 +103,6 @@ const Watermark: React.FC = ({ // 创建新的 watermarkImgRef.current = document.createElement('div'); watermarkImgRef.current.setAttribute('style', styleStr.current); - if (maskclassName.current) { - watermarkImgRef.current.setAttribute('class', maskclassName.current); - } watermarkRef.current?.append(watermarkImgRef.current); // 继续监听 setTimeout(() => { @@ -170,7 +162,7 @@ const Watermark: React.FC = ({ }); return ( -
+
{children || content}
); diff --git a/src/watermark/__tests__/watermark.test.tsx b/src/watermark/__tests__/watermark.test.tsx index 4467edff0a..defd855ab8 100644 --- a/src/watermark/__tests__/watermark.test.tsx +++ b/src/watermark/__tests__/watermark.test.tsx @@ -59,30 +59,33 @@ describe('Watermark 组件测试', () => { child.innerText = 'testing'; child.id = 'test'; + const watermarkWrapCls = wrapper.container.querySelector('.test-observer'); + expect(watermarkWrapCls).not.toBeNull(); const watermarkWrap = wrapper.container.querySelector('.t-watermark'); const watermarkWrapParent = watermarkWrap.parentElement; - const watermarkEle = wrapper.container.querySelector('.test-observer'); + const watermarkEle = watermarkWrap.querySelectorAll('div')[1]; // 删除了水印wrap元素,还会被立即追加回去 watermarkWrapParent.removeChild(watermarkWrap); - const afterWrapRemove = wrapper.container.querySelector('.t-watermark'); + const afterWrapRemove = watermarkWrapParent.querySelector('.t-watermark'); expect(afterWrapRemove).toBeNull(); await mockDelay(10); - const waitAfterWrapRemove = wrapper.container.querySelector('.t-watermark'); + const waitAfterWrapRemove = watermarkWrapParent.querySelector('.t-watermark'); expect(waitAfterWrapRemove).not.toBeNull(); // 删除了水印元素,还会被立即追加回去 - watermarkWrap.removeChild(watermarkEle); - const afterMarkRemove = wrapper.container.querySelector('.test-observer'); - expect(afterMarkRemove).toBeNull(); + waitAfterWrapRemove.removeChild(watermarkEle); + expect(waitAfterWrapRemove.querySelectorAll('div').length).toBe(1); + // const afterMarkRemove = waitAfterWrapRemove.querySelectorAll('div')[1]; + // expect(afterMarkRemove).toBeNull(); await mockDelay(10); - const waitAfterMarkRemove = wrapper.container.querySelector('.test-observer'); + const waitAfterMarkRemove = waitAfterWrapRemove.querySelectorAll('div')[1]; expect(waitAfterMarkRemove).not.toBeNull(); // 修改水印元素的属性,会立即还原 watermarkEle.setAttribute('any', '11'); await mockDelay(10); - const waitAfterAttrChange = wrapper.container.querySelector('.test-observer'); + const waitAfterAttrChange = watermarkWrap.querySelectorAll('div')[1]; expect(waitAfterAttrChange.getAttribute('any')).toBeNull(); }); }); diff --git a/src/watermark/index.ts b/src/watermark/index.ts index ce7ba39d1c..739ea3b84f 100644 --- a/src/watermark/index.ts +++ b/src/watermark/index.ts @@ -1,6 +1,9 @@ import _Watermark from './Watermark'; +import './style/index.js'; + export type { WatermarkProps } from './Watermark'; + export * from './type'; export const Watermark = _Watermark; diff --git a/src/watermark/style/css.js b/src/watermark/style/css.js new file mode 100644 index 0000000000..6a9a4b1328 --- /dev/null +++ b/src/watermark/style/css.js @@ -0,0 +1 @@ +import './index.css'; diff --git a/src/watermark/style/index.js b/src/watermark/style/index.js new file mode 100644 index 0000000000..8832630bb1 --- /dev/null +++ b/src/watermark/style/index.js @@ -0,0 +1 @@ +import '../../_common/style/web/components/watermark/_index.less';