-
Notifications
You must be signed in to change notification settings - Fork 117
[ACM-30639] General wizard UX improvements for placement #6010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8719d8
[ACM-30639] General wizard UX improvements for placement
Randy424 6cef396
[ACM-30639] Address CodeRabbit review: toleration validation and merg…
Randy424 df90e37
[ACM-30639] Remove dead DisplayMode.Details code from WizLabelSelect
Randy424 4ed4e58
[ACM-30639] Localize toleration options and use Map for label lookup
Randy424 8ee3c71
[ACM-30639] Deduplicate tolerations and strengthen test assertions
Randy424 cd592cb
[ACM-30639] Remove console.error to fix no-console lint rule
Randy424 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
frontend/packages/react-form-wizard/src/inputs/WizLabelSelect.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* Copyright Contributors to the Open Cluster Management project */ | ||
| import { Label, MenuToggle, MenuToggleElement, Select as PfSelect } from '@patternfly/react-core' | ||
| import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react' | ||
| import { useStringContext } from '../contexts/StringContext' | ||
| import { getSelectPlaceholder, InputCommonProps, useInput } from './Input' | ||
| import { InputSelect, SelectListOptions } from './InputSelect' | ||
| import { WizFormGroup } from './WizFormGroup' | ||
|
|
||
| import './Select.css' | ||
|
|
||
| export interface WizLabelSelectOption { | ||
| label: string | ||
| value: string | ||
| } | ||
|
|
||
| export type WizLabelSelectProps = InputCommonProps<string> & { | ||
| label: string | ||
| placeholder?: string | ||
| isCreatable?: boolean | ||
| footer?: ReactNode | ||
| options: (string | WizLabelSelectOption)[] | ||
| } | ||
|
|
||
| /** | ||
| * A single-select dropdown that displays the selected value as a PatternFly Label pill, | ||
| * similar to how WizMultiSelect displays its selected values. | ||
| * When no value is selected, shows a typeahead input with placeholder. | ||
| * When a value is selected, shows a simple toggle with the Label pill. | ||
| */ | ||
| export function WizLabelSelect(props: WizLabelSelectProps) { | ||
| const { value, setValue, validated, hidden, id, disabled, required } = useInput(props) | ||
| const { noResults } = useStringContext() | ||
| const { readonly, isCreatable, footer } = props | ||
| const placeholder = getSelectPlaceholder(props) | ||
| const [open, setOpen] = useState(false) | ||
| const [filteredOptions, setFilteredOptions] = useState<string[]>([]) | ||
|
|
||
| // Normalize options to { label, value } pairs | ||
| const normalizedOptions = useMemo( | ||
| () => props.options.map((opt) => (typeof opt === 'string' ? { label: opt, value: opt } : opt)), | ||
| [props.options] | ||
| ) | ||
|
|
||
| // String options for InputSelect (uses labels for display) | ||
| const stringOptions = useMemo(() => normalizedOptions.map((opt) => opt.label), [normalizedOptions]) | ||
|
|
||
| const labelToValue = useMemo(() => { | ||
| const map = new Map<string, string>() | ||
| for (const opt of normalizedOptions) { | ||
| if (!map.has(opt.label)) { | ||
| map.set(opt.label, opt.value) | ||
| } | ||
| } | ||
| return map | ||
| }, [normalizedOptions]) | ||
|
|
||
| useEffect(() => { | ||
| setFilteredOptions(stringOptions) | ||
| }, [stringOptions]) | ||
|
|
||
| // Find display label for the current value | ||
| const displayLabel = useMemo(() => { | ||
| const match = normalizedOptions.find((opt) => opt.value === value) | ||
| return match?.label ?? value ?? '' | ||
| }, [normalizedOptions, value]) | ||
|
|
||
| const onSelect = useCallback( | ||
| (selectedLabel: string | undefined) => { | ||
| if (!selectedLabel) { | ||
| setValue(undefined) | ||
| setOpen(false) | ||
| return | ||
| } | ||
| const selectedValue = labelToValue.get(selectedLabel) ?? selectedLabel | ||
|
|
||
| if (selectedValue === value) { | ||
| setValue(undefined) | ||
| } else { | ||
| setValue(selectedValue) | ||
| } | ||
| setOpen(false) | ||
| }, | ||
| [setValue, value, labelToValue] | ||
| ) | ||
|
|
||
| const handleSetOptions = useCallback( | ||
| (o: string[]) => { | ||
| if (o.length > 0) { | ||
| const filtered = stringOptions.filter((option) => o.includes(option)) | ||
| if (displayLabel && !stringOptions.includes(displayLabel)) { | ||
| filtered.unshift(displayLabel) | ||
| } | ||
| setFilteredOptions([...new Set([...filtered, ...o])]) | ||
| } else { | ||
| setFilteredOptions([noResults]) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }, | ||
| [noResults, stringOptions, displayLabel] | ||
| ) | ||
|
|
||
| if (hidden) return null | ||
|
|
||
| const toggle = (toggleRef: React.Ref<MenuToggleElement>) => | ||
| value ? ( | ||
| <MenuToggle | ||
| ref={toggleRef} | ||
| onClick={() => { | ||
| if (!open) setFilteredOptions(stringOptions) | ||
| setOpen(!open) | ||
| }} | ||
| isExpanded={open} | ||
| isDisabled={disabled || readonly} | ||
| status={validated === 'error' ? 'danger' : undefined} | ||
| > | ||
| <Label variant="outline">{displayLabel}</Label> | ||
| </MenuToggle> | ||
| ) : ( | ||
| <InputSelect | ||
| disabled={disabled || readonly} | ||
| validated={validated} | ||
| placeholder={placeholder} | ||
| required={required} | ||
| options={stringOptions} | ||
| setOptions={handleSetOptions} | ||
| isCreatable={isCreatable} | ||
| toggleRef={toggleRef} | ||
| value="" | ||
| onSelect={onSelect} | ||
| open={open} | ||
| setOpen={setOpen} | ||
| /> | ||
| ) | ||
|
|
||
| return ( | ||
| <div id={id}> | ||
| <WizFormGroup {...props} id={id}> | ||
| <PfSelect | ||
| isOpen={open} | ||
| onOpenChange={(isOpen) => { | ||
| if (!isOpen) { | ||
| setOpen(false) | ||
| } | ||
| }} | ||
| toggle={toggle} | ||
| selected={displayLabel} | ||
| onSelect={(_event, value) => { | ||
| const selected = value?.toString() ?? '' | ||
| if (selected !== noResults) { | ||
| onSelect(selected) | ||
| } | ||
| }} | ||
| isScrollable | ||
| > | ||
| <SelectListOptions | ||
| value={displayLabel} | ||
| allOptions={stringOptions} | ||
| filteredOptions={filteredOptions} | ||
| isCreatable={isCreatable} | ||
| footer={footer} | ||
| /> | ||
| </PfSelect> | ||
| </WizFormGroup> | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.