Wrapper above react-select that supports pagination on menu scroll
Installations
npm install react-select-async-paginate
Developer Guide
Typescript
No
Module System
CommonJS, ESM
Node Version
20.18.0
NPM Version
10.8.2
Score
79.6
Supply Chain
88.5
Quality
83.2
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Languages
TypeScript (100%)
Developer
vtaits
Download Statistics
Total Downloads
16,781,969
Last Day
13,637
Last Week
103,977
Last Month
513,375
Last Year
5,923,205
GitHub Statistics
311 Stars
336 Commits
75 Forks
8 Watching
4 Branches
11 Contributors
Bundle Size
7.57 kB
Minified
2.81 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.7.7
Package Id
react-select-async-paginate@0.7.7
Unpacked Size
110.51 kB
Size
15.34 kB
File Count
8
NPM Version
10.8.2
Node Version
20.18.0
Publised On
06 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
16,781,969
Last day
-46%
13,637
Compared to previous day
Last week
-17.7%
103,977
Compared to previous week
Last month
2.8%
513,375
Compared to previous month
Last year
40.3%
5,923,205
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-select-async-paginate
Wrapper above react-select
that supports pagination on menu scroll.
Sandbox examples
- Simple
- Multi
- Creatable
- Creatable with adding new options
- Initial options
- Autoload
- Debounce
- Request by page number
- Customization check of the need of load options
- Grouped options
- Custom select base
- Manual control of input value and menu opening
- react-hook-form integration
Versions
react-select | react-select-async-paginate |
---|---|
5.x | 0.6.x, 0.7.x |
4.x | 0.5.x |
3.x | 0.5.x, 0.4.x, ^0.3.2 |
2.x | 0.3.x, 0.2.x |
1.x | 0.1.x |
Installation
npm install react-select react-select-async-paginate
or
yarn add react-select react-select-async-paginate
Usage
AsyncPaginate
is an alternative of Async
but supports loading page by page. It is wrapper above default react-select
thus it accepts all props of default Select
. And there are some new props:
loadOptions
Required. Async function that take next arguments:
- Current value of search input.
- Loaded options for current search.
- Collected additional data e.g. current page number etc. For first load it is
additional
from props, for next isadditional
from previous response for current search.null
by default.
It should return next object:
{
options: Array,
hasMore: boolean,
additional?: any,
}
It similar to loadOptions
from Select.Async
but there is some differences:
- Loaded options as 2nd argument.
- Additional data as 3nd argument.
- Not supports callback.
- Should return
hasMore
for detect end of options list for current search.
debounceTimeout
Not required. Number. Debounce timeout for loadOptions
calls. 0
by default.
additional
Not required. Default additional
for first request for every search.
defaultAdditional
Not required. Default additional
for empty search if options
or defaultOptions
defined.
shouldLoadMore
Not required. Function. By default new options will load only after scroll menu to bottom. Arguments:
- scrollHeight
- clientHeight
- scrollTop
Should return boolean.
reduceOptions
Not required. Function. By default new loaded options are concat with previous. Arguments:
- previous options
- loaded options
- next additional
Should return new options.
reloadOnErrorTimeout
Not required. Number. Time in milliseconds to retry a request after an error
cacheUniqs
Not required. Array. Works as 2nd argument of useEffect
hook. When one of items changed, AsyncPaginate
cleans all cached options.
loadOptionsOnMenuOpen
Not required. Boolean. If false
options will not load on menu opening.
selectRef
Ref for take react-select
instance.
Example
offset way
1import { AsyncPaginate } from 'react-select-async-paginate'; 2 3... 4 5/* 6 * assuming the API returns something like this: 7 * const json = { 8 * results: [ 9 * { 10 * value: 1, 11 * label: 'Audi', 12 * }, 13 * { 14 * value: 2, 15 * label: 'Mercedes', 16 * }, 17 * { 18 * value: 3, 19 * label: 'BMW', 20 * }, 21 * ], 22 * has_more: true, 23 * }; 24 */ 25 26async function loadOptions(search, loadedOptions) { 27 const response = await fetch(`/awesome-api-url/?search=${search}&offset=${loadedOptions.length}`); 28 const responseJSON = await response.json(); 29 30 return { 31 options: responseJSON.results, 32 hasMore: responseJSON.has_more, 33 }; 34} 35 36<AsyncPaginate 37 value={value} 38 loadOptions={loadOptions} 39 onChange={setValue} 40/>
page way
1import { AsyncPaginate } from 'react-select-async-paginate'; 2 3... 4 5async function loadOptions(search, loadedOptions, { page }) { 6 const response = await fetch(`/awesome-api-url/?search=${search}&page=${page}`); 7 const responseJSON = await response.json(); 8 9 return { 10 options: responseJSON.results, 11 hasMore: responseJSON.has_more, 12 additional: { 13 page: page + 1, 14 }, 15 }; 16} 17 18<AsyncPaginate 19 value={value} 20 loadOptions={loadOptions} 21 onChange={setValue} 22 additional={{ 23 page: 1, 24 }} 25/>
Grouped options
You can use reduceGroupedOptions
util to group options by label
key.
1import { AsyncPaginate, reduceGroupedOptions } from 'react-select-async-paginate'; 2 3/* 4 * assuming the API returns something like this: 5 * const json = { 6 * options: [ 7 * label: 'Cars', 8 * options: [ 9 * { 10 * value: 1, 11 * label: 'Audi', 12 * }, 13 * { 14 * value: 2, 15 * label: 'Mercedes', 16 * }, 17 * { 18 * value: 3, 19 * label: 'BMW', 20 * }, 21 * ] 22 * ], 23 * hasMore: true, 24 * }; 25 */ 26 27... 28 29<AsyncPaginate 30 {...otherProps} 31 reduceOptions={reduceGroupedOptions} 32/>
Replacing react-select component
You can use withAsyncPaginate
HOC.
1import { withAsyncPaginate } from 'react-select-async-paginate'; 2 3... 4 5const CustomAsyncPaginate = withAsyncPaginate(CustomSelect);
typescript
Describing type of component with extra props (example with Creatable
):
1import type { ReactElement } from 'react'; 2import type { GroupBase } from 'react-select'; 3import Creatable from 'react-select/creatable'; 4import type { CreatableProps } from 'react-select/creatable'; 5 6import { withAsyncPaginate } from 'react-select-async-paginate'; 7import type { 8 UseAsyncPaginateParams, 9 ComponentProps, 10} from 'react-select-async-paginate'; 11 12type AsyncPaginateCreatableProps< 13OptionType, 14Group extends GroupBase<OptionType>, 15Additional, 16IsMulti extends boolean, 17> = 18 & CreatableProps<OptionType, IsMulti, Group> 19 & UseAsyncPaginateParams<OptionType, Group, Additional> 20 & ComponentProps<OptionType, Group, IsMulti>; 21 22type AsyncPaginateCreatableType = < 23OptionType, 24Group extends GroupBase<OptionType>, 25Additional, 26IsMulti extends boolean = false, 27>(props: AsyncPaginateCreatableProps<OptionType, Group, Additional, IsMulti>) => ReactElement; 28 29const AsyncPaginateCreatable = withAsyncPaginate(Creatable) as AsyncPaginateCreatableType;
Replacing Components
Usage of replacing components is similar with react-select
, but there is one difference. If you redefine MenuList
you should wrap it with wrapMenuList
for workaround of some internal bugs of react-select
.
1import { AsyncPaginate, wrapMenuList } from 'react-select-async-paginate'; 2 3... 4 5const MenuList = wrapMenuList(CustomMenuList); 6 7<AsyncPaginate 8 {...otherProps} 9 components={{ 10 ...otherComponents, 11 MenuList, 12 }} 13/>
Extended usage
If you want construct own component that uses logic of react-select-async-paginate
inside, you can use next hooks:
useAsyncPaginate
useAsyncPaginateBase
useComponents
1import { 2 useAsyncPaginate, 3 useComponents, 4} from 'react-select-async-paginate'; 5 6... 7 8const CustomAsyncPaginateComponent = ({ 9 options, 10 defaultOptions, 11 additional, 12 loadOptionsOnMenuOpen, 13 debounceTimeout, 14 filterOption, 15 reduceOptions, 16 shouldLoadMore, 17 18 components: defaultComponents, 19 20 value, 21 onChange, 22}) => { 23 const asyncPaginateProps = useAsyncPaginate({ 24 options, 25 defaultOptions, 26 additional, 27 loadOptionsOnMenuOpen, 28 debounceTimeout, 29 filterOption, 30 reduceOptions, 31 shouldLoadMore, 32 }); 33 34 const components = useComponents(defaultComponents); 35 36 return ( 37 <CustomSelect 38 {...asyncPaginateProps} 39 components={components} 40 value={value} 41 onChange={onChange} 42 /> 43 ); 44}
useComponents
provides redefined MenuList
component by default. If you want redefine it, you should also wrap in with wrapMenuList
.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
4 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 3/18 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 15 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-select-async-paginate
react-select-fetch
Wrapper above react-select-async-paginate that loads options from specified url
@turtletongue/react-select-async-paginate
Wrapper above react-select that supports pagination on menu scroll
react-select-async-paginate-sf2
Wrapper above react-select that supports pagination on menu scroll
react-select-async-paginate-self
Wrapper above react-select that supports pagination on menu scroll