From e0aa9596ebec75b8d0a77bf8c76885c1c4bc65d7 Mon Sep 17 00:00:00 2001 From: AlekseyManetov Date: Wed, 20 Nov 2024 15:05:12 +0100 Subject: [PATCH] [PickerInput]: fixed unnecessary api calls on body open with `minCharsToSearch` prop and search in body. --- changelog.md | 7 ++++ .../src/pickers/hooks/usePickerInput.ts | 4 ++- .../pickers/__tests__/PickerInput.test.tsx | 35 +++++++++++++------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index a06210b3ea..d21a8f5fbb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,10 @@ +# 5.xx.xx - xx.xx.2024 + +**What's New** + +**What's Fixed** +* [PickerInput]: fixed unnecessary api calls on body open with `minCharsToSearch` prop and search in body. + # 5.11.0 - 15.11.2024 **What's New** diff --git a/uui-components/src/pickers/hooks/usePickerInput.ts b/uui-components/src/pickers/hooks/usePickerInput.ts index 876aa537ad..bc4a59c039 100644 --- a/uui-components/src/pickers/hooks/usePickerInput.ts +++ b/uui-components/src/pickers/hooks/usePickerInput.ts @@ -52,7 +52,9 @@ export function usePickerInput(props: UsePickerInputProps (props.shouldShowBody ?? defaultShouldShowBody)(); - const showSelectedOnly = !shouldShowBody() || pickerInputState.showSelected; + const shouldLoadList = () => isSearchLongEnough() && shouldShowBody(); + + const showSelectedOnly = !shouldLoadList() || pickerInputState.showSelected; const picker = usePicker>({ ...props, showSelectedOnly }, pickerInputState); const { diff --git a/uui/components/pickers/__tests__/PickerInput.test.tsx b/uui/components/pickers/__tests__/PickerInput.test.tsx index 1bdf1ff2f1..39bd35a4ad 100644 --- a/uui/components/pickers/__tests__/PickerInput.test.tsx +++ b/uui/components/pickers/__tests__/PickerInput.test.tsx @@ -1,5 +1,5 @@ import React, { ReactNode } from 'react'; -import { ArrayDataSource, CascadeSelection } from '@epam/uui-core'; +import { ArrayDataSource, CascadeSelection, LazyDataSource } from '@epam/uui-core'; import { renderSnapshotWithContextAsync, setupComponentForTest, screen, within, fireEvent, waitFor, userEvent, PickerInputTestObject, act, delayAct, @@ -1411,25 +1411,20 @@ describe('PickerInput', () => { console.error = prevConsoleError; }); - it('should render message in body for not enough chars in search while using minCharsToSearch', async () => { - const mockEmptyDS = new ArrayDataSource({ - items: [], + it('should not load items while search less than minCharsToSearch', async () => { + const apiMock = jest.fn().mockResolvedValue([]); + + const mockEmptyDS = new LazyDataSource({ + api: apiMock, getId: ({ id }) => id, }); - const customText = 'Custom Text or Component'; - const { dom } = await setupPickerInputForTest({ value: undefined, selectionMode: 'multi', minCharsToSearch: 3, searchPosition: 'body', dataSource: mockEmptyDS, - renderNotFound: () => ( - - {customText} - - ), getSearchFields: (item) => [item!.level], }); @@ -1443,6 +1438,24 @@ describe('PickerInput', () => { const notFound = within(await screen.findByRole('dialog')); expect(notFound.getByText('Type search to load items')).toBeInTheDocument(); }); + + expect(apiMock).toBeCalledTimes(0); + + const bodyInput = within(dialog).getByPlaceholderText('Search'); + + jest.useFakeTimers(); + fireEvent.change(bodyInput, { target: { value: '1234' } }); + act(() => { + jest.runAllTimers(); + }); + jest.useRealTimers(); + + await waitFor(async () => { + const notFound = within(await screen.findByRole('dialog')); + expect(notFound.getByText('No records found')).toBeInTheDocument(); + }); + + expect(apiMock).toBeCalledTimes(1); }); it('should render custom renderEmpty', async () => {