Skip to content

Commit

Permalink
Update to PF5 - part II - onChange event handler
Browse files Browse the repository at this point in the history
Reference: #1098

For avoiding uncaught PF 4 -> PF 5 migration errors, this PR named and typed (i.e. (add type to function signature) all onChange callback appearances which use parameters.

Signed-off-by: Sharon Gratch <[email protected]>
  • Loading branch information
sgratch committed Jul 15, 2024
1 parent d854c84 commit 1d0c35d
Show file tree
Hide file tree
Showing 44 changed files with 441 additions and 120 deletions.
5 changes: 4 additions & 1 deletion packages/common/src/components/Filter/DateFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export const DateFilter = ({
onFilterUpdate([...validFilters.filter((d) => d !== option)]);
};

const onDateChange = (even: FormEvent<HTMLInputElement>, value: string) => {
const onDateChange: (event: FormEvent<HTMLInputElement>, value: string, date?: Date) => void = (
event,
value,
) => {
// require full format "YYYY-MM-DD" although partial date is also accepted
// i.e. YYYY-MM gets parsed as YYYY-MM-01 and results in auto completing the date
// unfortunately due to auto-complete user cannot delete the date char after char
Expand Down
15 changes: 12 additions & 3 deletions packages/common/src/components/Filter/DateRangeFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FormEvent, useState } from 'react';
import React, { useState } from 'react';
import { DateTime } from 'luxon';

import {
Expand Down Expand Up @@ -63,15 +63,23 @@ export const DateRangeFilter = ({
onFilterUpdate([...validFilters.filter((range) => range !== target)]);
};

const onFromDateChange = (even: FormEvent<HTMLInputElement>, value: string) => {
const onFromDateChange: (
event: React.FormEvent<HTMLInputElement>,
value: string,
date?: Date,
) => void = (event, value) => {
//see DateFilter onDateChange
if (value?.length === 10 && isValidDate(value)) {
setFrom(parseISOtoJSDate(value));
setTo(undefined);
}
};

const onToDateChange = (even: FormEvent<HTMLInputElement>, value: string) => {
const onToDateChange: (
event: React.FormEvent<HTMLInputElement>,
value: string,
date?: Date,
) => void = (event, value) => {
//see DateFilter onDateChange
if (value?.length === 10 && isValidDate(value)) {
const newTo = parseISOtoJSDate(value);
Expand All @@ -82,6 +90,7 @@ export const DateRangeFilter = ({
}
}
};

return (
<ToolbarFilter
key={filterId}
Expand Down
12 changes: 9 additions & 3 deletions packages/common/src/components/Filter/FreetextFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export const FreetextFilter = ({
onFilterUpdate([...selectedFilters, inputValue]);
setInputValue('');
};

const onChange: (event: React.FormEvent<HTMLInputElement>, value: string) => void = (
event,
value,
) => {
setInputValue(value);
};

return (
<ToolbarFilter
key={filterId}
Expand All @@ -46,9 +54,7 @@ export const FreetextFilter = ({
<SearchInput
placeholder={placeholderLabel}
value={inputValue}
onChange={(event, value) => {
setInputValue(value);
}}
onChange={onChange}
onSearch={onTextInput}
onClear={() => setInputValue('')}
/>
Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/components/Filter/SwitchFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ export const SwitchFilter = ({
onFilterUpdate,
placeholderLabel,
}: FilterTypeProps) => {
const onChange: (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void = (
checked,
) => {
onFilterUpdate(checked ? [Boolean(checked).toString()] : []);
};

return (
<ToolbarItem>
<Switch
label={placeholderLabel}
isChecked={selectedFilters.length === 1 && selectedFilters[0] === 'true'}
onChange={(checked) => onFilterUpdate(checked ? [Boolean(checked).toString()] : [])}
onChange={onChange}
/>
</ToolbarItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export const ManageColumnsModal = ({
onClose();
};

type onChangeFactoryType = (
id: string,
) => (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void;

const onChangeFactory: onChangeFactoryType = (id) => (checked) => onSelect(id, checked);

return (
<Modal
title={title}
Expand Down Expand Up @@ -177,7 +183,7 @@ export const ManageColumnsModal = ({
: isVisible
}
isDisabled={isIdentity}
onChange={(value) => onSelect(id, value)}
onChange={onChangeFactory(id)}
otherControls
/>
</DataListControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ export const FilterableSelect: React.FunctionComponent<FilterableSelectProps> =
* @param {React.FormEvent<HTMLInputElement>} _event The input event.
* @param {string} value The new input value.
*/
const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => {
const onTextInputChange: (_event: React.FormEvent<HTMLInputElement>, value: string) => void = (
_event,
value,
) => {
setInputValue(value);
setFilterValue(value);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ export const LazyTextInput: React.FunctionComponent<LazyTextInputProps> = ({
}
};

const onChangeText: (value: string, event: React.FormEvent<HTMLInputElement>) => void = (
value,
) => {
setValue(value);
};

return (
<TextInput
spellCheck="false"
value={value}
type={type}
onChange={(value) => setValue(value)}
onChange={onChangeText}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
aria-label={ariaLabel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const MapsEdit: React.FC<MapsEditProps> = ({
export type MapsEditProps = {
providers: V1beta1Provider[];
selectedProviderName: string;
onChange: (value: string) => void;
onChange: (value: string, event: React.FormEvent<HTMLSelectElement>) => void;
label: string;
placeHolderLabel: string;
invalidLabel: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ export const ProvidersSection: React.FC<ProvidersSectionProps> = ({ obj }) => {
dispatch({ type: 'INIT', payload: obj });
};

const onChangeSource: (value: string, event: React.FormEvent<HTMLSelectElement>) => void = (
value,
) => {
dispatch({
type: 'SET_SOURCE_PROVIDER',
payload: providers.find((p) => p?.metadata?.name === value),
});
};

const onChangeTarget: (value: string, event: React.FormEvent<HTMLSelectElement>) => void = (
value,
) => {
dispatch({
type: 'SET_TARGET_PROVIDER',
payload: providers.find((p) => p?.metadata?.name === value),
});
};

return (
<Suspend obj={providers} loaded={providersLoaded} loadError={providersLoadError}>
<Flex className="forklift-network-map__details-tab--update-button">
Expand Down Expand Up @@ -88,12 +106,7 @@ export const ProvidersSection: React.FC<ProvidersSectionProps> = ({ obj }) => {
selectedProviderName={state.networkMap?.spec?.provider?.source?.name}
label={t('Source provider')}
placeHolderLabel={t('Select a provider')}
onChange={(value) =>
dispatch({
type: 'SET_SOURCE_PROVIDER',
payload: providers.find((p) => p?.metadata?.name === value),
})
}
onChange={onChangeSource}
invalidLabel={t('The chosen provider is no longer available.')}
mode={state.sourceProviderMode}
helpContent="source provider"
Expand All @@ -105,12 +118,7 @@ export const ProvidersSection: React.FC<ProvidersSectionProps> = ({ obj }) => {
selectedProviderName={state.networkMap?.spec?.provider?.destination?.name}
label={t('Target provider')}
placeHolderLabel={t('Select a provider')}
onChange={(value) =>
dispatch({
type: 'SET_TARGET_PROVIDER',
payload: providers.find((p) => p?.metadata?.name === value),
})
}
onChange={onChangeTarget}
invalidLabel={t('The chosen provider is no longer available.')}
mode={state.targetProviderMode}
helpContent="Target provider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const ProviderOption = (provider, index) => (
export type ProvidersEditProps = {
providers: V1beta1Provider[];
selectedProviderName: string;
onChange: (value: string) => void;
onChange: (value: string, event: React.FormEvent<HTMLSelectElement>) => void;
label: string;
placeHolderLabel: string;
invalidLabel: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const SettingsNumberInput: React.FC<SettingsSelectInputProps> = ({
setNewValue(newValue);
};

const onUserChange = (event: React.FormEvent<HTMLInputElement>) => {
const onUserChange: (event: React.FormEvent<HTMLInputElement>) => void = (event) => {
const value = (event.target as HTMLInputElement).value;
const newValue = value === '' ? value : +value;
setNewValue(newValue || 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export const DuplicateModal: React.FC<DuplicateModalProps> = ({ title, resource,
setNewNameValidation('success');
};

const onChange = (name: string) => {
validateName(name);
setNewName(name);
const onChange: (value: string, event: React.FormEvent<HTMLInputElement>) => void = (value) => {
validateName(value);
setNewName(value);
};

const onDuplicate = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export const PlanCutoverMigrationModal: React.FC<PlanCutoverMigrationModalProps>
setCutoverDate(migrationCutoverDate);
}, [lastMigration]);

const onDateChange = (inputDate, newDate: string) => {
const onDateChange: (
event: React.FormEvent<HTMLInputElement>,
value: string,
date?: Date,
) => void = (event, value) => {
const updatedFromDate = cutoverDate ? new Date(cutoverDate) : new Date();

const [year, month, day] = newDate.split('-').map((num: string) => parseInt(num, 10));
const [year, month, day] = value.split('-').map((num: string) => parseInt(num, 10));

updatedFromDate.setFullYear(year);
updatedFromDate.setMonth(month - 1);
Expand All @@ -71,7 +75,14 @@ export const PlanCutoverMigrationModal: React.FC<PlanCutoverMigrationModalProps>
setCutoverDate(updatedFromDate.toISOString());
};

const onTimeChange = (_event, _time, hour: number, minute: number) => {
const onTimeChange: (
event: React.FormEvent<HTMLInputElement>,
time: string,
hour?: number,
minute?: number,
seconds?: number,
isValid?: boolean,
) => void = (event, time, hour, minute) => {
const updatedFromDate = cutoverDate ? new Date(cutoverDate) : new Date();

updatedFromDate.setHours(hour);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ export const SearchInputProvider: React.FunctionComponent<SearchInputProviderPro
filterDispatch({ type: 'SET_NAME_FILTER', payload: value });
};

const onChange: (event: React.FormEvent<HTMLInputElement>, value: string) => void = (
event,
value,
) => {
updateNameFilter(value);
};

return (
<div className="forklift--create-plan--search-input-provider">
<SearchInput
placeholder={t('Filter provider')}
value={filterState.nameFilter}
onChange={(_, value) => updateNameFilter(value)}
onChange={onChange}
onClear={() => updateNameFilter('')}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const ProviderOption = (provider, index) => (
export type ProvidersEditProps = {
providers: V1beta1Provider[];
selectedProviderName: string;
onChange: (value: string) => void;
onChange: (value: string, event: React.FormEvent<HTMLSelectElement>) => void;
label: string;
placeHolderLabel: string;
invalidLabel: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ interface SwitchRendererProps {

const PreserveClusterCpuModelInputFactory: () => ModalInputComponentType = () => {
const SwitchRenderer: React.FC<SwitchRendererProps> = ({ value, onChange }) => {
const onChangeInternal = (v) => {
onChange(v ? 'true' : 'false');
const onChangeInternal: (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void = (
checked,
) => {
onChange(checked ? 'true' : 'false');
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ const PreserveStaticIPsInputFactory: () => ModalInputComponentType = () => {
const { t } = useForkliftTranslation();

const SwitchRenderer: React.FC<SwitchRendererProps> = ({ value, onChange }) => {
const onChangeInternal = (v) => {
onChange(v ? 'true' : 'false');
const onChangeInternal: (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void = (
checked,
) => {
onChange(checked ? 'true' : 'false');
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ interface SwitchRendererProps {

const WarmInputFactory: () => ModalInputComponentType = () => {
const SwitchRenderer: React.FC<SwitchRendererProps> = ({ value, onChange }) => {
const onChangeInternal = (v) => {
onChange(v ? 'true' : 'false');
const onChangeInternal: (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void = (
checked,
) => {
onChange(checked ? 'true' : 'false');
};

return (
Expand Down
Loading

0 comments on commit 1d0c35d

Please sign in to comment.