File tree Expand file tree Collapse file tree 1 file changed +15
-9
lines changed Expand file tree Collapse file tree 1 file changed +15
-9
lines changed Original file line number Diff line number Diff line change 1
1
import React , { useContext } from 'react' ;
2
2
import { Context } from './store' ;
3
3
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
+ }
5
10
6
11
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 ;
9
14
const cls = [ `${ CODE_PREVIEW_PREFIX } -code` , className ] . filter ( Boolean ) . join ( ' ' ) . trim ( ) ;
10
15
const store = useContext ( Context ) ;
11
16
if ( store . collapse ) {
12
17
return null ;
13
18
}
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 ) ;
20
26
} ,
21
27
) ;
22
28
You can’t perform that action at this time.
0 commit comments