From c944633bf46568c315136f864bf9d98207d2e0a8 Mon Sep 17 00:00:00 2001 From: Sharon Gratch Date: Tue, 18 Jun 2024 14:07:58 +0300 Subject: [PATCH] Update to PF5 - part II - onSelect, onToggle event handlers Reference: https://github.com/kubev2v/forklift-console-plugin/issues/1098 For avoiding uncaught PF 4 -> PF 5 migration errors, this PR named and typed (i.e. (add type to function signature) all onSelect, onToggle callbacks appearances which use parameters. Signed-off-by: Sharon Gratch --- .../ActionServiceDropdown.tsx | 15 ++++++- .../components/Filter/AutocompleteFilter.tsx | 39 +++++++++++++----- .../src/components/Filter/EnumFilter.tsx | 31 ++++++++++---- .../components/Filter/GroupedEnumFilter.tsx | 33 +++++++++++---- .../FilterGroup/AttributeValueFilter.tsx | 19 ++++++++- .../Overview/modal/SettingsSelectInput.tsx | 12 ++++-- .../create/components/SelectProvider.tsx | 14 ++++--- .../details/components/MappingListItem.tsx | 39 ++++++++++++------ .../tabs/Hosts/modals/VSphereNetworkModal.tsx | 9 ++++- .../migrate/components/MappingListItem.tsx | 40 +++++++++++++------ 10 files changed, 184 insertions(+), 67 deletions(-) diff --git a/packages/common/src/components/ActionServiceDropdown/ActionServiceDropdown.tsx b/packages/common/src/components/ActionServiceDropdown/ActionServiceDropdown.tsx index 91222d3c1..4e279dcde 100644 --- a/packages/common/src/components/ActionServiceDropdown/ActionServiceDropdown.tsx +++ b/packages/common/src/components/ActionServiceDropdown/ActionServiceDropdown.tsx @@ -67,12 +67,23 @@ const ActionsAsDropdown = ({ const history = useHistory(); const [isActionMenuOpen, setIsActionMenuOpen] = useState(false); const isPlain = variant === 'kebab'; + const onToggle: ( + isExpanded: boolean, + event: + | Event + | React.KeyboardEvent + | React.MouseEvent + | React.ChangeEvent, + ) => void = (isExpanded) => { + setIsActionMenuOpen(isExpanded); + }; const toggle = variant === 'kebab' ? ( - + ) : ( - {dropdownToggleText} + {dropdownToggleText} ); + return ( | React.ChangeEvent, + value: string | SelectOptionObject, + isPlaceholder?: boolean, + ) => void = (event, value, isPlaceholder) => { + if (isPlaceholder) { + return; + } + // behave as Console's AutocompleteInput used i.e. to filter pods by label + if (!hasFilter(value)) { + addFilter(value); + } + setExpanded(false); + }; + + const onToggle: ( + isExpanded: boolean, + event: + | Event + | React.KeyboardEvent + | React.MouseEvent + | React.ChangeEvent, + ) => void = (isExpanded) => { + setExpanded(isExpanded); + }; + return ( { - if (isPlaceholder) { - return; - } - // behave as Console's AutocompleteInput used i.e. to filter pods by label - if (!hasFilter(option)) { - addFilter(option); - } - setExpanded(false); - }} + onSelect={onSelect} // intentionally keep the selections list empty // the select should pretend to be stateless // the selection is stored outside in a new chip selections={[]} placeholderText={placeholderLabel} isOpen={isExpanded} - onToggle={setExpanded} + onToggle={onToggle} onFilter={onFilter} shouldResetOnSelect={true} > diff --git a/packages/common/src/components/Filter/EnumFilter.tsx b/packages/common/src/components/Filter/EnumFilter.tsx index 7e6ef6e6d..2443072e4 100644 --- a/packages/common/src/components/Filter/EnumFilter.tsx +++ b/packages/common/src/components/Filter/EnumFilter.tsx @@ -138,6 +138,28 @@ export const EnumFilter = ({ } }; + const onSelect: ( + event: React.MouseEvent | React.ChangeEvent, + value: string | SelectOptionObject, + isPlaceholder?: boolean, + ) => void = (event, value, isPlaceholder) => { + if (isPlaceholder) { + return; + } + hasFilter(value) ? deleteFilter(value) : addFilter(value); + }; + + const onToggle: ( + isExpanded: boolean, + event: + | Event + | React.KeyboardEvent + | React.MouseEvent + | React.ChangeEvent, + ) => void = (isExpanded) => { + setExpanded(isExpanded); + }; + return ( { - if (isPlaceholder) { - return; - } - hasFilter(option) ? deleteFilter(option) : addFilter(option); - }} + onSelect={onSelect} selections={selectedUniqueEnumLabels} placeholderText={placeholderLabel} isOpen={isExpanded} - onToggle={setExpanded} + onToggle={onToggle} > {uniqueEnumLabels.map((label) => ( diff --git a/packages/common/src/components/Filter/GroupedEnumFilter.tsx b/packages/common/src/components/Filter/GroupedEnumFilter.tsx index 2d546564e..bfed41721 100644 --- a/packages/common/src/components/Filter/GroupedEnumFilter.tsx +++ b/packages/common/src/components/Filter/GroupedEnumFilter.tsx @@ -107,6 +107,29 @@ export const GroupedEnumFilter = ({ return filteredGroups; }; + const onSelect: ( + event: React.MouseEvent | React.ChangeEvent, + value: string | SelectOptionObject, + isPlaceholder?: boolean, + ) => void = (event, value, isPlaceholder) => { + if (isPlaceholder) { + return; + } + const id = typeof value === 'string' ? value : (value as EnumValue).id; + hasFilter(id) ? deleteFilter(id) : addFilter(id); + }; + + const onToggle: ( + isExpanded: boolean, + event: + | Event + | React.KeyboardEvent + | React.MouseEvent + | React.ChangeEvent, + ) => void = (isExpanded) => { + setExpanded(isExpanded); + }; + return ( <> {/** @@ -141,19 +164,13 @@ export const GroupedEnumFilter = ({ variant={SelectVariant.checkbox} isGrouped aria-label={placeholderLabel} - onSelect={(event, option, isPlaceholder) => { - if (isPlaceholder) { - return; - } - const id = typeof option === 'string' ? option : (option as EnumValue).id; - hasFilter(id) ? deleteFilter(id) : addFilter(id); - }} + onSelect={onSelect} selections={supportedEnumValues .filter(({ id }) => selectedEnumIds.includes(id)) .map(toSelectOption)} placeholderText={placeholderLabel} isOpen={isExpanded} - onToggle={setExpanded} + onToggle={onToggle} hasInlineFilter={hasInlineFilter} onFilter={onFilter} > diff --git a/packages/common/src/components/FilterGroup/AttributeValueFilter.tsx b/packages/common/src/components/FilterGroup/AttributeValueFilter.tsx index d8623b08d..a242f31e7 100644 --- a/packages/common/src/components/FilterGroup/AttributeValueFilter.tsx +++ b/packages/common/src/components/FilterGroup/AttributeValueFilter.tsx @@ -44,19 +44,34 @@ export const AttributeValueFilter = ({ const selectOptionToFilter = (selectedId) => fieldFilters.find(({ resourceFieldId }) => resourceFieldId === selectedId) ?? currentFilter; - const onFilterTypeSelect = (event, value, isPlaceholder) => { + const onFilterTypeSelect: ( + event: React.MouseEvent | React.ChangeEvent, + value: string | SelectOptionObject, + isPlaceholder?: boolean, + ) => void = (_event, value: IdOption, isPlaceholder) => { if (!isPlaceholder) { setCurrentFilter(selectOptionToFilter(value?.id)); setExpanded(!expanded); } }; + const onToggle: ( + isExpanded: boolean, + event: + | Event + | React.KeyboardEvent + | React.MouseEvent + | React.ChangeEvent, + ) => void = (isExpanded) => { + setExpanded(isExpanded); + }; + return (