ant-design/components/typography/Typography.tsx
afc163 59ad48476b
refactor: add boime lint and fix lint errrors (#49536)
* chore: add boime lint

* fix lint

* use files ignore

* revert change

* ignore clarity.js

* fix some errors

* fix some errors

* fix some errors

* fix some errors

* add yml file

* Update clarity.js

Signed-off-by: afc163 <afc163@gmail.com>

* add npm run lint:biome

* add npm run lint:biome

* fix test case

* fix ts errors

* fix ts errors

* fix lint and add .lintstagedrc

* shorten prop name

* chore: update package.json

* update biome.json

* chore: remove stylelint

* chore: useOptionalChain

* fix lint

* biome format

* prettier all code

* prettier all code

* fix site test

---------

Signed-off-by: afc163 <afc163@gmail.com>
2024-06-22 21:59:12 +08:00

97 lines
2.5 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import { composeRef } from 'rc-util/lib/ref';
import { devUseWarning } from '../_util/warning';
import type { ConfigConsumerProps, DirectionType } from '../config-provider';
import { ConfigContext } from '../config-provider';
import useStyle from './style';
export interface TypographyProps<C extends keyof JSX.IntrinsicElements>
extends React.HTMLAttributes<HTMLElement> {
id?: string;
prefixCls?: string;
className?: string;
rootClassName?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
/** @internal */
component?: C;
'aria-label'?: string;
direction?: DirectionType;
}
interface InternalTypographyProps<C extends keyof JSX.IntrinsicElements>
extends TypographyProps<C> {
/** @deprecated Use `ref` directly if using React 16 */
setContentRef?: (node: HTMLElement) => void;
}
const Typography = React.forwardRef<
HTMLElement,
InternalTypographyProps<keyof JSX.IntrinsicElements>
>((props, ref) => {
const {
prefixCls: customizePrefixCls,
component: Component = 'article',
className,
rootClassName,
setContentRef,
children,
direction: typographyDirection,
style,
...restProps
} = props;
const {
getPrefixCls,
direction: contextDirection,
typography,
} = React.useContext<ConfigConsumerProps>(ConfigContext);
const direction = typographyDirection ?? contextDirection;
let mergedRef = ref;
if (setContentRef) {
mergedRef = composeRef(ref, setContentRef);
}
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Typography');
warning.deprecated(!setContentRef, 'setContentRef', 'ref');
}
const prefixCls = getPrefixCls('typography', customizePrefixCls);
// Style
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const componentClassName = classNames(
prefixCls,
typography?.className,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
rootClassName,
hashId,
cssVarCls,
);
const mergedStyle: React.CSSProperties = { ...typography?.style, ...style };
return wrapCSSVar(
// @ts-expect-error: Expression produces a union type that is too complex to represent.
<Component className={componentClassName} style={mergedStyle} ref={mergedRef} {...restProps}>
{children}
</Component>,
);
});
if (process.env.NODE_ENV !== 'production') {
Typography.displayName = 'Typography';
}
// es default export should use const instead of let
export default Typography;