Skip to content

Commit 65ff0d3

Browse files
fixed types and added forwardRef to py-env
1 parent 54216b0 commit 65ff0d3

File tree

8 files changed

+71
-54
lines changed

8 files changed

+71
-54
lines changed

source/library/components/py-box/py-box.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type { PyBoxProperties, PyBoxTag } from "./py-box.types";
99
* @deprecated
1010
*/
1111
const PyBox: PyBoxTag = forwardRef(
12-
<T extends object>(
13-
{ children, ...rest }: PyBoxProperties<T>,
12+
<OptionalProperties extends object>(
13+
{ children, ...rest }: PyBoxProperties<OptionalProperties>,
1414
reference: ForwardedRef<HTMLElement> | undefined,
1515
// eslint-disable-next-line max-params
1616
): JSX.Element => {

source/library/components/py-box/py-box.types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type {
1+
import {
22
DetailedHTMLProps,
3+
ForwardedRef,
34
HTMLAttributes,
4-
LegacyRef,
55
PropsWithChildren,
66
WeakValidationMap,
77
} from "react";
@@ -23,7 +23,7 @@ export type PyBoxProperties<OptionalProperties> =
2323
export type PyBoxTag = {
2424
<OptionalProperties extends object>(
2525
properties: PyBoxProperties<OptionalProperties>,
26-
reference?: LegacyRef<HTMLElement>,
26+
reference?: ForwardedRef<HTMLElement>,
2727
): JSX.Element;
2828
displayName?: string;
2929
defaultProps?: Partial<PyBoxPropertiesBase>;

source/library/components/py-button/py-button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type { PyButtonProperties, PyButtonTag } from "./py-button.types";
99
* @deprecated
1010
*/
1111
const PyButton: PyButtonTag = forwardRef(
12-
<T extends object>(
13-
{ children, label, ...rest }: PyButtonProperties<T>,
12+
<OptionalProperties extends object>(
13+
{ children, label, ...rest }: PyButtonProperties<OptionalProperties>,
1414
reference: ForwardedRef<HTMLElement> | undefined,
1515
// eslint-disable-next-line max-params
1616
): JSX.Element => {

source/library/components/py-button/py-button.types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type {
1+
import {
22
DetailedHTMLProps,
3+
ForwardedRef,
34
HTMLAttributes,
4-
LegacyRef,
55
WeakValidationMap,
66
} from "react";
77
import type ReactElementProps from "~types/react-element-properties/react-element-properties";
@@ -24,7 +24,7 @@ export type PyButtonProperties<OptionalProperties> =
2424
export type PyButtonTag = {
2525
<OptionalProperties extends object>(
2626
properties: PyButtonProperties<OptionalProperties>,
27-
reference?: LegacyRef<HTMLElement>,
27+
reference?: ForwardedRef<HTMLElement>,
2828
): JSX.Element;
2929
displayName?: string;
3030
defaultProps?: Partial<PyButtonPropertiesBase>;

source/library/components/py-config/py-config.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const checkForAnyKey = (
4444
* @see {@link https://pyscript-react.github.io/} Pyscript-react element documentation.
4545
*/
4646
const PyConfig: PyConfigTag = forwardRef(
47-
<T extends object>(
47+
<OptionalProperties extends object>(
4848
{
4949
children,
5050
source,
@@ -55,7 +55,7 @@ const PyConfig: PyConfigTag = forwardRef(
5555
packages,
5656
plugins,
5757
...rest
58-
}: PyConfigProperties<T>,
58+
}: PyConfigProperties<OptionalProperties>,
5959
reference: ForwardedRef<HTMLElement> | undefined,
6060
// eslint-disable-next-line max-params, sonarjs/cognitive-complexity
6161
): JSX.Element => {

source/library/components/py-config/py-config.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
DetailedHTMLProps,
3+
ForwardedRef,
34
HTMLAttributes,
4-
LegacyRef,
55
WeakValidationMap,
66
} from "react";
77
import type ReactElementProps from "~types/react-element-properties/react-element-properties";
@@ -82,7 +82,7 @@ export type PyConfigProperties<OptionalProperties> =
8282
export type PyConfigTag = {
8383
<OptionalProperties extends object>(
8484
properties: PyConfigProperties<OptionalProperties>,
85-
reference?: LegacyRef<HTMLElement>,
85+
reference?: ForwardedRef<HTMLElement>,
8686
): JSX.Element;
8787
displayName?: string;
8888
defaultProps?: Partial<PyConfigPropertiesBase>;

source/library/components/py-env/py-env.tsx

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import propTypes from "prop-types";
2-
import { useMemo, type WeakValidationMap } from "react";
2+
import { ForwardedRef, WeakValidationMap, forwardRef, useMemo } from "react";
3+
34
import type {
45
PyEnvChildrenAsObject,
56
PyEnvProperties,
@@ -12,41 +13,50 @@ import type {
1213
* @param root0.children
1314
* @deprecated
1415
*/
15-
const PyEnv: PyEnvTag = <T extends object>({
16-
children,
17-
...rest
18-
}: PyEnvProperties<T>): JSX.Element => {
19-
const fixedChildren: string = useMemo((): string => {
20-
const { paths, items }: PyEnvChildrenAsObject = Object(children);
21-
if (paths || items) {
22-
const fixedItems: string =
23-
[...(items || [])]
24-
?.map((element: string): string => {
25-
return `- ${element}`;
26-
})
27-
.join("\n") || "";
28-
const fixedPaths: string =
29-
[...(paths || [])]
30-
?.map((element: string): string => {
31-
return `\t- ${element}`;
32-
})
33-
.join("\n") || "";
34-
const fixedPathsWithCondition: string = fixedPaths
35-
? `\n- paths:\n${fixedPaths}`
36-
: "";
37-
return `${fixedItems}${fixedPathsWithCondition}`;
38-
} else {
39-
return Array.isArray(children)
40-
? children
41-
.map((element: string): string => {
16+
const PyEnv: PyEnvTag = forwardRef(
17+
<OptionalProperties extends object>(
18+
{ children, ...rest }: PyEnvProperties<OptionalProperties>,
19+
reference: ForwardedRef<HTMLElement> | undefined,
20+
// eslint-disable-next-line max-params
21+
): JSX.Element => {
22+
const fixedChildren: string = useMemo((): string => {
23+
const { paths, items }: PyEnvChildrenAsObject = Object(children);
24+
if (paths || items) {
25+
const fixedItems: string =
26+
[...(items || [])]
27+
?.map((element: string): string => {
4228
return `- ${element}`;
4329
})
44-
.join("\n")
45-
: (children as string);
46-
}
47-
}, [children]);
48-
return <py-env {...rest}>{fixedChildren}</py-env>;
49-
};
30+
.join("\n") || "";
31+
const fixedPaths: string =
32+
[...(paths || [])]
33+
?.map((element: string): string => {
34+
return `\t- ${element}`;
35+
})
36+
.join("\n") || "";
37+
const fixedPathsWithCondition: string = fixedPaths
38+
? `\n- paths:\n${fixedPaths}`
39+
: "";
40+
return `${fixedItems}${fixedPathsWithCondition}`;
41+
} else {
42+
return Array.isArray(children)
43+
? children
44+
.map((element: string): string => {
45+
return `- ${element}`;
46+
})
47+
.join("\n")
48+
: (children as string);
49+
}
50+
}, [children]);
51+
return (
52+
<py-env ref={reference} {...rest}>
53+
{fixedChildren}
54+
</py-env>
55+
);
56+
},
57+
) as PyEnvTag;
58+
59+
PyEnv.displayName = "PyEnv";
5060

5161
PyEnv.propTypes = {
5262
children: propTypes.oneOfType([

source/library/components/py-env/py-env.types.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type {
1+
import {
22
DetailedHTMLProps,
3+
ForwardedRef,
34
HTMLAttributes,
45
WeakValidationMap,
56
} from "react";
@@ -28,11 +29,17 @@ export type PyEnvPropertiesBase = Omit<
2829
children: string | string[] | Set<string> | PyEnvChildrenAsObject;
2930
};
3031

31-
export type PyEnvProperties<T> = T extends infer T
32-
? T & PyEnvPropertiesBase
33-
: PyEnvPropertiesBase;
32+
export type PyEnvProperties<OptionalProperties> =
33+
OptionalProperties extends infer OptionalProperties
34+
? OptionalProperties & PyEnvPropertiesBase
35+
: PyEnvPropertiesBase;
3436

3537
export type PyEnvTag = {
36-
<T extends object>(properties: PyEnvProperties<T>): JSX.Element;
37-
propTypes: WeakValidationMap<PyEnvPropertiesBase>;
38+
<OptionalProperties extends object>(
39+
properties: PyEnvProperties<OptionalProperties>,
40+
reference?: ForwardedRef<HTMLElement>,
41+
): JSX.Element;
42+
displayName?: string;
43+
defaultProps?: Partial<PyEnvPropertiesBase>;
44+
propTypes?: WeakValidationMap<PyEnvPropertiesBase>;
3845
};

0 commit comments

Comments
 (0)