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(Picker): fixing keyboard handling when search is disabled #1315

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';

import { FloatingNode, FloatingPortal } from '@floating-ui/react';

import noop from '../../utils/noop';
import { Input } from '../Input';
import { IPickerListItem, PickerList } from '../Picker';
import { DEFAULT_LIST_HEIGHT, MIN_LIST_HEIGHT } from '../Picker/constants';
Expand Down Expand Up @@ -139,6 +140,7 @@ export const AutoComplete = React.forwardRef<
virtualItemRef,
listElementsRef,
activeIndex,
isPositioned,
} = useFloatingPicker({
openedOnInit: autocompleteOpenOnInit,
disabled: disabled || readOnly,
Expand Down Expand Up @@ -182,6 +184,9 @@ export const AutoComplete = React.forwardRef<
hideWhenEmpty
getItemProps={getItemProps}
onSelect={handleSelect}
isPositioned={isPositioned}
searchDisabled={false}
onItemRemove={noop}
/>
</FloatingPortal>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const PickerComponent = (args: IPickerProps) => {
return (
<Picker
{...args}
openedOnInit={false}
searchDisabled={true}
VadymBezpalko marked this conversation as resolved.
Show resolved Hide resolved
selected={selectedItems}
onSelect={(items) => setSelectedItems(items)}
/>
Expand Down
4 changes: 4 additions & 0 deletions packages/react-components/src/components/Picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const Picker: React.FC<IPickerProps> = ({
activeIndex,
maxHeight,
setPointer,
isPositioned,
} = useFloatingPicker({
openedOnInit,
disabled,
Expand Down Expand Up @@ -158,6 +159,9 @@ export const Picker: React.FC<IPickerProps> = ({
activeIndex={activeIndex}
selectedKeys={selectedKeys}
listElementsRef={listElementsRef}
searchDisabled={searchDisabled}
isPositioned={isPositioned}
onItemRemove={handleItemRemove}
setPointer={setPointer}
onSelect={handleSelect}
getFloatingProps={getFloatingProps}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';

import { VirtuosoProps } from 'react-virtuoso';
import { vitest } from 'vitest';

import { render, vi } from 'test-utils';
import { render, vi, vitest } from 'test-utils';

import noop from '../../../utils/noop';
import { DEFAULT_PICKER_OPTIONS } from '../constants';
Expand All @@ -28,6 +27,9 @@ vitest.mock('react-virtuoso', () => {
window.HTMLElement.prototype.scrollIntoView = () => {};

const defaultProps: IPickerListProps = {
isPositioned: false,
onItemRemove: noop,
searchDisabled: false,
activeIndex: null,
context: {
x: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';

import { FloatingContext, FloatingFocusManager } from '@floating-ui/react';
import cx from 'clsx';
Expand All @@ -23,6 +23,9 @@ export interface IPickerListProps {
listElementsRef: React.MutableRefObject<(HTMLElement | null)[]>;
activeIndex: number | null;
selectedKeys: string[];
isPositioned: boolean;
searchDisabled: boolean;
onItemRemove: (key: string) => void;
setPointer: (pointer: boolean) => void;
onSelect: (key: string) => void;
getFloatingProps: (
Expand All @@ -46,6 +49,9 @@ export const PickerList: React.FC<IPickerListProps> = ({
activeIndex,
selectedKeys,
listElementsRef,
isPositioned,
searchDisabled,
onItemRemove,
setPointer,
onSelect,
getFloatingProps,
Expand All @@ -57,6 +63,7 @@ export const PickerList: React.FC<IPickerListProps> = ({
virtuosoProps,
}) => {
const [listHeight, setListHeight] = React.useState(0);
const wrapperRef = useRef<HTMLDivElement>(null);
const numberOfItems = options.length;

const handleListHeightChange = React.useCallback(
Expand All @@ -73,6 +80,13 @@ export const PickerList: React.FC<IPickerListProps> = ({
[numberOfItems]
);

useEffect(() => {
// focusing the list to enable keyboard handlers for items selection when the search is disabled
if (searchDisabled && isPositioned && activeIndex !== null) {
wrapperRef.current?.focus();
}
}, [searchDisabled, isPositioned, activeIndex]);

useEffect(() => {
listElementsRef.current = new Array(options.length);
}, [options.length]);
Expand Down Expand Up @@ -101,6 +115,12 @@ export const PickerList: React.FC<IPickerListProps> = ({
(key === SELECT_ALL_OPTION_KEY &&
selectedKeys.length === getNormalizedItems(options).length);

const handleItemRemove = () => {
if (pickerType === 'multi') {
onItemRemove(selectedKeys[selectedKeys.length - 1]);
}
};

return (
<FloatingFocusManager context={context} modal={false} initialFocus={-1}>
<div
Expand All @@ -114,12 +134,11 @@ export const PickerList: React.FC<IPickerListProps> = ({
}}
>
<div
ref={wrapperRef}
tabIndex={0}
aria-multiselectable={pickerType === 'multi'}
className={styles['listbox-wrapper']}
// Some screen readers do not like any wrapper tags inside
// the element with the role, so we spread it onto the
// virtualizer wrapper.
// Those handlers are run only when the search is disabled (only then the list is focused) in other cases this is handled by the search input
{...getFloatingProps({
onKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {
setPointer(false);
Expand All @@ -128,6 +147,10 @@ export const PickerList: React.FC<IPickerListProps> = ({
onSelect(options[activeIndex].key);
}

if (e.key === 'Backspace' || e.key === 'Delete') {
handleItemRemove();
}

if (e.key === ' ') {
e.preventDefault();
}
Expand Down
Loading