Skip to content

Commit 3dfe67b

Browse files
committed
fix: fix CodeProps props.
1 parent 613009f commit 3dfe67b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

core/src/Code.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React, { useContext } from 'react';
22
import { Context } from './store';
33
import { CODE_PREVIEW_PREFIX } from './CodePreview';
4-
import type { ElementType, ComponentPropsWithoutRef } from 'react';
4+
5+
export type TagType = React.ComponentType | keyof JSX.IntrinsicElements;
6+
7+
export interface CodeProps<Tag extends TagType> extends React.HTMLProps<Tag> {
8+
tagName?: Tag;
9+
}
510

611
export const Code = React.forwardRef(
7-
<T extends ElementType<any> = 'div'>(props: { as?: T } & ComponentPropsWithoutRef<T>, ref?: React.LegacyRef<T>) => {
8-
const { as, className, children, ...htmlProps } = props;
12+
<Tag extends TagType = 'div'>(props: CodeProps<Tag>, ref: React.Ref<React.HTMLProps<Tag>>) => {
13+
const { tagName = 'div', className, children, ...htmlProps } = props;
914
const cls = [`${CODE_PREVIEW_PREFIX}-code`, className].filter(Boolean).join(' ').trim();
1015
const store = useContext(Context);
1116
if (store.collapse) {
1217
return null;
1318
}
14-
const Comp = as || 'div';
15-
return (
16-
<Comp {...htmlProps} className={cls} ref={(node) => {}}>
17-
{children}
18-
</Comp>
19-
);
19+
const TagName = props.href && typeof tagName === 'string' ? 'a' : tagName;
20+
const childProps = {
21+
...htmlProps,
22+
className: cls,
23+
ref,
24+
};
25+
return React.createElement(TagName, childProps, children);
2026
},
2127
);
2228

0 commit comments

Comments
 (0)