Skip to content

Commit

Permalink
fix(core): forward props in getEnvironmentProps
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 1, 2021
1 parent 2c953f6 commit af49483
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@ import { createPlayground } from '../../../../test/utils';
import { createAutocomplete } from '../createAutocomplete';

describe('getEnvironmentProps', () => {
test.todo('forwards the remaining props');
test('forwards the remaining props', () => {
const { getEnvironmentProps, formElement, inputElement } = createPlayground(
createAutocomplete,
{}
);
const panelElement = document.createElement('div');

const environmentProps = getEnvironmentProps({
inputElement,
formElement,
panelElement,
customProps: {},
});

expect(environmentProps).toEqual(
expect.objectContaining({ customProps: {} })
);
});

describe('onTouchStart', () => {
test('is a noop when panel is not open', () => {
Expand Down
37 changes: 19 additions & 18 deletions packages/autocomplete-core/src/getPropGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export function getPropGetters<
TMouseEvent,
TKeyboardEvent
>({ props, refresh, store, ...setters }: GetPropGettersOptions<TItem>) {
const getEnvironmentProps: GetEnvironmentProps = (getterProps) => {
const getEnvironmentProps: GetEnvironmentProps = (providedProps) => {
const { inputElement, formElement, panelElement, ...rest } = providedProps;

return {
// On touch devices, we do not rely on the native `blur` event of the
// input to close the panel, but rather on a custom `touchstart` event
Expand All @@ -38,25 +40,24 @@ export function getPropGetters<
onTouchStart(event) {
if (
store.getState().isOpen === false ||
event.target === getterProps.inputElement
event.target === inputElement
) {
return;
}

// @TODO: support cases where there are multiple Autocomplete instances.
// Right now, a second instance makes this computation return false.
const isTargetWithinAutocomplete = [
getterProps.formElement,
getterProps.panelElement,
].some((contextNode) => {
return (
isOrContainsNode(contextNode, event.target as Node) ||
isOrContainsNode(
contextNode,
props.environment.document.activeElement!
)
);
});
const isTargetWithinAutocomplete = [formElement, panelElement].some(
(contextNode) => {
return (
isOrContainsNode(contextNode, event.target as Node) ||
isOrContainsNode(
contextNode,
props.environment.document.activeElement!
)
);
}
);

if (isTargetWithinAutocomplete === false) {
store.dispatch('blur', null);
Expand All @@ -69,15 +70,15 @@ export function getPropGetters<
onTouchMove(event: TouchEvent) {
if (
store.getState().isOpen === false ||
getterProps.inputElement !==
props.environment.document.activeElement ||
event.target === getterProps.inputElement
inputElement !== props.environment.document.activeElement ||
event.target === inputElement
) {
return;
}

getterProps.inputElement.blur();
inputElement.blur();
},
...rest,
};
};

Expand Down

0 comments on commit af49483

Please sign in to comment.