2024-02-24 14:50:03 +08:00
|
|
|
import React, { useContext, useRef } from 'react';
|
2022-12-28 23:20:22 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import isVisible from 'rc-util/lib/Dom/isVisible';
|
2024-02-24 14:50:03 +08:00
|
|
|
import { composeRef, supportRef } from 'rc-util/lib/ref';
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
import type { ConfigConsumerProps } from '../../config-provider';
|
|
|
|
import { ConfigContext } from '../../config-provider';
|
|
|
|
import { cloneElement } from '../reactNode';
|
|
|
|
import useStyle from './style';
|
|
|
|
import useWave from './useWave';
|
|
|
|
|
|
|
|
export interface WaveProps {
|
|
|
|
disabled?: boolean;
|
|
|
|
children?: React.ReactNode;
|
2024-03-26 18:20:28 +08:00
|
|
|
component?: 'Tag' | 'Button' | 'Checkbox' | 'Radio' | 'Switch';
|
2022-12-28 23:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Wave: React.FC<WaveProps> = (props) => {
|
2023-07-25 16:29:47 +08:00
|
|
|
const { children, disabled, component } = props;
|
2022-12-28 23:20:22 +08:00
|
|
|
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const containerRef = useRef<HTMLElement>(null);
|
|
|
|
|
|
|
|
// ============================== Style ===============================
|
|
|
|
const prefixCls = getPrefixCls('wave');
|
|
|
|
const [, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
|
|
// =============================== Wave ===============================
|
2023-07-25 16:29:47 +08:00
|
|
|
const showWave = useWave(containerRef, classNames(prefixCls, hashId), component);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
// ============================== Effect ==============================
|
|
|
|
React.useEffect(() => {
|
|
|
|
const node = containerRef.current;
|
|
|
|
if (!node || node.nodeType !== 1 || disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Click handler
|
|
|
|
const onClick = (e: MouseEvent) => {
|
|
|
|
// Fix radio button click twice
|
|
|
|
if (
|
|
|
|
!isVisible(e.target as HTMLElement) ||
|
|
|
|
// No need wave
|
|
|
|
!node.getAttribute ||
|
|
|
|
node.getAttribute('disabled') ||
|
|
|
|
(node as HTMLInputElement).disabled ||
|
|
|
|
node.className.includes('disabled') ||
|
|
|
|
node.className.includes('-leave')
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-25 16:29:47 +08:00
|
|
|
showWave(e);
|
2022-12-28 23:20:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bind events
|
|
|
|
node.addEventListener('click', onClick, true);
|
|
|
|
return () => {
|
|
|
|
node.removeEventListener('click', onClick, true);
|
|
|
|
};
|
|
|
|
}, [disabled]);
|
|
|
|
|
|
|
|
// ============================== Render ==============================
|
|
|
|
if (!React.isValidElement(children)) {
|
2024-02-24 14:50:03 +08:00
|
|
|
return children ?? null;
|
2022-12-28 23:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ref = supportRef(children) ? composeRef((children as any).ref, containerRef) : containerRef;
|
|
|
|
|
|
|
|
return cloneElement(children, { ref });
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Wave.displayName = 'Wave';
|
|
|
|
}
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
export default Wave;
|