Skip to content
Merged
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
12 changes: 6 additions & 6 deletions packages/combobox/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 26329,
"minified": 14211,
"bundled": 26260,
"minified": 14174,
"gzipped": 4002
},
"index.esm.js": {
"bundled": 25261,
"minified": 13144,
"gzipped": 3979,
"bundled": 25192,
"minified": 13107,
"gzipped": 3980,
"treeshaked": {
"rollup": {
"code": 1296,
"import_statements": 177
},
"webpack": {
"code": 13081
"code": 13044
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/combobox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@babel/runtime": "^7.8.4",
"@zendeskgarden/container-field": "^3.0.7",
"@zendeskgarden/container-utilities": "^1.0.8",
"downshift": "^7.6.0"
"downshift": "^8.0.0"
},
"peerDependencies": {
"prop-types": "^15.6.1",
Expand Down
71 changes: 58 additions & 13 deletions packages/combobox/src/ComboboxContainer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React, { createRef, PropsWithChildren } from 'react';
import { act } from 'react-dom/test-utils';
import { render, RenderResult } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ComboboxContainer, useCombobox } from './';
Expand Down Expand Up @@ -279,19 +280,34 @@ describe('ComboboxContainer', () => {
}
});

it('expands and collapses the listbox on input click', async () => {
const { getByTestId } = render(<TestCombobox layout={layout} options={options} />);
const input = getByTestId('input');

expect(input).toHaveAttribute('aria-expanded', 'false');

await user.click(input);

expect(input).toHaveAttribute('aria-expanded', 'true');

await user.click(input);

expect(input).toHaveAttribute('aria-expanded', 'false');
});

describe('when focused', () => {
let input: HTMLElement;
let listboxOptions: HTMLElement[];

beforeEach(async () => {
beforeEach(() => {
const { getByTestId, getAllByRole } = render(
<TestCombobox layout={layout} options={options} />
);

input = getByTestId('input');
listboxOptions = getAllByRole('option');

await user.click(input);
input.focus();
});

it('expands and activates the first option on down arrow', async () => {
Expand Down Expand Up @@ -395,7 +411,7 @@ describe('ComboboxContainer', () => {
let listboxOptions: HTMLElement[];
let rerender: RenderResult['rerender'];

beforeEach(async () => {
beforeEach(() => {
const {
getByTestId,
getAllByRole,
Expand All @@ -415,7 +431,7 @@ describe('ComboboxContainer', () => {
listboxOptions = getAllByRole('option');
rerender = _rerender;

await user.click(input);
input.focus();
});

it('applies the correct accessibility attributes', () => {
Expand Down Expand Up @@ -548,7 +564,9 @@ describe('ComboboxContainer', () => {
expect(listboxOptions[1]).toHaveAttribute('aria-selected', 'false');
expect(listboxOptions[3]).toHaveAttribute('aria-selected', 'true');

await user.click(tags[1]);
await act(async () => {
await user.click(tags[1]);
});

expect(tags[1]).toHaveFocus();

Expand Down Expand Up @@ -667,6 +685,19 @@ describe('ComboboxContainer', () => {
expect(trigger).not.toHaveAttribute('aria-expanded');
}
});

it('remains collapsed on input click', async () => {
const { getByTestId } = render(
<TestCombobox isAutocomplete={false} layout={layout} options={options} />
);
const input = getByTestId('input');

expect(input).toHaveAttribute('aria-expanded', 'false');

await user.click(input);

expect(input).toHaveAttribute('aria-expanded', 'false');
});
});

describe('with disabled options', () => {
Expand Down Expand Up @@ -694,7 +725,7 @@ describe('ComboboxContainer', () => {
listboxOptions = getAllByRole('option');
rerender = _rerender;

await user.click(input);
input.focus();
await user.keyboard('{ArrowDown}');
});

Expand Down Expand Up @@ -769,11 +800,11 @@ describe('ComboboxContainer', () => {
await user.click(input);
await user.keyboard('{ArrowDown}');

expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledTimes(2);

const changeTypes = handleChange.mock.calls.map(([change]) => change.type);

expect(changeTypes).toMatchObject(['input:keyDown:ArrowDown']);
expect(changeTypes).toMatchObject(['input:click', 'input:keyDown:ArrowDown']);
});

it('handles controlled selection as expected', () => {
Expand Down Expand Up @@ -864,7 +895,12 @@ describe('ComboboxContainer', () => {
const triggerRef = createRef<HTMLDivElement>();
const inputRef = createRef<HTMLInputElement>();
const listboxRef = createRef<HTMLUListElement>();
const { selection: _selection, removeSelection: _removeSelection } = useCombobox({
const {
selection: _selection,
removeSelection: _removeSelection,
getInputProps,
getListboxProps
} = useCombobox({
triggerRef,
inputRef,
listboxRef,
Expand All @@ -875,7 +911,13 @@ describe('ComboboxContainer', () => {
selection = _selection;
removeSelection = _removeSelection;

return <>{children}</>;
return (
<>
<input {...getInputProps()} />
{children}
<ul {...getListboxProps({ 'aria-label': 'Options' })} />
Comment on lines +916 to +918
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downshift was throwing console errors for not calling the combobox getters

</>
);
};

it('clears multiselectable values', async () => {
Expand Down Expand Up @@ -1014,7 +1056,10 @@ describe('ComboboxContainer', () => {
const trigger = getByTestId('trigger');

await user.click(trigger);
await user.click(document.body);

await act(async () => {
await user.click(document.body);
});

expect(input).toHaveAttribute('aria-expanded', 'false');
});
Expand Down Expand Up @@ -1092,7 +1137,7 @@ describe('ComboboxContainer', () => {
<TestCombobox
layout="Garden"
isMultiselectable
options={[{ value: 'test-1' }, { value: 'test-2', selected: true }, { value: 'test-2' }]}
options={[{ value: 'test-1' }, { value: 'test-2', selected: true }, { value: 'test-3' }]}
/>
);
const tag = getByTestId('tag');
Expand Down Expand Up @@ -1123,7 +1168,7 @@ describe('ComboboxContainer', () => {
layout="Garden"
isEditable={false}
isMultiselectable
options={[{ value: 'test-1' }, { value: 'test-2', selected: true }, { value: 'test-2' }]}
options={[{ value: 'test-1' }, { value: 'test-2', selected: true }, { value: 'test-3' }]}
/>
);
const tag = getByTestId('tag');
Expand Down
41 changes: 19 additions & 22 deletions packages/combobox/src/useCombobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ export const useCombobox = <
false
};

case useDownshift.stateChangeTypes.InputFocus:
// Prevent expansion on focus.
return { ...state, isOpen: false };
case useDownshift.stateChangeTypes.InputClick:
if (!isAutocomplete) {
// Prevent input click listbox expansion on non-autocomplete comboboxes.
changes.isOpen = state.isOpen;
}

break;

case useDownshift.stateChangeTypes.InputKeyDownArrowDown:
case useDownshift.stateChangeTypes.FunctionOpenMenu:
Expand Down Expand Up @@ -300,7 +304,7 @@ export const useCombobox = <
initialHighlightedIndex: initialActiveIndex,
onStateChange: handleDownshiftStateChange,
stateReducer,
environment: win
environment: win as any /* HACK around Downshift's addition of Node to environment */
});

const closeListbox = useCallback(() => {
Expand Down Expand Up @@ -413,7 +417,7 @@ export const useCombobox = <

previousStateRef.current = {
...previousStateRef.current,
type: useDownshift.stateChangeTypes.InputFocus
type: useDownshift.stateChangeTypes.InputClick
};
}
});
Expand Down Expand Up @@ -449,11 +453,11 @@ export const useCombobox = <
};

if (isEditable && triggerContainsInput) {
const handleClick = (event: MouseEvent) => {
const handleClick = (event: React.MouseEvent) => {
if (disabled) {
event.preventDefault();
} else if (isAutocomplete) {
triggerProps.onClick(event);
triggerProps.onClick && triggerProps.onClick(event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚲 triggerProps?.onClick(event) also works, but I don't feel strongly. 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does. Instead of testing for invalid triggerProps, the updated Downshift is not guaranteeing that the trigger props will contain a valid onClick ... and we can't triggerProps.onClick?(event) 😉

} else {
inputRef.current?.focus();
}
Expand Down Expand Up @@ -591,13 +595,7 @@ export const useCombobox = <
);

const getInputProps = useCallback<IUseComboboxReturnValue['getInputProps']>(
({
role = isEditable ? 'combobox' : null,
'aria-labelledby': ariaLabeledBy = null,
onClick,
onFocus,
...other
} = {}) => {
({ role = isEditable ? 'combobox' : null, onClick, onFocus, ...other } = {}) => {
const inputProps = {
'data-garden-container-id': 'containers.combobox.input',
'data-garden-container-version': PACKAGE_VERSION,
Expand All @@ -613,11 +611,10 @@ export const useCombobox = <
triggerRef.current?.contains(event.target) &&
event.stopPropagation();

return getDownshiftInputProps({
return getDownshiftInputProps<any>({
...inputProps,
disabled,
role,
'aria-labelledby': ariaLabeledBy,
'aria-autocomplete': isAutocomplete ? 'list' : undefined,
onClick: composeEventHandlers(onClick, handleClick),
...getFieldInputProps(),
Expand Down Expand Up @@ -672,7 +669,7 @@ export const useCombobox = <
triggerRef.current?.contains(event.target) &&
event.stopPropagation();

const handleKeyDown = (event: KeyboardEvent) => {
const handleKeyDown = (event: React.KeyboardEvent) => {
if (event.key === KEYS.BACKSPACE || event.key === KEYS.DELETE) {
setDownshiftSelection(option.value);
} else {
Expand All @@ -699,7 +696,7 @@ export const useCombobox = <
triggerRef.current?.focus();
}

inputProps.onKeyDown(event);
inputProps.onKeyDown && inputProps.onKeyDown(event);
}
}
};
Expand All @@ -716,13 +713,12 @@ export const useCombobox = <
);

const getListboxProps = useCallback<IUseComboboxReturnValue['getListboxProps']>(
({ role = 'listbox', 'aria-labelledby': ariaLabeledBy = null, ...other }) =>
getDownshiftListboxProps({
({ role = 'listbox', ...other }) =>
getDownshiftListboxProps<any>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking re: any so I'm on the same page - is the intent here is to defer all typing to Garden's getListboxProps and prevent transient TS errors that may confuse consumers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of. imo, while well-intended, the associated Downshift PR kind of made a mess out of the getter prop types to the point where there was no reasonable combo that could satisfy what is going on – so I hit the any eject button. At least we're coercing the options object below to be of a valid Downshift type for the function call. But this is all deeply internal to the hook code and has no implications for the well-typed external API.

'data-garden-container-id': 'containers.combobox.listbox',
'data-garden-container-version': PACKAGE_VERSION,
ref: listboxRef,
role,
'aria-labelledby': ariaLabeledBy,
'aria-multiselectable': isMultiselectable ? true : undefined,
...other
} as IDownshiftListboxProps),
Expand Down Expand Up @@ -772,9 +768,10 @@ export const useCombobox = <
};
}

return getDownshiftOptionProps({
return getDownshiftOptionProps<any>({
item: option.value,
index: values.indexOf(option.value),
'aria-disabled': undefined,
'aria-selected': ariaSelected,
...optionProps
} as IDownshiftOptionProps<OptionValue>);
Expand Down
2 changes: 1 addition & 1 deletion packages/combobox/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const typeMap: Record<string, string> = {
[useDownshift.stateChangeTypes.FunctionSetInputValue]: 'fn:setInputValue',
[useDownshift.stateChangeTypes.InputBlur]: 'input:blur',
[useDownshift.stateChangeTypes.InputChange]: 'input:change',
[useDownshift.stateChangeTypes.InputFocus]: 'input:focus',
[useDownshift.stateChangeTypes.InputClick]: 'input:click',
[useDownshift.stateChangeTypes.InputKeyDownArrowDown]: `input:keyDown:${KEYS.DOWN}`,
[useDownshift.stateChangeTypes.InputKeyDownArrowUp]: `input:keyDown:${KEYS.UP}`,
[useDownshift.stateChangeTypes.InputKeyDownEnd]: `input:keyDown:${KEYS.END}`,
Expand Down
8 changes: 4 additions & 4 deletions packages/combobox/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ compute-scroll-into-view@^2.0.4:
resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-2.0.4.tgz#2b444b2b9e4724819d2531efacb7ac094155fdf6"
integrity sha512-y/ZA3BGnxoM/QHHQ2Uy49CLtnWPbt4tTPpEEZiEmmiWBFKjej7nEyH8Ryz54jH0MLXflUYA3Er2zUxPSJu5R+g==

downshift@^7.6.0:
version "7.6.2"
resolved "https://registry.yarnpkg.com/downshift/-/downshift-7.6.2.tgz#16fc951b7ff8f9c1c47d0f71b5ff10d78fb06e4c"
integrity sha512-iOv+E1Hyt3JDdL9yYcOgW7nZ7GQ2Uz6YbggwXvKUSleetYhU2nXD482Rz6CzvM4lvI1At34BYruKAL4swRGxaA==
downshift@^8.0.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/downshift/-/downshift-8.1.0.tgz#72023513256134723fe807a54168ebc64f9ddf6c"
integrity sha512-e9EBBLZvB2G73qT272x3hExttGCH1q1usbjirm+1aMcFXuzSWhgBdbnAHPlFI2rEq61cU/kDrEIMrY+ozMhvmg==
dependencies:
"@babel/runtime" "^7.14.8"
compute-scroll-into-view "^2.0.4"
Expand Down