-
Notifications
You must be signed in to change notification settings - Fork 1
[DOP-21927] add search to ManagedSelect #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './useModalState'; | ||
| export * from './useDebouncedState'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| /** | ||
| * Hook for handling debounced state | ||
| * | ||
| * @param initialValue - initial state | ||
| * @param delay - state change timeout | ||
| */ | ||
| export function useDebouncedState<T>(initialValue: T, delay: number) { | ||
| const [value, setValue] = useState(initialValue); | ||
| const timeoutRef = useRef<number | null>(null); | ||
|
|
||
| const clearTimeout = () => window.clearTimeout(timeoutRef.current!); | ||
| useEffect(() => clearTimeout, []); | ||
|
|
||
| const setDebouncedValue = useCallback( | ||
| (newValue: SetStateAction<T>) => { | ||
| clearTimeout(); | ||
| timeoutRef.current = window.setTimeout(() => { | ||
| setValue(newValue); | ||
| }, delay); | ||
| }, | ||
| [delay], | ||
| ); | ||
|
|
||
| const setValueImmediately = (newValue: T) => { | ||
| clearTimeout(); | ||
| setValue(newValue); | ||
| }; | ||
|
|
||
| return { value, setValue: setValueImmediately, setDebouncedValue }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,75 @@ | ||
| import React, { UIEventHandler, useMemo } from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import { Select, Spin } from 'antd'; | ||
| import { useInfiniteRequest } from '@shared/config'; | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
|
|
||
| import { PAGE_DEFAULT, PAGE_SIZE_DEFAULT } from './constants'; | ||
| import { prepareOptionsForSelect } from './utils'; | ||
| import { ManagedSelectProps } from './types'; | ||
| import { useGetList, useGetSelectedItem, useHandleSelectEvents, usePrepareOptions, useSearch } from './hooks'; | ||
|
|
||
| /** Select component for infinite pagination of data in a dropdown */ | ||
| export const ManagedSelect = <T, V extends DefaultOptionType['value']>({ | ||
| queryFunction, | ||
| queryKey, | ||
| detailQueryFunction, | ||
| detailQueryKey, | ||
| renderOptionValue, | ||
| renderOptionLabel, | ||
| value, | ||
| onBlur, | ||
| onSelect, | ||
| onSearch, | ||
| ...props | ||
| }: ManagedSelectProps<T, V>) => { | ||
| const { data, fetchNextPage, hasNextPage, isLoading } = useInfiniteRequest<T>({ | ||
| const [hasTouched, setTouched] = useState(false); | ||
|
|
||
| const { searchValue, setSearchValue, handleSearch } = useSearch({ onSearch }); | ||
|
|
||
| const { data, hasNextPage, fetchNextPage, isLoading, isFetching } = useGetList({ | ||
| queryKey, | ||
| queryFn: ({ pageParam }) => queryFunction(pageParam), | ||
| initialPageParam: { page: PAGE_DEFAULT, page_size: PAGE_SIZE_DEFAULT }, | ||
| queryFunction, | ||
| hasTouched, | ||
| searchValue, | ||
| }); | ||
|
|
||
| const options = useMemo(() => { | ||
| return prepareOptionsForSelect({ | ||
| data: data?.items, | ||
| renderValue: renderOptionValue, | ||
| renderLabel: renderOptionLabel, | ||
| }); | ||
| }, [data, renderOptionValue, renderOptionLabel]); | ||
|
|
||
| const handlePopupScroll: UIEventHandler<HTMLDivElement> = (event) => { | ||
| const target = event.currentTarget; | ||
| if (hasNextPage && target.scrollTop + target.offsetHeight === target.scrollHeight) { | ||
| fetchNextPage(); | ||
| } | ||
| }; | ||
| const { data: selectedItem } = useGetSelectedItem({ | ||
| detailQueryKey, | ||
| detailQueryFunction, | ||
| // 'as' is needed to pass an existing initial value to the useGetSelectedItem. | ||
| // It will always be of type V, since useGetSelectedItem checks for this | ||
| value: value as V, | ||
| }); | ||
|
|
||
| const { handleSelect, handleBlur, handleOpenDropdown, handlePopupScroll } = useHandleSelectEvents({ | ||
| onBlur, | ||
| onSelect, | ||
| setTouched, | ||
| setSearchValue, | ||
| hasNextPage, | ||
| fetchNextPage, | ||
| }); | ||
|
|
||
| const options = usePrepareOptions({ | ||
| dataList: data, | ||
| searchValue, | ||
| selectedItem, | ||
| renderOptionValue, | ||
| renderOptionLabel, | ||
| }); | ||
|
|
||
| return ( | ||
| <Select | ||
| {...props} | ||
| options={options} | ||
| notFoundContent={isLoading ? <Spin size="small" /> : null} | ||
| // Do not transform value to option if there are not any options | ||
| value={options.length ? value : undefined} | ||
| showSearch | ||
| onDropdownVisibleChange={handleOpenDropdown} | ||
| onSelect={handleSelect} | ||
| onSearch={handleSearch} | ||
| filterOption={false} | ||
| // render notFoundContent when first request data is in progress | ||
| options={isLoading ? [] : options} | ||
| notFoundContent={isFetching ? <Spin size="small" /> : undefined} | ||
| onPopupScroll={handlePopupScroll} | ||
| onBlur={handleBlur} | ||
| {...props} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| export const PAGE_DEFAULT = 1; | ||
| export const PAGE_SIZE_DEFAULT = 50; | ||
| export const SEARCH_VALUE_DEFAULT = ''; | ||
| // Debounced time for changing search value in Select | ||
| export const SEARCH_VALUE_CHANGE_DELAY = 500; | ||
| // Minimum delay to show loader when requesting 1st page of options | ||
| export const REQUEST_FIRST_PAGE_DELAY = 300; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './useGetList'; | ||
| export * from './useGetSelectedItem'; | ||
| export * from './useSearch'; | ||
| export * from './useHandleSelectEvents'; | ||
| export * from './usePrepareOptions'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './useGetList'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { PaginationRequest, PaginationResponse } from '@shared/types'; | ||
| import { QueryKey } from '@tanstack/react-query'; | ||
|
|
||
| /** | ||
| * Interface as Props for hook "useGetList" | ||
| * | ||
| * @template T - Data object type for select options. | ||
| */ | ||
| export interface UseGetListProps<T> { | ||
| /** Select was in focus one time at least */ | ||
| hasTouched: boolean; | ||
| /** Query keys for requests cache of entity list data */ | ||
| queryKey: QueryKey; | ||
| /** Function for request entity list data */ | ||
| queryFunction: (params: PaginationRequest) => Promise<PaginationResponse<T>>; | ||
| /** Search input value in select */ | ||
| searchValue: string; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/shared/ui/ManagedSelect/hooks/useGetList/useGetList.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { useInfiniteRequest } from '@shared/config'; | ||
|
|
||
| import { PAGE_DEFAULT, PAGE_SIZE_DEFAULT, REQUEST_FIRST_PAGE_DELAY } from '../../constants'; | ||
|
|
||
| import { UseGetListProps } from './types'; | ||
|
|
||
| /** Hook for getting option list data for Select */ | ||
| export const useGetList = <T>({ queryKey, queryFunction, searchValue, hasTouched }: UseGetListProps<T>) => { | ||
| return useInfiniteRequest<T>({ | ||
| queryKey: [...queryKey, searchValue], | ||
| queryFn: async ({ pageParam }) => { | ||
| const response = await queryFunction({ ...pageParam, search_query: searchValue }); | ||
| // Wait minimum delay to show loader when requesting 1st page | ||
| if (pageParam.page === PAGE_DEFAULT) { | ||
| await new Promise((resolve) => setTimeout(resolve, REQUEST_FIRST_PAGE_DELAY)); | ||
| } | ||
| return response; | ||
| }, | ||
| initialPageParam: { page: PAGE_DEFAULT, page_size: PAGE_SIZE_DEFAULT }, | ||
| // Show first page of options when user touches select | ||
| enabled: hasTouched, | ||
| }); | ||
| }; |
2 changes: 2 additions & 0 deletions
2
src/shared/ui/ManagedSelect/hooks/useGetSelectedItem/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './useGetSelectedItem'; | ||
| export * from './types'; |
17 changes: 17 additions & 0 deletions
17
src/shared/ui/ManagedSelect/hooks/useGetSelectedItem/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { QueryKey } from '@tanstack/react-query'; | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
|
|
||
| /** | ||
| * Interface as Props for hook "useGetSelectedItem" | ||
| * | ||
| * @template T - Data object type for select options | ||
| * @template V - Value type for select options | ||
| */ | ||
| export interface UseGetSelectedItemProps<T, V extends DefaultOptionType['value']> { | ||
| /** Query keys for requests cache of entity's detail data */ | ||
| detailQueryKey: QueryKey; | ||
| /** Function for request detail entity data */ | ||
| detailQueryFunction: (value: V) => Promise<T>; | ||
| /** Value of Select */ | ||
| value: V; | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/shared/ui/ManagedSelect/hooks/useGetSelectedItem/useGetSelectedItem.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
|
|
||
| import { UseGetSelectedItemProps } from './types'; | ||
|
|
||
| /** Hook for getting selected option data for Select */ | ||
| export const useGetSelectedItem = <T, V extends DefaultOptionType['value']>({ | ||
| detailQueryKey, | ||
| detailQueryFunction, | ||
| value, | ||
| }: UseGetSelectedItemProps<T, V>) => { | ||
| return useQuery<T>({ | ||
| queryKey: [...detailQueryKey, value], | ||
| queryFn: () => detailQueryFunction(value), | ||
| enabled: !!value, | ||
| }); | ||
| }; |
2 changes: 2 additions & 0 deletions
2
src/shared/ui/ManagedSelect/hooks/useHandleSelectEvents/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './useHandleSelectEvents'; | ||
| export * from './types'; |
17 changes: 17 additions & 0 deletions
17
src/shared/ui/ManagedSelect/hooks/useHandleSelectEvents/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { SelectProps } from 'antd'; | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
|
|
||
| import { OptionItem } from '../../types'; | ||
|
|
||
| /** Interface as Props for hook "useHandleSelectEvents" */ | ||
| export interface UseHandleSelectEventsProps<T, V extends DefaultOptionType['value']> | ||
| extends Pick<SelectProps<V, OptionItem<T>>, 'onSelect' | 'onBlur'> { | ||
| /** Flag that shows if there is next page in infinite request of options in Select */ | ||
| hasNextPage: boolean; | ||
| /** Callback for changing requesting next page of options in Select */ | ||
| fetchNextPage: () => void; | ||
| /** Callback for changing search input value in Select */ | ||
| setSearchValue: (value: string) => void; | ||
| /** Callback for changing touched state for Select */ | ||
| setTouched: (value: boolean) => void; | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/shared/ui/ManagedSelect/hooks/useHandleSelectEvents/useHandleSelectEvents.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
| import { FocusEventHandler, UIEventHandler } from 'react'; | ||
| import { flushSync } from 'react-dom'; | ||
|
|
||
| import { OptionItem } from '../../types'; | ||
| import { SEARCH_VALUE_DEFAULT } from '../../constants'; | ||
|
|
||
| import { UseHandleSelectEventsProps } from './types'; | ||
|
|
||
| /** Hook for handling Select's component events */ | ||
| export const useHandleSelectEvents = <T, V extends DefaultOptionType['value']>({ | ||
| hasNextPage, | ||
| fetchNextPage, | ||
| setSearchValue, | ||
| setTouched, | ||
| onSelect = () => undefined, | ||
| onBlur = () => undefined, | ||
| }: UseHandleSelectEventsProps<T, V>) => { | ||
| const handleSelect = (newValue: V extends (infer A)[] ? A : V, option: OptionItem<T>) => { | ||
| setSearchValue(SEARCH_VALUE_DEFAULT); | ||
| onSelect(newValue, option); | ||
| }; | ||
|
|
||
| const handleBlur: FocusEventHandler<HTMLElement> = (event) => { | ||
| setSearchValue(SEARCH_VALUE_DEFAULT); | ||
| onBlur(event); | ||
| }; | ||
|
|
||
| const handlePopupScroll: UIEventHandler<HTMLDivElement> = (event) => { | ||
| const target = event.currentTarget; | ||
| if (hasNextPage && target.scrollTop + target.offsetHeight === target.scrollHeight) { | ||
jellySWATy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fetchNextPage(); | ||
| } | ||
| }; | ||
|
|
||
| const handleOpenDropdown = (open: boolean) => { | ||
| if (open) { | ||
| // Immediate change touched state to avoid show initial option in dropdown while first page options is loading | ||
| flushSync(() => { | ||
| setTouched(true); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| return { handleSelect, handleBlur, handlePopupScroll, handleOpenDropdown }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './usePrepareOptions'; | ||
| export * from './types'; |
16 changes: 16 additions & 0 deletions
16
src/shared/ui/ManagedSelect/hooks/usePrepareOptions/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { PaginationResponse } from '@shared/types'; | ||
| import { DefaultOptionType } from 'antd/lib/select'; | ||
|
|
||
| /** Interface as Props for hook "usePrepareOptions" */ | ||
| export interface UsePrepareOptionsProps<T, V extends DefaultOptionType['value']> { | ||
| /** Search input value in Select */ | ||
| searchValue: string; | ||
| /** Function render value for option from data object */ | ||
| renderOptionValue: (item: T) => V; | ||
| /** Function render label for option from data object */ | ||
| renderOptionLabel: (item: T) => DefaultOptionType['label']; | ||
| /** Response data from infinite request for options in Select */ | ||
| dataList?: PaginationResponse<T>; | ||
| /** Selected option in Select */ | ||
| selectedItem?: T; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.