Skip to content

Commit 8c8f77c

Browse files
author
Kubit
committed
Iprove useInput hook and it integration
1 parent 9b900f8 commit 8c8f77c

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/components/dropdownSelected/__tests__/dropdownSelected.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,63 @@ describe('DropdownSelected component', () => {
204204

205205
expect(option1).not.toBeInTheDocument();
206206
});
207+
208+
it('The container should open the dropdown on focus if openAndCloseOnHover', () => {
209+
const { getByTestId } = renderProvider(
210+
<DropdownSelected
211+
{...mockProps}
212+
openAndCloseOnHover
213+
dataTestIdComponent="container-test-id"
214+
/>
215+
);
216+
217+
const container = getByTestId('container-test-id');
218+
219+
act(() => {
220+
fireEvent.focus(container);
221+
});
222+
223+
const optionsAfterOpened = getByTestId(mockProps.dataTestIdListOptionsContainer);
224+
expect(optionsAfterOpened).toBeInTheDocument();
225+
});
226+
227+
it('The container should close the dropdown on blur if openAndCloseOnHover', () => {
228+
const { queryByTestId, getByTestId } = renderProvider(
229+
<DropdownSelected
230+
{...mockProps}
231+
openAndCloseOnHover
232+
dataTestIdComponent="container-test-id"
233+
defaultOpen={true}
234+
/>
235+
);
236+
237+
const container = getByTestId('container-test-id');
238+
239+
act(() => {
240+
fireEvent.blur(container);
241+
});
242+
243+
const optionsAfterOpened = queryByTestId(mockProps.dataTestIdListOptionsContainer);
244+
expect(optionsAfterOpened).not.toBeInTheDocument();
245+
});
246+
247+
it('The container should close the dropdown on escape keydown', () => {
248+
const { queryByTestId, getByTestId } = renderProvider(
249+
<DropdownSelected
250+
{...mockProps}
251+
openAndCloseOnHover
252+
dataTestIdComponent="container-test-id"
253+
defaultOpen={true}
254+
/>
255+
);
256+
257+
const container = getByTestId('container-test-id');
258+
259+
act(() => {
260+
fireEvent.keyDown(container, { key: 'Escape' });
261+
});
262+
263+
const optionsAfterOpened = queryByTestId(mockProps.dataTestIdListOptionsContainer);
264+
expect(optionsAfterOpened).not.toBeInTheDocument();
265+
});
207266
});

src/components/inputDropdown/hooks/useInputDropdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ export const useInputDropdown = (props: ParamsType): ReturnHookType => {
306306

307307
// Uses effects
308308

309-
// Update value when new prop value
309+
// Update value when value or optionList options changes
310310
useEffect(() => {
311311
setValueSearchSelected(props.value);
312312
const searchText = findOptionByValue(props.value, props.optionList.options)?.label ?? '';
313313
handleChangeShowAllOptions({ value: true });
314314
setLabelSearchSelected(searchText);
315315
setSearchText(searchText);
316316
setInputPopoverText(searchText);
317-
}, [props.value]);
317+
}, [props.value, props.optionList.options]);
318318

319319
const actionBottomSheetRefCb = useCallback(node => {
320320
// Focus in the input popover if exists

src/hooks/useInput/useInput.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
7171
const eventKeyPressRef = useRef<EventKeyPressRefType>();
7272
const positionRef = useRef(0);
7373
const inputRef = useRef<HTMLInputElement | undefined>();
74+
const pasteRef = useRef<boolean>(false);
7475

7576
// Return ref of the input element
7677
useImperativeHandle(
@@ -117,6 +118,11 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
117118
}, []);
118119

119120
useEffect(() => {
121+
// prevent the behaviour, when the value is changed by paste method
122+
if (pasteRef.current) {
123+
pasteRef.current = false;
124+
return;
125+
}
120126
if (eventKeyPressRef.current && props.mask && inputRef?.current && value) {
121127
const { start, end } = eventKeyPressRef.current.cursor;
122128
// if multiple digits are selected, recovery the selected area
@@ -357,6 +363,7 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
357363
event.preventDefault();
358364
return;
359365
}
366+
pasteRef.current = true;
360367
props.onPaste?.(event);
361368
};
362369

0 commit comments

Comments
 (0)