37 |
38 | ## Features
39 | * Lightweight, with zero dependencies
40 | * Accessible
41 | * Headless mode
42 | * Basic HTML select functionality, including multiple
43 | * Search/filter options
44 | * Async options
45 | * Apply renderers to change markup and behavior
46 | * Keyboard support
47 | * Group options with group names, you can search group names
48 | * Fully stylable
49 |
50 | ## Install
51 |
52 | Install it with npm (`npm i react-select-search`) or yarn (`yarn add react-select-search`) and import it like you normally would.
53 |
54 | ## Quick start
55 |
56 | ```jsx harmony
57 | import SelectSearch from 'react-select-search';
58 |
59 | /**
60 | * The options array should contain objects.
61 | * Required keys are "name" and "value" but you can have and use any number of key/value pairs.
62 | */
63 | const options = [
64 | {name: 'Swedish', value: 'sv'},
65 | {name: 'English', value: 'en'},
66 | {
67 | type: 'group',
68 | name: 'Group name',
69 | items: [
70 | {name: 'Spanish', value: 'es'},
71 | ]
72 | },
73 | ];
74 |
75 | /* Simple example */
76 |
77 | ```
78 | For more examples, you can take a look in the [stories](stories) directory.
79 |
80 | You will also need some CSS to make it look right. Example theme can be found in [style.css](style.css). You can also import it:
81 |
82 | ```javascript
83 | import 'react-select-search/style.css'
84 | ```
85 |
86 | ## Use with SSR
87 |
88 | For use with SSR you might need to use the commonjs bundle (react-select-search/dist/cjs). If you want to utilise the example theme ([style.css](style.css)) you need to check if your build script manipulates class names, for example minifies them. If that's the case, you can use CSS modules to get the class names from the style.css file and apply them using the [className object](#custom-class-names). Example can be seen [here](stories/3-Custom.stories.js#L64) as well as here https://react-select-search.com/?path=/story/custom--css-modules.
89 |
90 | ## Headless mode with hooks
91 |
92 | If you want complete control (more than styling and [custom renderers](#custom-renderers)) you can use hooks to pass data to your own components and build it yourself.
93 |
94 | ```jsx harmony
95 | import React from 'react';
96 | import { useSelect } from 'react-select-search';
97 |
98 | const CustomSelect = ({ options, value, multiple, disabled }) => {
99 | const [snapshot, valueProps, optionProps] = useSelect({
100 | options,
101 | value,
102 | multiple,
103 | disabled,
104 | });
105 |
106 | return (
107 |
108 |
109 | {snapshot.focus && (
110 |
111 | {snapshot.options.map((option) => (
112 |
113 |
114 |
115 | ))}
116 |
117 | )}
118 |
119 | );
120 | };
121 | ```
122 |
123 | ## Configuration
124 |
125 | Below is all the available options you can pass to the component. Options without defaults are required.
126 |
127 | | Name | Type | Default | Description |
128 | | ---- |----------------| ------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
129 | | options | array | | See the [options documentation](#the-options-object) below |
130 | | getOptions | function | null | Get options through a function call, can return a promise for async usage. See [get options](#get-options) for more. |
131 | | filterOptions | array | null | An array of functions that takes the last filtered options and a search query if any. Runs after getOptions. |
132 | | value | string, array | null | The value should be an array if multiple mode. |
133 | | multiple | boolean | false | Set to true if you want to allow multiple selected options. |
134 | | search | boolean | false | Set to true to enable search functionality |
135 | | disabled | boolean | false | Disables all functionality |
136 | | closeOnSelect | boolean | true | The selectbox will blur by default when selecting an option. Set this to false to prevent this behavior. |
137 | | debounce | number | 0 | Number of ms to wait until calling [get options](#get-options) when searching. |
138 | | placeholder | string | empty string | Displayed if no option is selected and/or when search field is focused with empty value. |
139 | | id | string | null | HTML ID on the top level element. |
140 | | autoComplete | string, on/off | off | Disables/Enables autoComplete functionality in search field. |
141 | | autoFocus | boolean | false | Autofocus on select |
142 | | className | string, object | select-search-box | Set a base class string or pass a function for complete control. Se [custom classNames](#custom-class-names) for more. |
143 | | renderOption | function | null | Function that renders the options. See [custom renderers](#custom-renderers) for more. |
144 | | renderGroupHeader | function | null | Function that renders the group header. See [custom renderers](#custom-renderers) for more. |
145 | | renderValue | function | null | Function that renders the value/search field. See [custom renderers](#custom-renderers) for more. |
146 | | emptyMessage | React node | null | Set empty message for empty options list, you can provide render function without arguments instead plain string message |
147 | | onChange | function | null | Function to receive and handle value changes. |
148 | | onFocus | function | null | Focus callback. |
149 | | onBlur | function | null | Blur callback. |
150 |
151 | ## The options object
152 |
153 | The options object can contain any properties and values you like. The only required one is `name`.
154 |
155 | | Property | Type | Description | Required |
156 | | -------- | ---- | ----------- | -------- |
157 | | name | string | The name of the option | Yes |
158 | | value | string | The value of the option | Yes, if the type is not "group" |
159 | | type | string | If you set the type to "group" you can add an array of options that will be grouped | No |
160 | | items | array | Array of option objects that will be used if the type is set to "group" | Yes, if `type` is set to "group" |
161 | | disabled | boolean | Set to `true` to disable this option | No |
162 |
163 | ## Custom class names
164 |
165 | If you set a string as the `className` attribute value, the component will use that as a base for all elements.
166 | If you want to fully control the class names you can pass an object with classnames. The following keys exists:
167 |
168 | * container
169 | * value
170 | * input
171 | * select
172 | * options
173 | * row
174 | * option
175 | * group
176 | * group-header
177 | * is-selected
178 | * is-highlighted
179 | * is-loading
180 | * is-multiple
181 | * has-focus
182 |
183 | ## Custom renderers
184 |
185 | If CSS isn't enough, you can also control the HTML for the different parts of the component.
186 |
187 | | Callback | Args | Description |
188 | | -------- |-------------------------------------------------------------------------------------| ----------- |
189 | | renderOption | optionsProps: object, optionData: object, optionSnapshot: object, className: string | Controls the rendering of the options. |
190 | | renderGroupHeader | name: string | Controls the rendering of the group header name |
191 | | renderValue | valueProps: object, snapshot: object, className: string | Controls the rendering of the value/input element |
192 |
193 | The optionProps and the valueProps are needed for the component you render to work. For example:
194 |
195 | ```jsx
196 | } />
197 | ```
198 |
199 | Monkeypatch it if you need to but make sure to not remove important props.
200 |
201 | The optionSnapshot is an object that contains the object state: `{ selected: bool, highlighted: bool }`.
202 |
203 | ## Get options
204 |
205 | You can fetch options asynchronously with the `getOptions` property. You can either return options directly or through a `Promise`.
206 |
207 | ```jsx
208 | function getOptions(query) {
209 | return new Promise((resolve, reject) => {
210 | fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${query}`)
211 | .then(response => response.json())
212 | .then(({ drinks }) => {
213 | resolve(drinks.map(({ idDrink, strDrink }) => ({ value: idDrink, name: strDrink })))
214 | })
215 | .catch(reject);
216 | });
217 | }
218 | ```
219 |
220 | The function runs on each search query update, so you might want to throttle the fetches.
221 | If you return a promise, the class `is-loading` will be applied to the main element, giving you a chance
222 | to change the appearance, like adding a spinner. The property `fetching` is also available in the snapshot that is sent to your render callbacks.
223 |
224 | ## Contributors
225 |
226 |
227 |
228 |
229 |
230 | Made with [contrib.rocks](https://contrib.rocks).
231 |
--------------------------------------------------------------------------------
/__tests__/data/index.js:
--------------------------------------------------------------------------------
1 | export const countries = [
2 | {name: 'Afghanistan', value: 'AF'},
3 | {name: 'Åland Islands', value: 'AX'},
4 | {name: 'Albania', value: 'AL'},
5 | {name: 'Algeria', value: 'DZ'},
6 | {name: 'American Samoa', value: 'AS'},
7 | {name: 'AndorrA', value: 'AD'},
8 | {name: 'Angola', value: 'AO'},
9 | {name: 'Anguilla', value: 'AI'},
10 | {name: 'Antarctica', value: 'AQ'},
11 | {name: 'Antigua and Barbuda', value: 'AG'},
12 | {name: 'Argentina', value: 'AR'},
13 | {name: 'Armenia', value: 'AM'},
14 | {name: 'Aruba', value: 'AW'},
15 | {name: 'Australia', value: 'AU'},
16 | {name: 'Austria', value: 'AT'},
17 | {name: 'Azerbaijan', value: 'AZ'},
18 | {name: 'Bahamas', value: 'BS'},
19 | {name: 'Bahrain', value: 'BH'},
20 | {name: 'Bangladesh', value: 'BD'},
21 | {name: 'Barbados', value: 'BB'},
22 | {name: 'Belarus', value: 'BY'},
23 | {name: 'Belgium', value: 'BE'},
24 | {name: 'Belize', value: 'BZ'},
25 | {name: 'Benin', value: 'BJ'},
26 | {name: 'Bermuda', value: 'BM'},
27 | {name: 'Bhutan', value: 'BT'},
28 | {name: 'Bolivia', value: 'BO'},
29 | {name: 'Bosnia and Herzegovina', value: 'BA'},
30 | {name: 'Botswana', value: 'BW'},
31 | {name: 'Bouvet Island', value: 'BV'},
32 | {name: 'Brazil', value: 'BR'},
33 | {name: 'British Indian Ocean Territory', value: 'IO'},
34 | {name: 'Brunei Darussalam', value: 'BN'},
35 | {name: 'Bulgaria', value: 'BG'},
36 | {name: 'Burkina Faso', value: 'BF'},
37 | {name: 'Burundi', value: 'BI'},
38 | {name: 'Cambodia', value: 'KH'},
39 | {name: 'Cameroon', value: 'CM'},
40 | {name: 'Canada', value: 'CA'},
41 | {name: 'Cape Verde', value: 'CV'},
42 | {name: 'Cayman Islands', value: 'KY'},
43 | {name: 'Central African Republic', value: 'CF'},
44 | {name: 'Chad', value: 'TD'},
45 | {name: 'Chile', value: 'CL'},
46 | {name: 'China', value: 'CN'},
47 | {name: 'Christmas Island', value: 'CX'},
48 | {name: 'Cocos (Keeling) Islands', value: 'CC'},
49 | {name: 'Colombia', value: 'CO'},
50 | {name: 'Comoros', value: 'KM'},
51 | {name: 'Congo', value: 'CG'},
52 | {name: 'Congo, The Democratic Republic of the', value: 'CD'},
53 | {name: 'Cook Islands', value: 'CK'},
54 | {name: 'Costa Rica', value: 'CR'},
55 | {name: 'Cote D\'Ivoire', value: 'CI'},
56 | {name: 'Croatia', value: 'HR'},
57 | {name: 'Cuba', value: 'CU'},
58 | {name: 'Cyprus', value: 'CY'},
59 | {name: 'Czech Republic', value: 'CZ'},
60 | {name: 'Denmark', value: 'DK'},
61 | {name: 'Djibouti', value: 'DJ'},
62 | {name: 'Dominica', value: 'DM'},
63 | {name: 'Dominican Republic', value: 'DO'},
64 | {name: 'Ecuador', value: 'EC'},
65 | {name: 'Egypt', value: 'EG'},
66 | {name: 'El Salvador', value: 'SV'},
67 | {name: 'Equatorial Guinea', value: 'GQ'},
68 | {name: 'Eritrea', value: 'ER'},
69 | {name: 'Estonia', value: 'EE'},
70 | {name: 'Ethiopia', value: 'ET'},
71 | {name: 'Falkland Islands (Malvinas)', value: 'FK'},
72 | {name: 'Faroe Islands', value: 'FO'},
73 | {name: 'Fiji', value: 'FJ'},
74 | {name: 'Finland', value: 'FI'},
75 | {name: 'France', value: 'FR'},
76 | {name: 'French Guiana', value: 'GF'},
77 | {name: 'French Polynesia', value: 'PF'},
78 | {name: 'French Southern Territories', value: 'TF'},
79 | {name: 'Gabon', value: 'GA'},
80 | {name: 'Gambia', value: 'GM'},
81 | {name: 'Georgia', value: 'GE'},
82 | {name: 'Germany', value: 'DE'},
83 | {name: 'Ghana', value: 'GH'},
84 | {name: 'Gibraltar', value: 'GI'},
85 | {name: 'Greece', value: 'GR'},
86 | {name: 'Greenland', value: 'GL'},
87 | {name: 'Grenada', value: 'GD'},
88 | {name: 'Guadeloupe', value: 'GP'},
89 | {name: 'Guam', value: 'GU'},
90 | {name: 'Guatemala', value: 'GT'},
91 | {name: 'Guernsey', value: 'GG'},
92 | {name: 'Guinea', value: 'GN'},
93 | {name: 'Guinea-Bissau', value: 'GW'},
94 | {name: 'Guyana', value: 'GY'},
95 | {name: 'Haiti', value: 'HT'},
96 | {name: 'Heard Island and Mcdonald Islands', value: 'HM'},
97 | {name: 'Holy See (Vatican City State)', value: 'VA'},
98 | {name: 'Honduras', value: 'HN'},
99 | {name: 'Hong Kong', value: 'HK'},
100 | {name: 'Hungary', value: 'HU'},
101 | {name: 'Iceland', value: 'IS'},
102 | {name: 'India', value: 'IN'},
103 | {name: 'Indonesia', value: 'ID'},
104 | {name: 'Iran, Islamic Republic Of', value: 'IR'},
105 | {name: 'Iraq', value: 'IQ'},
106 | {name: 'Ireland', value: 'IE'},
107 | {name: 'Isle of Man', value: 'IM'},
108 | {name: 'Israel', value: 'IL'},
109 | {name: 'Italy', value: 'IT'},
110 | {name: 'Jamaica', value: 'JM'},
111 | {name: 'Japan', value: 'JP'},
112 | {name: 'Jersey', value: 'JE'},
113 | {name: 'Jordan', value: 'JO'},
114 | {name: 'Kazakhstan', value: 'KZ'},
115 | {name: 'Kenya', value: 'KE'},
116 | {name: 'Kiribati', value: 'KI'},
117 | {name: 'Korea, Democratic People\'S Republic of', value: 'KP'},
118 | {name: 'Korea, Republic of', value: 'KR'},
119 | {name: 'Kuwait', value: 'KW'},
120 | {name: 'Kyrgyzstan', value: 'KG'},
121 | {name: 'Lao People\'S Democratic Republic', value: 'LA'},
122 | {name: 'Latvia', value: 'LV'},
123 | {name: 'Lebanon', value: 'LB'},
124 | {name: 'Lesotho', value: 'LS'},
125 | {name: 'Liberia', value: 'LR'},
126 | {name: 'Libyan Arab Jamahiriya', value: 'LY'},
127 | {name: 'Liechtenstein', value: 'LI'},
128 | {name: 'Lithuania', value: 'LT'},
129 | {name: 'Luxembourg', value: 'LU'},
130 | {name: 'Macao', value: 'MO'},
131 | {name: 'Macedonia, The Former Yugoslav Republic of', value: 'MK'},
132 | {name: 'Madagascar', value: 'MG'},
133 | {name: 'Malawi', value: 'MW'},
134 | {name: 'Malaysia', value: 'MY'},
135 | {name: 'Maldives', value: 'MV'},
136 | {name: 'Mali', value: 'ML'},
137 | {name: 'Malta', value: 'MT'},
138 | {name: 'Marshall Islands', value: 'MH'},
139 | {name: 'Martinique', value: 'MQ'},
140 | {name: 'Mauritania', value: 'MR'},
141 | {name: 'Mauritius', value: 'MU'},
142 | {name: 'Mayotte', value: 'YT'},
143 | {name: 'Mexico', value: 'MX'},
144 | {name: 'Micronesia, Federated States of', value: 'FM'},
145 | {name: 'Moldova, Republic of', value: 'MD'},
146 | {name: 'Monaco', value: 'MC'},
147 | {name: 'Mongolia', value: 'MN'},
148 | {name: 'Montserrat', value: 'MS'},
149 | {name: 'Morocco', value: 'MA'},
150 | {name: 'Mozambique', value: 'MZ'},
151 | {name: 'Myanmar', value: 'MM'},
152 | {name: 'Namibia', value: 'NA'},
153 | {name: 'Nauru', value: 'NR'},
154 | {name: 'Nepal', value: 'NP'},
155 | {name: 'Netherlands', value: 'NL'},
156 | {name: 'Netherlands Antilles', value: 'AN'},
157 | {name: 'New Caledonia', value: 'NC'},
158 | {name: 'New Zealand', value: 'NZ'},
159 | {name: 'Nicaragua', value: 'NI'},
160 | {name: 'Niger', value: 'NE'},
161 | {name: 'Nigeria', value: 'NG'},
162 | {name: 'Niue', value: 'NU'},
163 | {name: 'Norfolk Island', value: 'NF'},
164 | {name: 'Northern Mariana Islands', value: 'MP'},
165 | {name: 'Norway', value: 'NO'},
166 | {name: 'Oman', value: 'OM'},
167 | {name: 'Pakistan', value: 'PK'},
168 | {name: 'Palau', value: 'PW'},
169 | {name: 'Palestinian Territory, Occupied', value: 'PS'},
170 | {name: 'Panama', value: 'PA'},
171 | {name: 'Papua New Guinea', value: 'PG'},
172 | {name: 'Paraguay', value: 'PY'},
173 | {name: 'Peru', value: 'PE'},
174 | {name: 'Philippines', value: 'PH'},
175 | {name: 'Pitcairn', value: 'PN'},
176 | {name: 'Poland', value: 'PL'},
177 | {name: 'Portugal', value: 'PT'},
178 | {name: 'Puerto Rico', value: 'PR'},
179 | {name: 'Qatar', value: 'QA'},
180 | {name: 'Reunion', value: 'RE'},
181 | {name: 'Romania', value: 'RO'},
182 | {name: 'Russian Federation', value: 'RU'},
183 | {name: 'RWANDA', value: 'RW'},
184 | {name: 'Saint Helena', value: 'SH'},
185 | {name: 'Saint Kitts and Nevis', value: 'KN'},
186 | {name: 'Saint Lucia', value: 'LC'},
187 | {name: 'Saint Pierre and Miquelon', value: 'PM'},
188 | {name: 'Saint Vincent and the Grenadines', value: 'VC'},
189 | {name: 'Samoa', value: 'WS'},
190 | {name: 'San Marino', value: 'SM'},
191 | {name: 'Sao Tome and Principe', value: 'ST'},
192 | {name: 'Saudi Arabia', value: 'SA'},
193 | {name: 'Senegal', value: 'SN'},
194 | {name: 'Serbia and Montenegro', value: 'CS'},
195 | {name: 'Seychelles', value: 'SC'},
196 | {name: 'Sierra Leone', value: 'SL'},
197 | {name: 'Singapore', value: 'SG'},
198 | {name: 'Slovakia', value: 'SK'},
199 | {name: 'Slovenia', value: 'SI'},
200 | {name: 'Solomon Islands', value: 'SB'},
201 | {name: 'Somalia', value: 'SO'},
202 | {name: 'South Africa', value: 'ZA'},
203 | {name: 'South Georgia and the South Sandwich Islands', value: 'GS'},
204 | {name: 'Spain', value: 'ES'},
205 | {name: 'Sri Lanka', value: 'LK'},
206 | {name: 'Sudan', value: 'SD'},
207 | {name: 'Suriname', value: 'SR'},
208 | {name: 'Svalbard and Jan Mayen', value: 'SJ'},
209 | {name: 'Swaziland', value: 'SZ'},
210 | {name: 'Sweden', value: 'SE'},
211 | {name: 'Switzerland', value: 'CH'},
212 | {name: 'Syrian Arab Republic', value: 'SY'},
213 | {name: 'Taiwan, Province of China', value: 'TW'},
214 | {name: 'Tajikistan', value: 'TJ'},
215 | {name: 'Tanzania, United Republic of', value: 'TZ'},
216 | {name: 'Thailand', value: 'TH'},
217 | {name: 'Timor-Leste', value: 'TL'},
218 | {name: 'Togo', value: 'TG'},
219 | {name: 'Tokelau', value: 'TK'},
220 | {name: 'Tonga', value: 'TO'},
221 | {name: 'Trinidad and Tobago', value: 'TT'},
222 | {name: 'Tunisia', value: 'TN'},
223 | {name: 'Turkey', value: 'TR'},
224 | {name: 'Turkmenistan', value: 'TM'},
225 | {name: 'Turks and Caicos Islands', value: 'TC'},
226 | {name: 'Tuvalu', value: 'TV'},
227 | {name: 'Uganda', value: 'UG'},
228 | {name: 'Ukraine', value: 'UA'},
229 | {name: 'United Arab Emirates', value: 'AE'},
230 | {name: 'United Kingdom', value: 'GB'},
231 | {name: 'United States', value: 'US'},
232 | {name: 'United States Minor Outlying Islands', value: 'UM'},
233 | {name: 'Uruguay', value: 'UY'},
234 | {name: 'Uzbekistan', value: 'UZ'},
235 | {name: 'Vanuatu', value: 'VU'},
236 | {name: 'Venezuela', value: 'VE'},
237 | {name: 'Viet Nam', value: 'VN'},
238 | {name: 'Virgin Islands, British', value: 'VG'},
239 | {name: 'Virgin Islands, U.S.', value: 'VI'},
240 | {name: 'Wallis and Futuna', value: 'WF'},
241 | {name: 'Western Sahara', value: 'EH'},
242 | {name: 'Yemen', value: 'YE'},
243 | {name: 'Zambia', value: 'ZM'},
244 | {name: 'Zimbabwe', value: 'ZW'}
245 | ];
246 |
247 | export const fontStacks = [
248 | {
249 | name: 'Helvetica',
250 | value: 'helvetica',
251 | },
252 | {
253 | type: 'group',
254 | name: 'Sans serif',
255 | items: [
256 | { name: 'Roboto', value: 'Roboto', 'data-stack': 'Roboto, sans-serif' }
257 | ]
258 | },
259 | {
260 | type: 'group',
261 | name: 'Serif',
262 | items: [
263 | { name: 'Playfair Display', value: 'Playfair Display', 'data-stack': '"Playfair Display", serif' }
264 | ]
265 | },
266 | {
267 | type: 'group',
268 | name: 'Cursive',
269 | items: [
270 | { name: 'Monoton', value: 'Monoton', 'data-stack': 'Monoton, cursive' },
271 | { name: 'Gloria Hallelujah', value: 'Gloria Hallelujah', 'data-stack': '"Gloria Hallelujah", cursive' }
272 | ]
273 | },
274 | {
275 | type: 'group',
276 | name: 'Monospace',
277 | items: [
278 | { name: 'VT323', value: 'VT323', 'data-stack': 'VT323, monospace' }
279 | ]
280 | }
281 | ];
282 |
283 | export const friends = [
284 | { name: 'Annie Cruz', value: 'annie.cruz', photo: 'https://randomuser.me/api/portraits/women/60.jpg' },
285 | { name: 'Eli Shelton', disabled: true, value: 'eli.shelton', photo: 'https://randomuser.me/api/portraits/men/7.jpg' },
286 | { name: 'Loretta Rogers', value: 'loretta.rogers', photo: 'https://randomuser.me/api/portraits/women/51.jpg' },
287 | { name: 'Lloyd Fisher', value: 'lloyd.fisher', photo: 'https://randomuser.me/api/portraits/men/34.jpg' },
288 | { name: 'Tiffany Gonzales', value: 'tiffany.gonzales', photo: 'https://randomuser.me/api/portraits/women/71.jpg' },
289 | { name: 'Charles Hardy', value: 'charles.hardy', photo: 'https://randomuser.me/api/portraits/men/12.jpg' },
290 | { name: 'Rudolf Wilson', value: 'rudolf.wilson', photo: 'https://randomuser.me/api/portraits/men/40.jpg' },
291 | { name: 'Emerald Hensley', value: 'emerald.hensley', photo: 'https://randomuser.me/api/portraits/women/1.jpg' },
292 | { name: 'Lorena McCoy', value: 'lorena.mccoy', photo: 'https://randomuser.me/api/portraits/women/70.jpg' },
293 | { name: 'Alicia Lamb', value: 'alicia.lamb', photo: 'https://randomuser.me/api/portraits/women/22.jpg' },
294 | { name: 'Maria Waters', value: 'maria.waters', photo: 'https://randomuser.me/api/portraits/women/82.jpg' },
295 | ];
296 |
--------------------------------------------------------------------------------
/__tests__/flattenOptions.test.js:
--------------------------------------------------------------------------------
1 | import FlattenOptions from '../src/lib/flattenOptions';
2 |
3 | describe('Unit test for FlattenOptions function', () => {
4 | const groupedOptions = [
5 | {
6 | "type": "group",
7 | "name": "Cursive",
8 | "items": [
9 | {
10 | "name": "Monoton",
11 | "value": "Monoton",
12 | },
13 | ]
14 | },
15 | {
16 | "name": "Gloria Hallelujah",
17 | "value": "Gloria Hallelujah",
18 | },
19 | ];
20 |
21 | const flattenOptions = FlattenOptions(groupedOptions);
22 |
23 | test('Has correct items', () => {
24 | expect(flattenOptions).toHaveLength(2);
25 | });
26 |
27 | test('First item should be a group', () => {
28 | expect('group' in flattenOptions[0]).toEqual(true);
29 | });
30 |
31 | test('Second item should not be a group', () => {
32 | expect('group' in flattenOptions[1]).toEqual(false);
33 | });
34 | });
35 |
--------------------------------------------------------------------------------
/__tests__/getDisplayValue.test.js:
--------------------------------------------------------------------------------
1 | import getDisplayValue from '../src/lib/getDisplayValue';
2 | import { friends } from './data';
3 |
4 | describe('Unit test for getDisplayValue function', () => {
5 | test('Can get name from option by value', () => {
6 | const option = friends[Math.floor(Math.random() * friends.length)];
7 | const secondOption =
8 | friends[Math.floor(Math.random() * friends.length)];
9 |
10 | expect(getDisplayValue(option)).toEqual(option.name);
11 | expect(getDisplayValue([option, secondOption])).toStrictEqual(
12 | `${option.name}, ${secondOption.name}`,
13 | );
14 | expect(getDisplayValue('foo')).toEqual('');
15 | expect(getDisplayValue([{ value: 'fake-option' }])).toEqual('');
16 | });
17 |
18 | test('Can return default values', () => {
19 | expect(getDisplayValue(null, friends)).toEqual(friends[0].name);
20 | expect(getDisplayValue(null, [{ value: 'fake-option' }])).toEqual('');
21 | expect(getDisplayValue(null, [])).toEqual('');
22 | expect(getDisplayValue(null, friends, 'Placeholder')).toEqual('');
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/__tests__/getOption.test.js:
--------------------------------------------------------------------------------
1 | import getOption from '../src/lib/getOption';
2 | import { friends } from './data';
3 |
4 | describe('Unit test for getOption function', () => {
5 | test('Can get option by value', () => {
6 | const optionToFind =
7 | friends[Math.floor(Math.random() * friends.length)];
8 |
9 | expect(getOption(optionToFind.value, friends)).toStrictEqual(
10 | optionToFind,
11 | );
12 | expect(getOption('foo', friends)).toEqual(null);
13 | });
14 |
15 | test('Can get option by value (multiple)', () => {
16 | const option1 = friends[0];
17 | const option2 = friends[1];
18 |
19 | expect(
20 | getOption([option1.value, option2.value], friends),
21 | ).toStrictEqual([option1, option2]);
22 | });
23 |
24 | test('Return null if no default value can be found', () => {
25 | const option1 = { ...friends[0], disabled: true };
26 | const option2 = { ...friends[1], disabled: true };
27 |
28 | expect(getOption(null, [option1, option2])).toEqual(null);
29 | });
30 | });
31 |
--------------------------------------------------------------------------------
/__tests__/getValue.test.js:
--------------------------------------------------------------------------------
1 | import { friends } from './data';
2 | import getValue from '../src/lib/getValue';
3 |
4 | describe('Unit test for getValue function', () => {
5 | test('Can get value from option', () => {
6 | const friend = friends[0];
7 |
8 | expect(getValue(friend)).toStrictEqual(friend.value);
9 | });
10 |
11 | test('Non option should return null', () => {
12 | expect(getValue({ name: 'Name' })).toStrictEqual(null);
13 | expect(getValue()).toStrictEqual(null);
14 | });
15 |
16 | test('Can get value from multiple options', () => {
17 | const friend1 = friends[0];
18 | const friend2 = friends[1];
19 |
20 | expect(getValue([friend1, friend2])).toStrictEqual([
21 | friend1.value,
22 | friend2.value,
23 | ]);
24 | });
25 | });
26 |
--------------------------------------------------------------------------------
/__tests__/groupOptions.test.js:
--------------------------------------------------------------------------------
1 | import GroupOptions from '../src/lib/groupOptions';
2 |
3 | describe('Unit test for GroupOptions function', () => {
4 | const flattenOptions = [
5 | {
6 | "name": "Monoton",
7 | "value": "monoton",
8 | "group": "Cursive"
9 | },
10 | {
11 | "name": "Helvetica",
12 | "value": "helvetica",
13 | },
14 | {
15 | "name": "Gloria Hallelujah",
16 | "value": "gloria",
17 | "group": "Cursive"
18 | },
19 | ];
20 |
21 | const groupedOptions = GroupOptions(flattenOptions);
22 |
23 | test('Has correct amount of items', () => {
24 | expect(groupedOptions.length).toEqual(2);
25 | });
26 |
27 | test('First item should be a group', () => {
28 | expect(groupedOptions[0].type).toEqual('group');
29 | expect(groupedOptions[0].name).toEqual('Cursive');
30 | expect('items' in groupedOptions[0]).toEqual(true);
31 | });
32 |
33 | test('Group should have correct amount of items', () => {
34 | expect(groupedOptions[0].items.length).toEqual(2);
35 | });
36 |
37 | test('Last item should not be a group', () => {
38 | expect('items' in groupedOptions[1]).toEqual(false);
39 | });
40 | });
41 |
--------------------------------------------------------------------------------
/__tests__/highlight.test.js:
--------------------------------------------------------------------------------
1 | import highlight from '../src/lib/highlight';
2 | import { friends } from './data';
3 |
4 | describe('Unit test for highlight function', () => {
5 | test('Can move down', () => {
6 | expect(highlight(-1, 'down', friends)).toEqual(0);
7 | });
8 |
9 | test('Can move up', () => {
10 | expect(highlight(3, 'up', friends)).toEqual(2);
11 | });
12 |
13 | test('Can reverse to end or beginning', () => {
14 | expect(highlight(-1, 'up', friends)).toEqual(friends.length - 1);
15 | expect(highlight(friends.length - 1, 'down', friends)).toEqual(0);
16 | });
17 | });
18 |
--------------------------------------------------------------------------------
/__tests__/isSelected.test.js:
--------------------------------------------------------------------------------
1 | import isSelected from '../src/lib/isSelected';
2 |
3 | const option = { value: 'foo', name: 'Foo' };
4 | const secondOption = { value: 'bar', name: 'Bar' };
5 |
6 | describe('Unit test for isSelected function', () => {
7 | test('Should be true', () => {
8 | const str = isSelected(option, option);
9 | const arr = isSelected(option, [option, secondOption]);
10 |
11 | expect(str).toEqual(true);
12 | expect(arr).toEqual(true);
13 | });
14 |
15 | test('Should be false', () => {
16 | const str = isSelected(option, secondOption);
17 | const arr = isSelected(option, [secondOption]);
18 | const arr2 = isSelected(option, []);
19 |
20 | expect(str).toEqual(false);
21 | expect(arr).toEqual(false);
22 | expect(arr2).toEqual(false);
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/__tests__/reduce.test.js:
--------------------------------------------------------------------------------
1 | import flattenOptions from '../src/lib/flattenOptions';
2 | import { countries } from './data';
3 | import reduce from '../src/lib/reduce';
4 |
5 | const options = flattenOptions(countries);
6 |
7 | describe('Unit test for reduce function', () => {
8 | test('Can search', () => {
9 | const middleware = [
10 | (items, query) => items.filter((option) => option.name.indexOf(query) >= 0),
11 | (items) => items.slice(0, 3),
12 | ];
13 |
14 | const filteredOptions = reduce(middleware, options, 'land');
15 |
16 | expect(filteredOptions[0].value).toEqual('AX');
17 | expect(filteredOptions.length).toEqual(3);
18 | });
19 | });
20 |
--------------------------------------------------------------------------------
/__tests__/search.test.js:
--------------------------------------------------------------------------------
1 | import fuzzySearch from '../src/lib/fuzzySearch';
2 | import flattenOptions from '../src/lib/flattenOptions';
3 | import { countries, fontStacks } from './data';
4 |
5 | const options = flattenOptions(countries);
6 | const fontOptions = flattenOptions(fontStacks);
7 |
8 | describe('Unit test for search function', () => {
9 | test('Can search', () => {
10 | const newOptions = fuzzySearch(options, 'swden');
11 | const exactMatch = fuzzySearch(options, 'Italy');
12 | const noOptions = fuzzySearch(options, 'foobar');
13 |
14 | expect(newOptions.length).toEqual(1);
15 | expect(newOptions[0].name).toEqual('Sweden');
16 | expect(exactMatch[0].name).toEqual('Italy');
17 | expect(noOptions.length).toEqual(0);
18 | });
19 |
20 | test('Can search groups', () => {
21 | const newOptions = fuzzySearch(fontOptions, 'Sans serif');
22 | const noOptions = fuzzySearch(fontOptions, 'foobar');
23 |
24 | expect(newOptions.length).toEqual(1);
25 | expect(noOptions.length).toEqual(0);
26 | });
27 | });
28 |
--------------------------------------------------------------------------------
/__tests__/storybook.test.js:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 | import * as glob from 'glob';
3 |
4 | import { describe, test, expect } from '@jest/globals';
5 | import { composeStories } from '@storybook/react';
6 |
7 | const compose = (entry) => {
8 | try {
9 | return composeStories(entry);
10 | } catch (e) {
11 | throw new Error(
12 | `There was an issue composing stories for the module: ${JSON.stringify(entry)}, ${e}`
13 | );
14 | }
15 | };
16 |
17 | function getAllStoryFiles() {
18 | // Place the glob you want to match your stories files
19 | const storyFiles = glob.sync(
20 | path.join(__dirname, '../stories/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}')
21 | );
22 |
23 | return storyFiles.map((filePath) => {
24 | const storyFile = require(filePath);
25 | return { filePath, storyFile };
26 | });
27 | }
28 |
29 | // Recreate similar options to Storyshots. Place your configuration below
30 | const options = {
31 | suite: 'Storybook Tests',
32 | storyKindRegex: /^.*?DontTest$/,
33 | storyNameRegex: /UNSET/,
34 | snapshotsDirName: '__snapshots__',
35 | snapshotExtension: '.storyshot',
36 | };
37 |
38 | describe(options.suite, () => {
39 | getAllStoryFiles().forEach(({ storyFile, componentName }) => {
40 | const meta = storyFile.default;
41 | const title = meta.title || componentName;
42 |
43 | if (options.storyKindRegex.test(title) || meta.parameters?.storyshots?.disable) {
44 | // Skip component tests if they are disabled
45 | return;
46 | }
47 |
48 | describe(title, () => {
49 | const stories = Object.entries(compose(storyFile))
50 | .map(([name, story]) => ({ name, story }))
51 | .filter(({ name, story }) => {
52 | // Implements a filtering mechanism to avoid running stories that are disabled via parameters or that match a specific regex mirroring the default behavior of Storyshots.
53 | return !options.storyNameRegex.test(name) && !story.parameters.storyshots?.disable;
54 | });
55 |
56 | if (stories.length <= 0) {
57 | throw new Error(
58 | `No stories found for this module: ${title}. Make sure there is at least one valid story for this module, without a disable parameter, or add parameters.storyshots.disable in the default export of this file.`
59 | );
60 | }
61 |
62 | stories.forEach(({ name, story }) => {
63 | // Instead of not running the test, you can create logic to skip it, flagging it accordingly in the test results.
64 | const testFn = story.parameters.storyshots?.skip ? test.skip : test;
65 |
66 | testFn(name, async () => {
67 | await story.run();
68 | // Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot.
69 | await new Promise((resolve) => setTimeout(resolve, 1));
70 | expect(document.body.firstChild).toMatchSnapshot();
71 | });
72 | });
73 | });
74 | });
75 | });
76 |
--------------------------------------------------------------------------------
/__tests__/updateOption.test.js:
--------------------------------------------------------------------------------
1 | import updateOption from '../src/lib/updateOption';
2 | import { friends } from './data';
3 |
4 | describe('Unit test for updateOption function', () => {
5 | test('Can change option', () => {
6 | const friend = friends[0];
7 |
8 | expect(updateOption(friend, null).value).toEqual(friend.value);
9 | expect(updateOption(friend, friend[1]).value).toEqual(friend.value);
10 | });
11 |
12 | test('Can update multiple options', () => {
13 | const friend1 = friends[0];
14 | const friend2 = friends[1];
15 |
16 | expect(updateOption(friend1, null, true)).toStrictEqual([friend1]);
17 | expect(updateOption([friend1], null, true)).toStrictEqual([friend1]);
18 | expect(updateOption(friend1, [friend2], true)).toStrictEqual([
19 | friend2,
20 | friend1,
21 | ]);
22 | expect(updateOption(friend1, friend2, true)).toStrictEqual([
23 | friend2,
24 | friend1,
25 | ]);
26 | expect(updateOption(friend1, [friend1], true)).toStrictEqual([]);
27 | });
28 |
29 | test('Return old value if no new one', () => {
30 | const friend = friends[0];
31 |
32 | expect(updateOption(null, friend)).toStrictEqual(friend);
33 | expect(updateOption(null, [friend], true)).toStrictEqual([friend]);
34 | });
35 | });
36 |
--------------------------------------------------------------------------------
/babel.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "plugins": ["transform-react-remove-prop-types"],
3 | "presets": [
4 | "@babel/preset-env",
5 | ["@babel/preset-react", {"runtime": "automatic"}]
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/doctor-storybook.log:
--------------------------------------------------------------------------------
1 | 🩺 The doctor is checking the health of your Storybook..
2 | ╭ Incompatible packages found ───────────────────────────────────────────────────────────────────────────────────────────────────────╮
3 | │ │
4 | │ The following packages are incompatible with Storybook 8.3.2 as they depend on different major versions of Storybook packages: │
5 | │ - @storybook/addon-storyshots@7.6.17 │
6 | │ Repo: https://github.com/storybookjs/storybook/tree/next/code/addons/storyshots-core │
7 | │ - @storybook/addons@7.6.17 │
8 | │ Repo: https://github.com/storybookjs/storybook/tree/next/code/deprecated/addons │
9 | │ │
10 | │ │
11 | │ Please consider updating your packages or contacting the maintainers for compatibility details. │
12 | │ For more on Storybook 8 compatibility, see the linked GitHub issue: │
13 | │ https://github.com/storybookjs/storybook/issues/26031 │
14 | │ │
15 | ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
16 |
17 | You can always recheck the health of your project by running:
18 | npx storybook doctor
19 |
20 | Full logs are available in /Users/tobiasbleckert/Utveckling/www/react-select-search/doctor-storybook.log
21 |
22 |
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | collectCoverage: true,
3 | collectCoverageFrom: ['src/**/{!(types.js|index.js),}.{js,jsx}'],
4 | testMatch: ['/__tests__/*.test.{js,jsx}'],
5 | moduleNameMapper: {
6 | '\\.(css|less)$': 'identity-obj-proxy',
7 | },
8 | testEnvironment: 'jest-environment-jsdom',
9 | };
10 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-select-search",
3 | "version": "4.1.8",
4 | "description": "Lightweight select component for React",
5 | "source": "src/index.js",
6 | "main": "dist/cjs/index.js",
7 | "module": "dist/esm/index.js",
8 | "types": "src/index.d.ts",
9 | "targets": {
10 | "types": false
11 | },
12 | "sideEffects": false,
13 | "scripts": {
14 | "lint": "eslint src --ext .js --ext .jsx",
15 | "test": "npm run build && size-limit && NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
16 | "test:watch": "npm test -- --watch",
17 | "test:coverage": "npm test -- --coverage --silent",
18 | "coveralls": "npm test:coverage && cat ./coverage/lcov.info | coveralls",
19 | "start": "storybook dev -p 6006",
20 | "build": "parcel build --no-cache",
21 | "storybook": "storybook dev -p 6006",
22 | "build-storybook": "storybook build --output-dir public",
23 | "size": "size-limit",
24 | "pub": "npm run build && npm publish",
25 | "eslint": "eslint src --ext .jsx --ext .js"
26 | },
27 | "repository": {
28 | "type": "git",
29 | "url": "https://github.com/tbleckert/react-select-search.git"
30 | },
31 | "keywords": [
32 | "react",
33 | "select",
34 | "js",
35 | "search",
36 | "react-component"
37 | ],
38 | "author": "Tobias Bleckert (hola@tobiasbleckert.se)",
39 | "license": "MIT",
40 | "bugs": {
41 | "url": "https://github.com/tbleckert/react-select-search/issues"
42 | },
43 | "homepage": "https://github.com/tbleckert/react-select-search",
44 | "size-limit": [
45 | {
46 | "path": "dist/esm/index.js",
47 | "limit": "3 kB"
48 | }
49 | ],
50 | "peerDependencies": {
51 | "prop-types": "^15.8.1",
52 | "react": "^18.0.1 || ^17.0.1",
53 | "react-dom": "^18.0.1 || ^17.0.1"
54 | },
55 | "devDependencies": {
56 | "@babel/core": "^7.25.2",
57 | "@babel/preset-env": "^7.25.4",
58 | "@babel/preset-react": "^7.24.7",
59 | "@chromatic-com/storybook": "^2.0.2",
60 | "@jest/globals": "^29.7.0",
61 | "@size-limit/preset-small-lib": "^11.1.5",
62 | "@storybook/addon-actions": "^8.3.2",
63 | "@storybook/addon-essentials": "^8.3.2",
64 | "@storybook/addon-links": "^8.3.2",
65 | "@storybook/addon-webpack5-compiler-babel": "^3.0.3",
66 | "@storybook/addons": "^7.6.17",
67 | "@storybook/react": "^8.3.2",
68 | "@storybook/react-webpack5": "^8.3.2",
69 | "babel-jest": "^29.7.0",
70 | "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
71 | "coveralls": "^3.1.1",
72 | "eslint": "^9.11.1",
73 | "eslint-config-prettier": "^9.1.0",
74 | "eslint-plugin-react": "^7.36.1",
75 | "identity-obj-proxy": "^3.0.0",
76 | "jest": "^29.7.0",
77 | "jest-environment-jsdom": "^29.7.0",
78 | "jest-image-snapshot": "^6.4.0",
79 | "parcel": "^2.12.0",
80 | "prettier": "^3.3.3",
81 | "prop-types": "^15.8.1",
82 | "react": "^18.3.1",
83 | "react-dom": "^18.3.1",
84 | "react-test-renderer": "^18.3.1",
85 | "react-transition-group": "^4.4.5",
86 | "size-limit": "^11.1.5",
87 | "storybook": "^8.3.2",
88 | "storybook-dark-mode": "^4.0.2"
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/SelectSearch.jsx:
--------------------------------------------------------------------------------
1 | import React, { forwardRef, useEffect, useRef, useState } from "react";
2 | import PropTypes from 'prop-types';
3 | import useSelect from './useSelect';
4 | import classes from './lib/classes';
5 | import Options from './components/Options';
6 |
7 | const SelectSearch = forwardRef(
8 | (
9 | {
10 | disabled,
11 | placeholder,
12 | multiple,
13 | search,
14 | autoFocus,
15 | autoComplete = 'on',
16 | id,
17 | closeOnSelect = true,
18 | className = 'select-search',
19 | renderValue,
20 | renderOption,
21 | renderGroupHeader,
22 | fuzzySearch = true,
23 | emptyMessage,
24 | value,
25 | debounce = 250,
26 | printOptions = 'auto',
27 | options = [],
28 | ...hookProps
29 | },
30 | ref,
31 | ) => {
32 | const selectRef = useRef(null);
33 | const cls = (classNames) => classes(classNames, className);
34 | const [controlledValue, setControlledValue] = useState(value);
35 | const [snapshot, valueProps, optionProps] = useSelect({
36 | value: controlledValue,
37 | placeholder,
38 | multiple,
39 | search,
40 | closeOnSelect: closeOnSelect && !multiple,
41 | useFuzzySearch: fuzzySearch,
42 | options,
43 | printOptions,
44 | debounce,
45 | ...hookProps,
46 | });
47 | const { highlighted, value: snapValue, fetching, focus } = snapshot;
48 |
49 | const props = {
50 | ...valueProps,
51 | autoFocus,
52 | autoComplete,
53 | disabled,
54 | };
55 |
56 | useEffect(() => {
57 | const { current } = selectRef;
58 |
59 | if (current) {
60 | const val = Array.isArray(snapValue) ? snapValue[0] : snapValue;
61 | const selected = current.querySelector(
62 | highlighted > -1
63 | ? `[data-index="${highlighted}"]`
64 | : `[value="${encodeURIComponent(val)}"]`,
65 | );
66 |
67 | if (selected) {
68 | const rect = current.getBoundingClientRect();
69 | const selectedRect = selected.getBoundingClientRect();
70 |
71 | current.scrollTop =
72 | selected.offsetTop -
73 | rect.height / 2 +
74 | selectedRect.height / 2;
75 | }
76 | }
77 | }, [snapValue, highlighted, selectRef.current]);
78 |
79 | useEffect(() => setControlledValue(value), [value]);
80 |
81 | return (
82 |