2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { BlockProps } from './Base';
|
|
|
|
import Base from './Base';
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-12-13 14:57:40 +08:00
|
|
|
const TITLE_ELE_LIST = [1, 2, 3, 4, 5] as const;
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
export interface TitleProps
|
|
|
|
extends Omit<BlockProps<'h1' | 'h2' | 'h3' | 'h4' | 'h5'>, 'strong'>,
|
|
|
|
Omit<
|
|
|
|
React.HTMLAttributes<HTMLHeadElement>,
|
|
|
|
'type' | keyof BlockProps<'h1' | 'h2' | 'h3' | 'h4' | 'h5'>
|
|
|
|
> {
|
2024-04-08 14:04:08 +08:00
|
|
|
level?: (typeof TITLE_ELE_LIST)[number];
|
2022-10-18 11:45:43 +08:00
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
const Title = React.forwardRef<HTMLElement, TitleProps>((props, ref) => {
|
2019-02-19 11:42:05 +08:00
|
|
|
const { level = 1, ...restProps } = props;
|
2022-10-18 11:45:43 +08:00
|
|
|
let component: keyof JSX.IntrinsicElements;
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Typography.Title');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2023-09-11 17:28:04 +08:00
|
|
|
TITLE_ELE_LIST.includes(level),
|
|
|
|
'usage',
|
2020-07-28 20:39:43 +08:00
|
|
|
'Title only accept `1 | 2 | 3 | 4 | 5` as `level` value. And `5` need 4.6.0+ version.',
|
|
|
|
);
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TITLE_ELE_LIST.includes(level)) {
|
|
|
|
component = `h${level}`;
|
|
|
|
} else {
|
2019-02-19 11:42:05 +08:00
|
|
|
component = 'h1';
|
|
|
|
}
|
|
|
|
|
2022-04-05 13:02:32 +08:00
|
|
|
return <Base ref={ref} {...restProps} component={component} />;
|
2022-10-18 11:45:43 +08:00
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
export default Title;
|