|
1 | 1 | import propTypes from "prop-types";
|
2 |
| -import { WeakValidationMap, useEffect, useMemo } from "react"; |
| 2 | +import { |
| 3 | + forwardRef, |
| 4 | + useEffect, |
| 5 | + useMemo, |
| 6 | + type ForwardedRef, |
| 7 | + type WeakValidationMap, |
| 8 | +} from "react"; |
3 | 9 | import type {
|
4 | 10 | PyConfigFetch,
|
5 | 11 | PyConfigFetchItem,
|
@@ -37,79 +43,91 @@ const checkForAnyKey = (
|
37 | 43 | * @see {@link https://docs.pyscript.net/latest/reference/elements/py-config.html} Original py-config element documentation.
|
38 | 44 | * @see {@link https://pyscript-react.github.io/} Pyscript-react element documentation.
|
39 | 45 | */
|
40 |
| -const PyConfig: PyConfigTag = <T extends object>({ |
41 |
| - children, |
42 |
| - source, |
43 |
| - type, |
44 |
| - splashscreen, |
45 |
| - interpreters, |
46 |
| - fetch, |
47 |
| - packages, |
48 |
| - plugins, |
49 |
| - ...rest |
50 |
| -}: PyConfigProperties<T>): JSX.Element => { |
51 |
| - // eslint-disable-next-line sonarjs/cognitive-complexity |
52 |
| - const config: string = useMemo((): string => { |
53 |
| - if (type === "json") { |
54 |
| - const transformedPlugins: string[] = [ |
55 |
| - ...(plugins || []), |
56 |
| - ...(children?.plugins || []), |
57 |
| - ]; |
58 |
| - const transformedPackages: string[] = [ |
59 |
| - ...(packages || []), |
60 |
| - ...(children?.packages || []), |
61 |
| - ]; |
62 |
| - const transformedFetch: PyConfigFetch = [ |
63 |
| - ...(fetch || []), |
64 |
| - ...(children?.fetch || []), |
65 |
| - ].map(({ files, ...restItem }: PyConfigFetchItem): PyConfigFetchItem => { |
66 |
| - const transformedFiles: string[] = [...(files || [])]; |
67 |
| - return { |
68 |
| - files: transformedFiles.length ? transformedFiles : undefined, |
69 |
| - ...restItem, |
| 46 | +const PyConfig: PyConfigTag = forwardRef( |
| 47 | + <OptionalProperties extends object>( |
| 48 | + { |
| 49 | + children, |
| 50 | + source, |
| 51 | + type, |
| 52 | + splashscreen, |
| 53 | + interpreters, |
| 54 | + fetch, |
| 55 | + packages, |
| 56 | + plugins, |
| 57 | + ...rest |
| 58 | + }: PyConfigProperties<OptionalProperties>, |
| 59 | + reference: ForwardedRef<HTMLElement> | undefined, |
| 60 | + // eslint-disable-next-line max-params, sonarjs/cognitive-complexity |
| 61 | + ): JSX.Element => { |
| 62 | + const config: string = useMemo((): string => { |
| 63 | + if (type === "json") { |
| 64 | + const transformedPlugins: string[] = [ |
| 65 | + ...(plugins ?? []), |
| 66 | + ...(children?.plugins ?? []), |
| 67 | + ]; |
| 68 | + const transformedPackages: string[] = [ |
| 69 | + ...(packages ?? []), |
| 70 | + ...(children?.packages ?? []), |
| 71 | + ]; |
| 72 | + const transformedFetch: PyConfigFetch = [ |
| 73 | + ...(fetch ?? []), |
| 74 | + ...(children?.fetch ?? []), |
| 75 | + ].map( |
| 76 | + ({ files, ...restItem }: PyConfigFetchItem): PyConfigFetchItem => { |
| 77 | + const transformedFiles: string[] = [...(files ?? [])]; |
| 78 | + return { |
| 79 | + files: transformedFiles.length ? transformedFiles : undefined, |
| 80 | + ...restItem, |
| 81 | + }; |
| 82 | + }, |
| 83 | + ); |
| 84 | + const transformedInterpreters: Omit<PyConfigInterpreters, "source"> & { |
| 85 | + src?: string; |
| 86 | + } = { |
| 87 | + src: interpreters?.source, |
| 88 | + name: interpreters?.name, |
| 89 | + language: interpreters?.language, |
| 90 | + ...children?.interpreters, |
70 | 91 | };
|
71 |
| - }); |
72 |
| - const transformedInterpreters: Omit<PyConfigInterpreters, "source"> & { |
73 |
| - src?: string; |
74 |
| - } = { |
75 |
| - src: interpreters?.source, |
76 |
| - name: interpreters?.name, |
77 |
| - language: interpreters?.language, |
78 |
| - ...children?.interpreters, |
79 |
| - }; |
80 |
| - const transformedSplashscreen: PyConfigSplashscreen = { |
81 |
| - autoclose: splashscreen?.autoclose, |
82 |
| - ...children?.splashscreen, |
83 |
| - }; |
84 |
| - const config: string = JSON.stringify({ |
85 |
| - splashscreen: checkForAnyKey(transformedSplashscreen) |
86 |
| - ? transformedSplashscreen |
87 |
| - : undefined, |
88 |
| - interpreters: checkForAnyKey(transformedInterpreters) |
89 |
| - ? transformedInterpreters |
90 |
| - : undefined, |
91 |
| - fetch: transformedFetch.length ? transformedFetch : undefined, |
92 |
| - packages: transformedPackages.length ? transformedPackages : undefined, |
93 |
| - plugins: transformedPlugins.length ? transformedPlugins : undefined, |
94 |
| - ...children, |
95 |
| - }); |
96 |
| - return config; |
97 |
| - } |
98 |
| - return `${children || ""}`; |
99 |
| - }, [children, splashscreen, interpreters, fetch, packages, plugins]); |
100 |
| - useEffect((): void => { |
101 |
| - source && |
102 |
| - children && |
103 |
| - console.warn( |
104 |
| - "Children is passed with source. It may create undefined behavior. Remove one of these properties.", |
105 |
| - ); |
106 |
| - }, [source, children]); |
107 |
| - return ( |
108 |
| - <py-config {...rest} type={type} src={source}> |
109 |
| - {!source ? config : undefined} |
110 |
| - </py-config> |
111 |
| - ); |
112 |
| -}; |
| 92 | + const transformedSplashscreen: PyConfigSplashscreen = { |
| 93 | + autoclose: splashscreen?.autoclose, |
| 94 | + ...children?.splashscreen, |
| 95 | + }; |
| 96 | + const config: string = JSON.stringify({ |
| 97 | + splashscreen: checkForAnyKey(transformedSplashscreen) |
| 98 | + ? transformedSplashscreen |
| 99 | + : undefined, |
| 100 | + interpreters: checkForAnyKey(transformedInterpreters) |
| 101 | + ? transformedInterpreters |
| 102 | + : undefined, |
| 103 | + fetch: transformedFetch.length ? transformedFetch : undefined, |
| 104 | + packages: transformedPackages.length |
| 105 | + ? transformedPackages |
| 106 | + : undefined, |
| 107 | + plugins: transformedPlugins.length ? transformedPlugins : undefined, |
| 108 | + ...children, |
| 109 | + }); |
| 110 | + return config; |
| 111 | + } |
| 112 | + return `${children ?? ""}`; |
| 113 | + }, [children, splashscreen, interpreters, fetch, packages, plugins]); |
| 114 | + useEffect((): void => { |
| 115 | + source && |
| 116 | + children && |
| 117 | + // eslint-disable-next-line no-console |
| 118 | + console.warn( |
| 119 | + "Children is passed with source. It may create undefined behavior. Remove one of these properties.", |
| 120 | + ); |
| 121 | + }, [source, children]); |
| 122 | + return ( |
| 123 | + <py-config ref={reference} {...rest} type={type} src={source}> |
| 124 | + {!source ? config : undefined} |
| 125 | + </py-config> |
| 126 | + ); |
| 127 | + }, |
| 128 | +) as PyConfigTag; |
| 129 | + |
| 130 | +PyConfig.displayName = "PyConfig"; |
113 | 131 |
|
114 | 132 | PyConfig.propTypes = {
|
115 | 133 | children: propTypes.oneOfType([
|
|
0 commit comments