Skip to content

Commit e858ede

Browse files
chore: Enable eslint react/prop-types rule - part 2 (kyma-project#4534)
* Chnage components to tsx * Change another components * Corrected types * Corrected types * Fix error detected by the test * Types correction * Correction * Corrected types
1 parent c5e8fa5 commit e858ede

File tree

21 files changed

+433
-211
lines changed

21 files changed

+433
-211
lines changed

src/components/App/App.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ export default function App() {
149149
manualKubeConfigId.formOpen &&
150150
createPortal(
151151
<Dialog open={true}>
152-
{/*@ts-expect-error Type mismatch between js and ts*/}
153152
<ResourceForm.Single
154153
formElementRef={authFormRef}
155154
createResource={updateManualKubeConfigIdState}
@@ -164,7 +163,6 @@ export default function App() {
164163
{t('clusters.add.title')}
165164
</Button>
166165
</div>
167-
{/*@ts-expect-error Type mismatch between js and ts*/}
168166
</ResourceForm.Single>
169167
</Dialog>,
170168
document.body,

src/resources/RoleBindings/RoleForm.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const RoleForm = ({
3030
if (error) return error.message;
3131

3232
const roleTypeDropdown = (
33-
/*@ts-expect-error Type mismatch between js and ts*/
3433
<ResourceForm.FormField
3534
required
3635
label={t('role-bindings.create-modal.role-type')}
@@ -68,7 +67,6 @@ export const RoleForm = ({
6867
};
6968

7069
const roleNameInput = (
71-
/*@ts-expect-error Type mismatch between js and ts*/
7270
<ResourceForm.FormField
7371
required
7472
label={t('role-bindings.create-modal.role')}
@@ -102,7 +100,6 @@ export const RoleForm = ({
102100
);
103101

104102
return (
105-
/*@ts-expect-error Type mismatch between js and ts*/
106103
<ResourceFormWrapper resource={binding} setResource={setBinding}>
107104
{roleTypeDropdown}
108105
{roleNameInput}

src/resources/ServiceAccounts/TokenRequestModal/TokenRequestModal.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ export function TokenRequestModal({
121121
/>
122122
}
123123
>
124-
{/*@ts-expect-error Type mismatch between js and ts*/}
125124
<ResourceForm.Single
126125
resource={tokenRequest}
127126
setResource={setTokenRequest}
128127
>
129-
{/*@ts-expect-error Type mismatch between js and ts*/}
130128
<ResourceForm.FormField
131129
required
132130
propertyPath="$.spec.expirationSeconds"
@@ -181,7 +179,6 @@ export function TokenRequestModal({
181179
language="yaml"
182180
/>
183181
</div>
184-
{/*@ts-expect-error Type mismatch between js and ts*/}
185182
</ResourceForm.Single>
186183
</Dialog>
187184
);

src/shared/ResourceForm/components/CollapsibleSection.jsx renamed to src/shared/ResourceForm/components/CollapsibleSection.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react';
1+
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
22
import classnames from 'classnames';
33
import { ResourceFormWrapper } from './Wrapper';
44
import { Panel } from '@ui5/webcomponents-react';
@@ -7,6 +7,25 @@ import './CollapsibleSection.scss';
77
import { Toolbar } from '@ui5/webcomponents-react-compat/dist/components/Toolbar/index.js';
88
import { ToolbarSpacer } from '@ui5/webcomponents-react-compat/dist/components/ToolbarSpacer/index.js';
99

10+
export type CollapsibleSectionProps = {
11+
disabled?: boolean;
12+
defaultOpen?: boolean;
13+
canChangeState?: boolean;
14+
title: string | JSX.Element;
15+
defaultTitleType?: boolean;
16+
actions?:
17+
| React.ReactNode[]
18+
| JSX.Element
19+
| ((setOpen: Dispatch<SetStateAction<boolean | undefined>>) => JSX.Element);
20+
children: React.ReactNode;
21+
resource?: Record<string, any> | string;
22+
setResource?: (resource: Record<string, any> | string) => void;
23+
className?: string;
24+
required?: boolean;
25+
tooltipContent?: React.ReactNode;
26+
nestingLevel?: number;
27+
};
28+
1029
export function CollapsibleSection({
1130
disabled = undefined,
1231
defaultOpen,
@@ -21,9 +40,9 @@ export function CollapsibleSection({
2140
required = undefined,
2241
tooltipContent = undefined,
2342
nestingLevel = 0,
24-
}) {
43+
}: CollapsibleSectionProps) {
2544
const [open, setOpen] = useState(defaultOpen);
26-
const actionsRef = useRef();
45+
const actionsRef = useRef<HTMLDivElement>(null);
2746
required = required === true;
2847

2948
useEffect(() => {
@@ -38,11 +57,12 @@ export function CollapsibleSection({
3857
}
3958
}, [defaultOpen]);
4059

41-
const toggle = (e) => {
42-
e.stopPropagation();
43-
if (!canChangeState) return;
44-
if (disabled) return;
45-
if (!actionsRef.current?.contains(e.target)) setOpen(!open);
60+
const toggle = (e?: CustomEvent) => {
61+
e?.stopPropagation();
62+
if (!canChangeState || disabled) return;
63+
if (!actionsRef.current?.contains(e?.target as Node)) {
64+
setOpen(!open);
65+
}
4666
};
4767

4868
const classNames = classnames(
@@ -71,7 +91,7 @@ export function CollapsibleSection({
7191
onToggle={toggle}
7292
data-testid={titleText?.toLowerCase().replaceAll(' ', '-')}
7393
accessibleName={titleText}
74-
ref={(panelElement) => {
94+
ref={(panelElement: any) => {
7595
if (panelElement) {
7696
panelElement.useAccessibleNameForToggleButton = true;
7797
}

src/shared/ResourceForm/components/FormField.jsx renamed to src/shared/ResourceForm/components/FormField.tsx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@ import { Label } from './Label';
44
import { HintButton } from 'shared/components/HintButton/HintButton';
55

66
import { useCreateResourceDescription } from 'components/Extensibility/helpers';
7-
import { useState } from 'react';
7+
import { JSXElementConstructor, ReactElement, useState } from 'react';
88

99
import './FormField.scss';
1010

11+
export type FormFieldProps = {
12+
label?: string;
13+
input: (props: any) => JSX.Element;
14+
className?: string;
15+
required?: boolean;
16+
disabled?: boolean;
17+
tooltipContent?: React.ReactNode | string;
18+
isListItem?: boolean;
19+
messageStrip?: JSX.Element;
20+
inputInfo?: string | ReactElement<any, string | JSXElementConstructor<any>>;
21+
updatesOnInput?: boolean;
22+
style?: React.CSSProperties;
23+
} & Record<string, any>;
24+
1125
export function FormField({
1226
label,
1327
input,
@@ -21,8 +35,7 @@ export function FormField({
2135
updatesOnInput,
2236
style,
2337
...props
24-
}) {
25-
const { ...inputProps } = props;
38+
}: FormFieldProps) {
2639
const inputInfoLink = useCreateResourceDescription(inputInfo);
2740
const [openPopover, setOpenPopover] = useState(false);
2841

@@ -63,17 +76,16 @@ export function FormField({
6376
alignItems="Center"
6477
className="full-width"
6578
>
66-
{messageStrip
67-
? messageStrip
68-
: input({
69-
updatesOnInput,
70-
required,
71-
disabled,
72-
className: 'full-width',
73-
accessibleName: label,
74-
id: label.replace(' ', '-').toLowerCase(),
75-
...inputProps,
76-
})}
79+
{messageStrip ||
80+
input({
81+
updatesOnInput,
82+
required,
83+
disabled,
84+
className: 'full-width',
85+
accessibleName: label,
86+
id: label?.replace(' ', '-').toLowerCase(),
87+
...props,
88+
})}
7789
{inputInfo && (
7890
<Label wrappingType="Normal" style={{ marginTop: '5px' }}>
7991
{inputInfoLink}

src/shared/ResourceForm/components/Label.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Label as UI5Label } from '@ui5/webcomponents-react';
22
import WrappingType from '@ui5/webcomponents/dist/types/WrappingType';
33

4-
type LabelProps = {
4+
export type LabelProps = {
55
required?: boolean;
66
forElement?: string;
77
children: React.ReactNode;

src/shared/ResourceForm/components/ModeSelector.jsx renamed to src/shared/ResourceForm/components/ModeSelector.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@ import { SegmentedButton, SegmentedButtonItem } from '@ui5/webcomponents-react';
33
import { useAtom } from 'jotai';
44
import {
55
editViewModeAtom,
6+
EditViewTypes,
67
getEditViewModeState,
78
} from 'state/settings/editViewModeAtom';
89

9-
export function ModeSelector({ mode, setMode, isDisabled = false }) {
10+
type ModeSelectorProps = {
11+
mode: string;
12+
setMode: (mode: string) => void;
13+
isDisabled?: boolean;
14+
};
15+
16+
export function ModeSelector({
17+
mode,
18+
setMode,
19+
isDisabled = false,
20+
}: ModeSelectorProps) {
1021
const { t } = useTranslation();
1122
const [editViewMode, setEditViewMode] = useAtom(editViewModeAtom);
1223
const { preferencesViewType } = getEditViewModeState(editViewMode);
@@ -27,8 +38,10 @@ export function ModeSelector({ mode, setMode, isDisabled = false }) {
2738
<SegmentedButton
2839
className="mode-selector__content"
2940
onSelectionChange={(event) => {
30-
const mode = event.detail.selectedItems[0].getAttribute('data-mode');
31-
setMode(mode);
41+
const mode = event.detail.selectedItems[0].getAttribute(
42+
'data-mode',
43+
) as EditViewTypes['dynamicViewType'];
44+
setMode(mode as string);
3245
if (preferencesViewType === 'MODE_DEFAULT') {
3346
setEditViewMode({
3447
preferencesViewType: preferencesViewType,

src/shared/ResourceForm/components/Presets.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { useTranslation } from 'react-i18next';
33
import { Dropdown } from 'shared/components/Dropdown/Dropdown';
44
import './Presets.scss';
55

6-
type PresetProps = {
7-
presets: { name: string; data: Record<string, any> }[];
6+
export type PresetProps = {
7+
presets: {
8+
name: string;
9+
data?: Record<string, any>;
10+
value?: Record<string, any>;
11+
}[];
812
onSelect: (preset?: any) => void;
913
inlinePresets?: boolean;
1014
disabled?: boolean;

src/shared/ResourceForm/components/ResourceForm.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import EditorWrapper from 'shared/ResourceForm/fields/Editor';
1414
import { useTranslation } from 'react-i18next';
1515

1616
import { ModeSelector } from './ModeSelector';
17-
import { ResourceFormWrapper } from './Wrapper';
18-
import { Presets } from './Presets';
17+
import { ResourceFormWrapper, ResourceFormWrapperProps } from './Wrapper';
18+
import { PresetProps, Presets } from './Presets';
19+
import { SingleFormProps } from './Single';
20+
import { CollapsibleSectionProps } from './CollapsibleSection';
21+
import { LabelProps } from './Label';
22+
import { FormFieldProps } from './FormField';
23+
import { TitleProps } from './Title';
1924
import { useCreateResource } from '../useCreateResource';
2025
import { K8sNameField, KeyValueField } from '../fields';
2126
import jp from 'jsonpath';
@@ -83,7 +88,17 @@ type ResourceFormProps = {
8388
formWithoutPanel?: boolean;
8489
};
8590

86-
export function ResourceForm({
91+
type ResourceFormType = FunctionComponent<ResourceFormProps> & {
92+
Single: FunctionComponent<SingleFormProps>;
93+
Wrapper: FunctionComponent<ResourceFormWrapperProps>;
94+
CollapsibleSection: FunctionComponent<CollapsibleSectionProps>;
95+
Label: FunctionComponent<LabelProps>;
96+
FormField: FunctionComponent<FormFieldProps>;
97+
Title: FunctionComponent<TitleProps>;
98+
Presets: FunctionComponent<PresetProps>;
99+
};
100+
101+
export const ResourceForm: ResourceFormType = (({
87102
pluralKind, // used for the request path
88103
singularName,
89104
resource,
@@ -122,7 +137,7 @@ export function ResourceForm({
122137
title,
123138
resetLayout,
124139
formWithoutPanel,
125-
}: ResourceFormProps) {
140+
}) => {
126141
const layoutState = useAtomValue(columnLayoutAtom);
127142

128143
const isEdit = useMemo(
@@ -411,4 +426,4 @@ export function ResourceForm({
411426
)}
412427
</section>
413428
);
414-
}
429+
}) as ResourceFormType;

src/shared/ResourceForm/components/Single.jsx renamed to src/shared/ResourceForm/components/Single.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import { createPortal } from 'react-dom';
66
import { UnsavedMessageBox } from 'shared/components/UnsavedMessageBox/UnsavedMessageBox';
77
import { useFormEditTracking } from 'shared/hooks/useFormEditTracking';
88

9+
export type SingleFormProps = {
10+
formElementRef?: React.RefObject<HTMLFormElement>;
11+
createResource?: (e: React.FormEvent<HTMLFormElement>) => void;
12+
children: React.ReactNode;
13+
resource?: Record<string, any>;
14+
setResource?: (resource: any) => void;
15+
className?: string;
16+
setCustomValid?: (isValid: boolean) => void;
17+
initialResource?: Record<string, any>;
18+
} & Record<string, any>;
19+
920
export function SingleForm({
1021
formElementRef,
1122
createResource,
@@ -16,7 +27,7 @@ export function SingleForm({
1627
setCustomValid,
1728
initialResource,
1829
...props
19-
}) {
30+
}: SingleFormProps) {
2031
const validationRef = useRef(true);
2132

2233
useEffect(() => {

0 commit comments

Comments
 (0)