mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-17 23:50:52 +08:00

* basic flex * chore: style basic * chore: status style * chore: small size * chore: label vertical * chore: more style * chore: hover disabled * chore: variant * chore: dot variant * chore: max width * chore: hover dot * chore: update style * chore: click style * chore: init percent * chore: pass detail * chore: for progress * chore: clean up * chore: clean up * chore: multiple css var * chore: status text * chore: status use var * chore: fix nest * chore: use svg * chore: type of panel * chore: offset support * chore: inline offset * chore: warning of steps * docs: add semantic preview * chore: semantic transition * chore: hover * chore: adjust style * chore: update semantic preview * chore: fix nav style * chore: patch panel motion * chore: update semantic preview * docs: update config provider doc * chore: fix part ts def * chore: fix test ts * test: update test case * test: coverage * chore: fix lint * test: update test case * test: update snapshot * chore: adjust style * chore: fix rtl * docs: update design * chore: update sctructure * chore: update comment * chore: clean up * chore: clean up * chore: cursor style * chore: coverage * docs: add missing part * chore: order * test: add test case * chore: fix ts * chore: clean up
78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import React, { useContext, useRef } from 'react';
|
|
import isVisible from '@rc-component/util/lib/Dom/isVisible';
|
|
import { composeRef, getNodeRef, supportRef } from '@rc-component/util/lib/ref';
|
|
import classNames from 'classnames';
|
|
|
|
import type { ConfigConsumerProps } from '../../config-provider';
|
|
import { ConfigContext } from '../../config-provider';
|
|
import { cloneElement } from '../reactNode';
|
|
import type { WaveComponent } from './interface';
|
|
import useStyle from './style';
|
|
import useWave from './useWave';
|
|
|
|
export interface WaveProps {
|
|
disabled?: boolean;
|
|
children?: React.ReactNode;
|
|
component?: WaveComponent;
|
|
colorSource?: 'color' | 'backgroundColor' | 'borderColor' | null;
|
|
}
|
|
|
|
const Wave: React.FC<WaveProps> = (props) => {
|
|
const { children, disabled, component, colorSource } = props;
|
|
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
|
const containerRef = useRef<HTMLElement>(null!);
|
|
|
|
// ============================== Style ===============================
|
|
const prefixCls = getPrefixCls('wave');
|
|
const [, hashId] = useStyle(prefixCls);
|
|
|
|
// =============================== Wave ===============================
|
|
const showWave = useWave(containerRef, classNames(prefixCls, hashId), component, colorSource);
|
|
|
|
// ============================== 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;
|
|
}
|
|
showWave(e);
|
|
};
|
|
|
|
// Bind events
|
|
node.addEventListener('click', onClick, true);
|
|
return () => {
|
|
node.removeEventListener('click', onClick, true);
|
|
};
|
|
}, [disabled]);
|
|
|
|
// ============================== Render ==============================
|
|
if (!React.isValidElement(children)) {
|
|
return children ?? null;
|
|
}
|
|
|
|
const ref = supportRef(children) ? composeRef(getNodeRef(children), containerRef) : containerRef;
|
|
|
|
return cloneElement(children, { ref });
|
|
};
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Wave.displayName = 'Wave';
|
|
}
|
|
|
|
export default Wave;
|