Skip to content

Commit af49483

Browse files
fix(core): forward props in getEnvironmentProps
1 parent 2c953f6 commit af49483

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

packages/autocomplete-core/src/__tests__/getEnvironmentProps.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@ import { createPlayground } from '../../../../test/utils';
22
import { createAutocomplete } from '../createAutocomplete';
33

44
describe('getEnvironmentProps', () => {
5-
test.todo('forwards the remaining props');
5+
test('forwards the remaining props', () => {
6+
const { getEnvironmentProps, formElement, inputElement } = createPlayground(
7+
createAutocomplete,
8+
{}
9+
);
10+
const panelElement = document.createElement('div');
11+
12+
const environmentProps = getEnvironmentProps({
13+
inputElement,
14+
formElement,
15+
panelElement,
16+
customProps: {},
17+
});
18+
19+
expect(environmentProps).toEqual(
20+
expect.objectContaining({ customProps: {} })
21+
);
22+
});
623

724
describe('onTouchStart', () => {
825
test('is a noop when panel is not open', () => {

packages/autocomplete-core/src/getPropGetters.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export function getPropGetters<
2828
TMouseEvent,
2929
TKeyboardEvent
3030
>({ props, refresh, store, ...setters }: GetPropGettersOptions<TItem>) {
31-
const getEnvironmentProps: GetEnvironmentProps = (getterProps) => {
31+
const getEnvironmentProps: GetEnvironmentProps = (providedProps) => {
32+
const { inputElement, formElement, panelElement, ...rest } = providedProps;
33+
3234
return {
3335
// On touch devices, we do not rely on the native `blur` event of the
3436
// input to close the panel, but rather on a custom `touchstart` event
@@ -38,25 +40,24 @@ export function getPropGetters<
3840
onTouchStart(event) {
3941
if (
4042
store.getState().isOpen === false ||
41-
event.target === getterProps.inputElement
43+
event.target === inputElement
4244
) {
4345
return;
4446
}
4547

4648
// @TODO: support cases where there are multiple Autocomplete instances.
4749
// Right now, a second instance makes this computation return false.
48-
const isTargetWithinAutocomplete = [
49-
getterProps.formElement,
50-
getterProps.panelElement,
51-
].some((contextNode) => {
52-
return (
53-
isOrContainsNode(contextNode, event.target as Node) ||
54-
isOrContainsNode(
55-
contextNode,
56-
props.environment.document.activeElement!
57-
)
58-
);
59-
});
50+
const isTargetWithinAutocomplete = [formElement, panelElement].some(
51+
(contextNode) => {
52+
return (
53+
isOrContainsNode(contextNode, event.target as Node) ||
54+
isOrContainsNode(
55+
contextNode,
56+
props.environment.document.activeElement!
57+
)
58+
);
59+
}
60+
);
6061

6162
if (isTargetWithinAutocomplete === false) {
6263
store.dispatch('blur', null);
@@ -69,15 +70,15 @@ export function getPropGetters<
6970
onTouchMove(event: TouchEvent) {
7071
if (
7172
store.getState().isOpen === false ||
72-
getterProps.inputElement !==
73-
props.environment.document.activeElement ||
74-
event.target === getterProps.inputElement
73+
inputElement !== props.environment.document.activeElement ||
74+
event.target === inputElement
7575
) {
7676
return;
7777
}
7878

79-
getterProps.inputElement.blur();
79+
inputElement.blur();
8080
},
81+
...rest,
8182
};
8283
};
8384

0 commit comments

Comments
 (0)