Skip to content
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

Fix for select components causing scroll container to jump #3134

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 59 additions & 50 deletions frontend/src/components/MultiSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import {
HelperTextItem,
SelectGroup,
Divider,
SelectPopperProps,
} from '@patternfly/react-core';
import { TimesIcon } from '@patternfly/react-icons/dist/esm/icons/times-icon';
import { TimesIcon } from '@patternfly/react-icons';
import { WithScrollContainer } from '~/utilities/WithScrollContainer';

export type SelectionOptions = {
id: number | string;
Expand All @@ -42,6 +44,7 @@ type MultiSelectionProps = {
selectionRequired?: boolean;
noSelectedOptionsMessage?: string;
toggleTestId?: string;
popperProps?: SelectPopperProps;
};

export const MultiSelection: React.FC<MultiSelectionProps> = ({
Expand All @@ -56,6 +59,7 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
toggleTestId,
selectionRequired,
noSelectedOptionsMessage = 'One or more options must be selected',
popperProps = {},
}) => {
const [isOpen, setIsOpen] = React.useState(false);
const [inputValue, setInputValue] = React.useState<string>('');
Expand Down Expand Up @@ -255,28 +259,50 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
);

return (
<>
<Select
id={id}
isOpen={isOpen}
selected={selected}
onSelect={(ev, selection) => {
const selectedOption = allOptions.find((option) => option.id === selection);
onSelect(selectedOption);
}}
onOpenChange={() => setOpen(false)}
toggle={toggle}
>
{visibleOptions.length === 0 && inputValue ? (
<SelectList isAriaMultiselectable>
<SelectOption isDisabled>No results found</SelectOption>
</SelectList>
) : null}
{selectGroups.map((g, index) => (
<>
<SelectGroup label={g.name} key={g.id}>
<WithScrollContainer>
{(scrollContainer) => (
<>
<Select
id={id}
isOpen={isOpen}
selected={selected}
onSelect={(ev, selection) => {
const selectedOption = allOptions.find((option) => option.id === selection);
onSelect(selectedOption);
}}
onOpenChange={() => setOpen(false)}
toggle={toggle}
popperProps={{ appendTo: scrollContainer, ...popperProps }}
>
{visibleOptions.length === 0 && inputValue ? (
<SelectList isAriaMultiselectable>
{g.values.map((option) => (
<SelectOption isDisabled>No results found</SelectOption>
</SelectList>
) : null}
{selectGroups.map((g, index) => (
<>
<SelectGroup label={g.name} key={g.id}>
<SelectList isAriaMultiselectable>
{g.values.map((option) => (
<SelectOption
key={option.name}
isFocused={focusedItemIndex === option.index}
id={`select-multi-typeahead-${option.name.replace(' ', '-')}`}
value={option.id}
ref={null}
isSelected={option.selected}
>
{option.name}
</SelectOption>
))}
</SelectList>
</SelectGroup>
{index < selectGroups.length - 1 || selectOptions.length ? <Divider /> : null}
</>
))}
{selectOptions.length ? (
<SelectList isAriaMultiselectable>
{selectOptions.map((option) => (
<SelectOption
key={option.name}
isFocused={focusedItemIndex === option.index}
Expand All @@ -289,34 +315,17 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
</SelectOption>
))}
</SelectList>
</SelectGroup>
{index < selectGroups.length - 1 || selectOptions.length ? <Divider /> : null}
</>
))}
{selectOptions.length ? (
<SelectList isAriaMultiselectable>
{selectOptions.map((option) => (
<SelectOption
key={option.name}
isFocused={focusedItemIndex === option.index}
id={`select-multi-typeahead-${option.name.replace(' ', '-')}`}
value={option.id}
ref={null}
isSelected={option.selected}
>
{option.name}
</SelectOption>
))}
</SelectList>
) : null}
</Select>
{noSelectedItems && selectionRequired && (
<HelperText isLiveRegion>
<HelperTextItem variant="error" hasIcon data-testid="group-selection-error-text">
{noSelectedOptionsMessage}
</HelperTextItem>
</HelperText>
) : null}
</Select>
{noSelectedItems && selectionRequired && (
<HelperText isLiveRegion>
<HelperTextItem variant="error" hasIcon data-testid="group-selection-error-text">
{noSelectedOptionsMessage}
</HelperTextItem>
</HelperText>
)}
</>
)}
</>
</WithScrollContainer>
);
};
117 changes: 64 additions & 53 deletions frontend/src/components/SimpleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Divider,
MenuToggleProps,
} from '@patternfly/react-core';
import { WithScrollContainer } from '~/utilities/WithScrollContainer';

import './SimpleSelect.scss';

Expand Down Expand Up @@ -56,6 +57,7 @@ const SimpleSelect: React.FC<SimpleSelectProps> = ({
icon,
dataTestId,
toggleProps,
popperProps = {},
...props
}) => {
const [open, setOpen] = React.useState(false);
Expand All @@ -70,41 +72,66 @@ const SimpleSelect: React.FC<SimpleSelectProps> = ({
const selectedLabel = selectedOption?.label ?? placeholder;

return (
<Select
{...props}
isOpen={open}
selected={value || toggleLabel}
onSelect={(e, selectValue) => {
onChange(
String(selectValue),
!!selectValue && (findOptionForKey(String(selectValue))?.isPlaceholder ?? false),
);
setOpen(false);
}}
onOpenChange={setOpen}
toggle={(toggleRef) => (
<MenuToggle
ref={toggleRef}
data-testid={dataTestId}
aria-label="Options menu"
onClick={() => setOpen(!open)}
icon={icon}
isExpanded={open}
isDisabled={isDisabled}
isFullWidth={isFullWidth}
{...toggleProps}
<WithScrollContainer>
{(scrollContainer) => (
<Select
{...props}
popperProps={{ appendTo: scrollContainer, ...popperProps }}
isOpen={open}
selected={value || toggleLabel}
onSelect={(e, selectValue) => {
onChange(
String(selectValue),
!!selectValue && (findOptionForKey(String(selectValue))?.isPlaceholder ?? false),
);
setOpen(false);
}}
onOpenChange={setOpen}
toggle={(toggleRef) => (
<MenuToggle
ref={toggleRef}
data-testid={dataTestId}
aria-label="Options menu"
onClick={() => setOpen(!open)}
icon={icon}
isExpanded={open}
isDisabled={isDisabled}
isFullWidth={isFullWidth}
{...toggleProps}
>
{toggleLabel || (
<Truncate content={selectedLabel} className="truncate-no-min-width" />
)}
</MenuToggle>
)}
shouldFocusToggleOnSelect
>
{toggleLabel || <Truncate content={selectedLabel} className="truncate-no-min-width" />}
</MenuToggle>
)}
shouldFocusToggleOnSelect
>
{groupedOptions?.map((group, index) => (
<>
{index > 0 ? <Divider /> : null}
<SelectGroup key={group.key} label={group.label}>
{groupedOptions?.map((group, index) => (
<>
{index > 0 ? <Divider /> : null}
<SelectGroup key={group.key} label={group.label}>
<SelectList>
{group.options.map(
({ key, label, dropdownLabel, description, isDisabled: optionDisabled }) => (
<SelectOption
key={key}
value={key}
description={description}
isDisabled={optionDisabled}
data-testid={key}
>
{dropdownLabel || label}
</SelectOption>
),
)}
</SelectList>
</SelectGroup>
</>
)) ?? null}
{options?.length ? (
<SelectList>
{group.options.map(
{groupedOptions?.length ? <Divider /> : null}
{options.map(
({ key, label, dropdownLabel, description, isDisabled: optionDisabled }) => (
<SelectOption
key={key}
Expand All @@ -118,26 +145,10 @@ const SimpleSelect: React.FC<SimpleSelectProps> = ({
),
)}
</SelectList>
</SelectGroup>
</>
)) ?? null}
{options?.length ? (
<SelectList>
{groupedOptions?.length ? <Divider /> : null}
{options.map(({ key, label, dropdownLabel, description, isDisabled: optionDisabled }) => (
<SelectOption
key={key}
value={key}
description={description}
isDisabled={optionDisabled}
data-testid={key}
>
{dropdownLabel || label}
</SelectOption>
))}
</SelectList>
) : null}
</Select>
) : null}
</Select>
)}
</WithScrollContainer>
);
};

Expand Down
59 changes: 34 additions & 25 deletions frontend/src/components/TypeaheadSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
Button,
MenuToggleProps,
SelectProps,
SelectPopperProps,
} from '@patternfly/react-core';
import { TimesIcon } from '@patternfly/react-icons';
import { WithScrollContainer } from '~/utilities/WithScrollContainer';

export interface TypeaheadSelectOption extends Omit<SelectOptionProps, 'content' | 'isSelected'> {
/** Content of the select option. */
Expand Down Expand Up @@ -66,6 +68,7 @@ export interface TypeaheadSelectProps extends Omit<SelectProps, 'toggle' | 'onSe
toggleWidth?: string;
/** Additional props passed to the toggle. */
toggleProps?: MenuToggleProps;
popperProps?: SelectPopperProps;
}

const defaultNoOptionsFoundMessage = (filter: string) => `No results found for "${filter}"`;
Expand All @@ -92,6 +95,7 @@ const TypeaheadSelect: React.FunctionComponent<TypeaheadSelectProps> = ({
isDisabled,
toggleWidth,
toggleProps,
popperProps = {},
...props
}: TypeaheadSelectProps) => {
const [isOpen, setIsOpen] = React.useState(false);
Expand Down Expand Up @@ -395,31 +399,36 @@ const TypeaheadSelect: React.FunctionComponent<TypeaheadSelectProps> = ({
);

return (
<Select
isOpen={isOpen}
selected={selected}
onSelect={handleSelect}
onOpenChange={(open) => !open && closeMenu()}
toggle={toggle}
ref={innerRef}
{...props}
>
<SelectList>
{filteredSelections.map((option, index) => {
const { content, value, ...optionProps } = option;
return (
<SelectOption
key={value}
value={value}
isFocused={focusedItemIndex === index}
{...optionProps}
>
{content}
</SelectOption>
);
})}
</SelectList>
</Select>
<WithScrollContainer>
{(scrollContainer) => (
<Select
isOpen={isOpen}
selected={selected}
onSelect={handleSelect}
onOpenChange={(open) => !open && closeMenu()}
toggle={toggle}
ref={innerRef}
popperProps={{ appendTo: scrollContainer, ...popperProps }}
{...props}
>
<SelectList>
{filteredSelections.map((option, index) => {
const { content, value, ...optionProps } = option;
return (
<SelectOption
key={value}
value={value}
isFocused={focusedItemIndex === index}
{...optionProps}
>
{content}
</SelectOption>
);
})}
</SelectList>
</Select>
)}
</WithScrollContainer>
);
};

Expand Down
Loading
Loading