1
1
import propTypes from "prop-types" ;
2
2
import {
3
- DetailedHTMLProps ,
4
- HTMLAttributes ,
5
- WeakValidationMap ,
6
3
createElement ,
4
+ forwardRef ,
7
5
useMemo ,
6
+ type DetailedHTMLProps ,
7
+ type ForwardedRef ,
8
+ type HTMLAttributes ,
9
+ type WeakValidationMap ,
8
10
} from "react" ;
9
- import {
11
+ import type {
10
12
PyConfigPropertiesWithJsonType ,
11
13
PyConfigPropertiesWithTomlType ,
12
14
PyConfigPropertiesWithoutChildren ,
13
15
} from "~components/py-config/py-config.types" ;
14
- import ReactElementProps from "~root/source/library/types/react-element-properties/react-element-properties" ;
16
+ import type ReactElementProps from "~root/source/library/types/react-element-properties/react-element-properties" ;
15
17
import PyConfig from "../py-config/py-config" ;
16
18
import PyEnv from "../py-env/py-env" ;
17
19
import type {
@@ -36,47 +38,36 @@ import type {
36
38
* @see {@link https://docs.pyscript.net/latest/reference/elements/py-script.html } Original py-script element documentation.
37
39
* @see {@link https://pyscript-react.github.io/ } Pyscript-react element documentation.
38
40
*/
39
- const PyScript : PyScriptTag = < T extends object > ( {
40
- children,
41
- output,
42
- generateOutputTag,
43
- pyEnvContent,
44
- pyEnvProps,
45
- pyConfigContent,
46
- pyConfigProps,
47
- src,
48
- source,
49
- ...rest
50
- } : PyScriptProperties < T > ) : JSX . Element => {
51
- const pyConfigElement : "" | JSX . Element | undefined = useMemo ( ( ) :
52
- | ""
53
- | JSX . Element
54
- | undefined => {
55
- const isPyConfigSource : boolean = Boolean (
56
- ( pyConfigProps as Partial < PyConfigPropertiesWithoutChildren > ) ?. source ,
57
- ) ;
58
- const isPyConfigContentJsonType : boolean = pyConfigProps ?. type === "json" ;
59
- const pyConfigElementWithContent : "" | JSX . Element | undefined =
60
- isPyConfigContentJsonType ? (
61
- < PyConfig
62
- { ...( pyConfigProps as Omit <
63
- PyConfigPropertiesWithJsonType ,
64
- "children"
65
- > &
66
- Omit <
67
- ReactElementProps <
68
- DetailedHTMLProps < HTMLAttributes < HTMLElement > , HTMLElement >
69
- > ,
70
- "children"
71
- > ) }
72
- >
73
- { pyConfigContent as PyConfigPropertiesWithJsonType [ "children" ] }
74
- </ PyConfig >
75
- ) : (
76
- pyConfigContent && (
41
+ const PyScript : PyScriptTag = forwardRef (
42
+ < OptionalProperties extends object > (
43
+ {
44
+ children,
45
+ output,
46
+ generateOutputTag,
47
+ pyEnvContent,
48
+ pyEnvProps,
49
+ pyConfigContent,
50
+ pyConfigProps,
51
+ src,
52
+ source,
53
+ ...rest
54
+ } : PyScriptProperties < OptionalProperties > ,
55
+ reference : ForwardedRef < HTMLElement > | undefined ,
56
+ // eslint-disable-next-line max-params
57
+ ) : JSX . Element => {
58
+ const pyConfigElement : "" | JSX . Element | undefined = useMemo ( ( ) :
59
+ | ""
60
+ | JSX . Element
61
+ | undefined => {
62
+ const isPyConfigSource : boolean = Boolean (
63
+ ( pyConfigProps as Partial < PyConfigPropertiesWithoutChildren > ) ?. source ,
64
+ ) ;
65
+ const isPyConfigContentJsonType : boolean = pyConfigProps ?. type === "json" ;
66
+ const pyConfigElementWithContent : "" | JSX . Element | undefined =
67
+ isPyConfigContentJsonType ? (
77
68
< PyConfig
78
69
{ ...( pyConfigProps as Omit <
79
- PyConfigPropertiesWithTomlType ,
70
+ PyConfigPropertiesWithJsonType ,
80
71
"children"
81
72
> &
82
73
Omit <
@@ -86,34 +77,58 @@ const PyScript: PyScriptTag = <T extends object>({
86
77
"children"
87
78
> ) }
88
79
>
89
- { pyConfigContent as PyConfigPropertiesWithTomlType [ "children" ] }
80
+ { pyConfigContent as PyConfigPropertiesWithJsonType [ "children" ] }
90
81
</ PyConfig >
91
- )
82
+ ) : (
83
+ pyConfigContent && (
84
+ < PyConfig
85
+ { ...( pyConfigProps as Omit <
86
+ PyConfigPropertiesWithTomlType ,
87
+ "children"
88
+ > &
89
+ Omit <
90
+ ReactElementProps <
91
+ DetailedHTMLProps < HTMLAttributes < HTMLElement > , HTMLElement >
92
+ > ,
93
+ "children"
94
+ > ) }
95
+ >
96
+ { pyConfigContent as PyConfigPropertiesWithTomlType [ "children" ] }
97
+ </ PyConfig >
98
+ )
99
+ ) ;
100
+ return isPyConfigSource ? (
101
+ < PyConfig { ...( pyConfigProps as PyConfigPropertiesWithoutChildren ) } />
102
+ ) : (
103
+ pyConfigElementWithContent
92
104
) ;
93
- return isPyConfigSource ? (
94
- < PyConfig { ...( pyConfigProps as PyConfigPropertiesWithoutChildren ) } />
95
- ) : (
96
- pyConfigElementWithContent
105
+ } , [ pyConfigProps , pyConfigContent ] ) ;
106
+ return (
107
+ < >
108
+ { pyConfigElement }
109
+ { pyEnvContent && < PyEnv { ...pyEnvProps } > { pyEnvContent } </ PyEnv > }
110
+ { output &&
111
+ generateOutputTag &&
112
+ createElement (
113
+ typeof generateOutputTag === "string" ? generateOutputTag : "div" ,
114
+ {
115
+ id : output ,
116
+ } ,
117
+ ) }
118
+ < py-script
119
+ ref = { reference }
120
+ { ...rest }
121
+ src = { source ?? src }
122
+ output = { output }
123
+ >
124
+ { children ?? "" }
125
+ </ py-script >
126
+ </ >
97
127
) ;
98
- } , [ pyConfigProps , pyConfigContent ] ) ;
99
- return (
100
- < >
101
- { pyConfigElement }
102
- { pyEnvContent && < PyEnv { ...pyEnvProps } > { pyEnvContent } </ PyEnv > }
103
- { output &&
104
- generateOutputTag &&
105
- createElement (
106
- typeof generateOutputTag === "string" ? generateOutputTag : "div" ,
107
- {
108
- id : output ,
109
- } ,
110
- ) }
111
- < py-script { ...rest } src = { source || src } output = { output } >
112
- { children || "" }
113
- </ py-script >
114
- </ >
115
- ) ;
116
- } ;
128
+ } ,
129
+ ) as PyScriptTag ;
130
+
131
+ PyScript . displayName = "PyScript" ;
117
132
118
133
PyScript . propTypes = {
119
134
children : propTypes . string ,
0 commit comments