mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 11:10:01 +08:00
feat: Support ref for Title, Text, Paragraph (#34847)
* feat: Support ref for Title, Text, Paragraph * feat: Add tests for Title, Paragraph and Text
This commit is contained in:
parent
c4ffbff841
commit
445e6c6bca
@ -5,6 +5,8 @@ export interface ParagraphProps extends BlockProps {
|
||||
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
|
||||
}
|
||||
|
||||
const Paragraph: React.FC<ParagraphProps> = props => <Base {...props} component="div" />;
|
||||
const Paragraph: React.ForwardRefRenderFunction<HTMLDivElement, ParagraphProps> = (props, ref) => (
|
||||
<Base ref={ref} {...props} component="div" />
|
||||
);
|
||||
|
||||
export default Paragraph;
|
||||
export default React.forwardRef(Paragraph);
|
||||
|
@ -8,7 +8,10 @@ export interface TextProps extends BlockProps {
|
||||
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
|
||||
}
|
||||
|
||||
const Text: React.FC<TextProps> = ({ ellipsis, ...restProps }) => {
|
||||
const Text: React.ForwardRefRenderFunction<HTMLSpanElement, TextProps> = (
|
||||
{ ellipsis, ...restProps },
|
||||
ref,
|
||||
) => {
|
||||
const mergedEllipsis = React.useMemo(() => {
|
||||
if (ellipsis && typeof ellipsis === 'object') {
|
||||
return omit(ellipsis as any, ['expandable', 'rows']);
|
||||
@ -25,7 +28,7 @@ const Text: React.FC<TextProps> = ({ ellipsis, ...restProps }) => {
|
||||
'`ellipsis` do not support `expandable` or `rows` props.',
|
||||
);
|
||||
|
||||
return <Base {...restProps} ellipsis={mergedEllipsis} component="span" />;
|
||||
return <Base ref={ref} {...restProps} ellipsis={mergedEllipsis} component="span" />;
|
||||
};
|
||||
|
||||
export default Text;
|
||||
export default React.forwardRef(Text);
|
||||
|
@ -13,7 +13,7 @@ export type TitleProps = Omit<
|
||||
'strong'
|
||||
>;
|
||||
|
||||
const Title: React.FC<TitleProps> = props => {
|
||||
const Title: React.ForwardRefRenderFunction<HTMLHeadingElement, TitleProps> = (props, ref) => {
|
||||
const { level = 1, ...restProps } = props;
|
||||
let component: string;
|
||||
|
||||
@ -28,7 +28,7 @@ const Title: React.FC<TitleProps> = props => {
|
||||
component = 'h1';
|
||||
}
|
||||
|
||||
return <Base {...restProps} component={component} />;
|
||||
return <Base ref={ref} {...restProps} component={component} />;
|
||||
};
|
||||
|
||||
export default Title;
|
||||
export default React.forwardRef(Title);
|
||||
|
@ -402,4 +402,25 @@ describe('Typography', () => {
|
||||
|
||||
expect(errorSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should get HTMLHeadingElement ref from Title', () => {
|
||||
const ref = React.createRef();
|
||||
|
||||
mount(<Title level={1} ref={ref} />);
|
||||
expect(ref.current instanceof HTMLHeadingElement).toBe(true);
|
||||
});
|
||||
|
||||
it('should get HTMLDivElement ref from Paragraph', () => {
|
||||
const ref = React.createRef();
|
||||
|
||||
mount(<Paragraph ref={ref} />);
|
||||
expect(ref.current instanceof HTMLDivElement).toBe(true);
|
||||
});
|
||||
|
||||
it('should get HTMLSpanElement ref from Text', () => {
|
||||
const ref = React.createRef();
|
||||
|
||||
mount(<Text ref={ref} />);
|
||||
expect(ref.current instanceof HTMLSpanElement).toBe(true);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user