2019-02-19 11:42:05 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-11-21 15:40:06 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2022-03-08 18:13:51 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
export interface TypographyProps {
|
|
|
|
id?: string;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
['aria-label']?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface InternalTypographyProps extends TypographyProps {
|
|
|
|
component?: string;
|
2019-10-01 14:06:09 +08:00
|
|
|
/** @deprecated Use `ref` directly if using React 16 */
|
2019-03-05 13:48:38 +08:00
|
|
|
setContentRef?: (node: HTMLElement) => void;
|
2019-02-19 11:42:05 +08:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:12:31 +08:00
|
|
|
const Typography: React.ForwardRefRenderFunction<{}, InternalTypographyProps> = (
|
2019-10-01 14:06:09 +08:00
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
component = 'article',
|
|
|
|
className,
|
|
|
|
'aria-label': ariaLabel,
|
|
|
|
setContentRef,
|
|
|
|
children,
|
|
|
|
...restProps
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
2022-03-08 18:13:51 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2019-10-01 14:06:09 +08:00
|
|
|
|
2022-03-08 18:13:51 +08:00
|
|
|
let mergedRef = ref;
|
2019-10-01 14:06:09 +08:00
|
|
|
if (setContentRef) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(false, 'Typography', '`setContentRef` is deprecated. Please use `ref` instead.');
|
2019-10-01 14:06:09 +08:00
|
|
|
mergedRef = composeRef(ref, setContentRef);
|
2019-09-30 19:37:42 +08:00
|
|
|
}
|
2019-10-01 14:06:09 +08:00
|
|
|
|
2022-03-08 18:13:51 +08:00
|
|
|
const Component = component as any;
|
|
|
|
const prefixCls = getPrefixCls('typography', customizePrefixCls);
|
|
|
|
const componentClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2019-10-01 14:06:09 +08:00
|
|
|
return (
|
2022-03-08 18:13:51 +08:00
|
|
|
<Component className={componentClassName} aria-label={ariaLabel} ref={mergedRef} {...restProps}>
|
|
|
|
{children}
|
|
|
|
</Component>
|
2019-10-01 14:06:09 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-20 19:18:53 +08:00
|
|
|
const RefTypography = React.forwardRef(Typography);
|
2019-10-01 14:06:09 +08:00
|
|
|
|
2020-04-20 19:18:53 +08:00
|
|
|
RefTypography.displayName = 'Typography';
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2019-10-01 14:06:09 +08:00
|
|
|
// es default export should use const instead of let
|
2022-03-08 18:13:51 +08:00
|
|
|
const ExportTypography = RefTypography as unknown as React.FC<TypographyProps>;
|
2019-10-01 14:06:09 +08:00
|
|
|
|
|
|
|
export default ExportTypography;
|