UNPKG

17.3 kBMarkdownView Raw
1# @availity/reactstrap-validation-select
2
3> Wrapper for react-select (with async pagination) to work with availity-reactstrap-validation.
4
5[![Version](https://img.shields.io/npm/v/@availity/reactstrap-validation-select.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/reactstrap-validation-select)
6
7## Installation
8
9```bash
10npx install-peerdeps @availity/reactstrap-validation-select --save
11```
12
13### Usage
14
15```javascript
16import React from 'react';
17import { Label } from 'reactstrap';
18import { AvForm, AvGroup, AvFeedback } from 'availity-reactstrap-validation';
19import AvSelect, { AvSelectField } from '@availity/reactstrap-validation-select';
20import '@availity/reactstrap-validation-select/styles.scss';
21// ...
22const options = [
23 {label: "Option 1", value: 1},
24 {label: "Option 2", value: 2},
25 {label: "Option 3", value: 3},
26];
27// ...
28<AvForm>
29 <AvGroup>
30 <Label for="justTheInput">My Input Label</Label>
31 <AvSelect
32 name="justTheInput"
33 options={options}
34 required
35 />
36 <AvFeedback>Some error message</AvFeedback>
37 </AvGroup>
38
39 <AvSelectField
40 name="fieldWithLabel"
41 label="Label Made For Me"
42 options={options}
43 required
44 />
45</AvForm>;
46```
47
48Note: the input callbacks (e.g. onChange) do not get called with an event like other reactstrap-validation component; just the value of the field. This is because the underlying react-select does not return the event in it's callbacks.
49
50### AvSelect (Default export)
51
52This is the underlying select without the `AvGroup`, `Label` or `AvFeedback`
53
54#### AvSelect Props
55
56Please refer to [react-select with async pagination](https://github.com/TheSharpieOne/react-select/tree/npm/v2-async-pagination)'s props and [availity-reactstrap-validation](https://github.com/Availity/availity-reactstrap-validation)'s input validation props. This component just combines those.
57
58* **`raw`**: Boolean. Optional. Default: `false`. If `true`, the entire object of the selected value will be returned as the value instead of the value for the `valueKey` within the object.
59* **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
60* **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
61* **`autofill`**: Boolean or Object. Optional. Default: `false`. If `true`, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the new value are auto-filled. In order for a field to be auto-filled, the `name` property on the field _must_ match the key inside the new value.
62
63For example, if the new value is `{ "payer": "Availity" }`, in order for the payer input in the form to be auto-filled to "Availity", the `name` prop on the input must be "payer".
64
65If `autofill` is an object, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the keys in the `autofill` prop will be auto-filled.
66
67When `autofill` is an object, the values in the object can be a string or a function. When a string, the key in the `autofill` prop gets auto-filled to that path on the selected option. When a function, it gets called with the selected option, and the key in the `autofill` prop gets auto-filled to the return value of the function.
68
69For example, if the new value is `{ "payer": { "name": "Availity", "id": "1" } }`, in order for the "payerName", "payerId", and "payerNameAndId" inputs to be auto-filled to "Availity", "1", and "1 - Availity" respectively, the `autofill` prop should be:
70
71```js
72{
73 payerName: 'payer.name',
74 payerId: 'payer.id',
75 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
76}
77```
78
79#### AvSelect Example usage
80
81```javascript
82import React from 'react';
83import { Label } from 'reactstrap';
84import { AvForm, AvGroup, AvFeedback } from 'availity-reactstrap-validation';
85import AvSelect from '@availity/reactstrap-validation-select';
86import '@availity/reactstrap-validation-select/styles.scss';
87// ...
88const options = [
89 {label: "Option 1", value: 1},
90 {label: "Option 2", value: 2},
91 {label: "Option 3", value: 3},
92];
93// ...
94<AvForm>
95 <AvGroup>
96 <Label for="justTheInput">My Input Label</Label>
97 <AvSelect
98 name="justTheInput"
99 options={options}
100 required
101 />
102 <AvFeedback>Some error message</AvFeedback>
103 </AvGroup>
104</AvForm>;
105```
106
107### AvSelectField
108
109Please refer to [react-select with async pagination](https://github.com/TheSharpieOne/react-select/tree/npm/v2-async-pagination)'s props and [availity-reactstrap-validation](https://github.com/Availity/availity-reactstrap-validation)'s field validation props. This component just combines those.
110
111#### AvSelectField Props
112
113Please refer to [react-select with async pagination](https://github.com/TheSharpieOne/react-select/tree/npm/v2-async-pagination)'s props and [availity-reactstrap-validation](https://github.com/Availity/availity-reactstrap-validation)'s input validation props. This component just combines those.
114
115* **`raw`**: Boolean. Optional. Default: `false`. If `true`, the entire object of the selected value will be returned as the value instead of the value for the `valueKey` within the object.
116* **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
117* **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
118* **`groupClass`**: String. Optional. ClassName to add to the wrapping AvGroup
119* **`labelClass`**: String. Optional. ClassName to add to the label
120* **`feedbackClass`**: String. Optional. ClassName to add to the AvFeedback
121* **`creatable`**: boolean. Optional. Whether or not to allow new items to be created if not found.
122* **`autofill`**: Boolean or Object. Optional. Default: `false`. If `true`, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the new value are auto-filled. In order for a field to be auto-filled, the `name` property on the field _must_ match the key inside the new value.
123
124For example, if the new value is `{ "payer": "Availity" }`, in order for the payer input in the form to be auto-filled to "Availity", the `name` prop on the input must be "payer".
125
126If `autofill` is an object, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the keys in the `autofill` prop will be auto-filled.
127
128When `autofill` is an object, the values in the object can be a string or a function. When a string, the key in the `autofill` prop gets auto-filled to that path on the selected option. When a function, it gets called with the selected option, and the key in the `autofill` prop gets auto-filled to the return value of the function.
129
130For example, if the new value is `{ "payer": { "name": "Availity", "id": "1" } }`, in order for the "payerName", "payerId", and "payerNameAndId" inputs to be auto-filled to "Availity", "1", and "1 - Availity" respectively, the `autofill` prop should be:
131
132```js
133{
134 payerName: 'payer.name',
135 payerId: 'payer.id',
136 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
137}
138```
139
140#### AvSelectField Example usage
141
142```javascript
143import React from 'react';
144import { AvSelectField } from '@availity/reactstrap-validation-select';
145import '@availity/reactstrap-validation-select/styles.scss';
146// ...
147const options = [
148 {label: "Option 1", value: 1},
149 {label: "Option 2", value: 2},
150 {label: "Option 3", value: 3},
151];
152// ...
153<AvForm>
154 <AvSelectField
155 name="fieldWithLabel"
156 label="Label Made For Me"
157 options={options}
158 required
159 />
160</AvForm>;
161```
162
163### AvResourceSelect
164
165A select list which automatically loads and pages though a resource when the user scrolls down.
166
167The search field will fire a request after the debounce timer (see `debounceTimeout` prop) using the given `resource` prop with the payload:
168
169```js
170{
171 limit: "50", //limit quantity can be changed with `itemsPerPage` prop
172 offset: "0",
173 q: "user typed search text after debounce"
174}
175```
176
177#### AvResourceSelect Props
178
179Please refer to [react-select-async-paginate](https://github.com/vtaits/react-select-async-paginate)'s props and [availity-reactstrap-validation](https://github.com/Availity/availity-reactstrap-validation)'s input validation props. This component just combines those.
180
181* **`raw`**: Boolean. Optional. Default: `true`. If `true`, the entire object of the selected value will be returned as the value instead of the value for the `valueKey` within the object.
182* **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
183* **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
184* **`label`**: String. Optional. If provided, the rendered component will mimic `AvSelectField` instead of `AvSelect` (it will create a group with a label and feedback).
185* **`requestConfig`**: Object. Optional. Configuration object which will be used with the query method on the resource. Useful for defining headers to be sent with the request.
186* **`parameters`**: Object. Optional. Object which will be used to create querystring parameters in the request.
187* **`customerId`**: String. Optional. The value of the customer ID which will be sent in the parameters. Useful for restricting the loaded options to be related to the organization the user has in context.
188* **`requiredParams`**: Array of strings. Optional. If present, the network request will not be made until all of the required parameters specified in the array have a truthy value.
189* **`cacheUniq`**: Any. Optional. When this prop changes, all cache options are cleared. (see [react-select-async-paginate](https://github.com/vtaits/react-select-async-paginate#cacheuniq))
190* **`watchParams`**: Array of strings. Optional. If present, the options will reset when any of the parameters specified in the array change value. This is useful for when a customerId changes and you need to load a new list of options for the user to choose from. Used to derive `cacheUniq` if `cacheUniq` prop is not provided.
191* **`resource`**: Availity API resource (see [@availity/api-core](https://github.com/Availity/sdk-js/tree/master/packages/api-core) and [@availity/api-axios](https://github.com/Availity/sdk-js/tree/master/packages/api-axios)). Required.
192* **`getResult`**: String or Function. Optional. When a function, the function will be called with the API response body/payload and is expected to return an array containing the list of items for the page. When a string, the string is expected to be a simple key used to get the value from the response ("simple" meaning not handling dot-notation for nested objects, if you need that provide a function.)
193* **`debounceTimeout`**: Number. default: 350. The amount of time (in milliseconds) to wait after the user has stopped typing before making the network request (debounced input).
194* **`delay`**: Number. default: 350. Set to `debounceTimeout` if `debounceTimeout` is not provided. (see [react-select-async-paginate](https://github.com/vtaits/react-select-async-paginate#debouncetimeout))
195* **`itemsPerPage`**: Number. Optional. Default: `50`. The number of items to fetched be displayed per page when the usr scrolls down.
196* **`onPageChange`**: Function. Optional. A callback function to inform you that the user has scrolled to the bottom of the list and more items are loaded. The current input value and the page the user wants to go to will be provided as arguments to the callback function.
197* **`hasMore`**: Boolean or Function. Optional. If true, `AvResourceSelect` will attempt to retrieve the next page of results. `response.data` from axios response is passed as the only argument to `hasMore` when `hasMore` is a function. Defaults to: `({ totalCount, limit, offset }) => totalCount > offset + limit;` for non-GraphQL apis. Defaults to `(data) => data.data[${this.props.graphqlConfig.type}Pagination].pageInfo.hasNextPage` for GraphQL apis.
198* **`additional`**: Object. Optional. Additional properties to pass to `AsyncPaginate` (see [react-select-async-paginate](https://github.com/vtaits/react-select-async-paginate#additional)).
199* **`graphqlConfig`**: Object{ type, query }. Optional. `type` String. is the type of asset returned. `query` String. is the GraphQL query to use in the request.
200* **`creatable`**: boolean. Optional. Whether or not to allow new items to be created if not found.
201* **`minCharsToSearch`**: Number. Optional. The minimum number of characters the user must input before `AvResourceSelect` makes the network request. If the user has not inputted any characters, the network request will still be made. Useful for relieving pressure on the api the `resource` is calling.
202* **`waitUntilFocused`**: Boolean. Optional. When true, the network request is not made until the dropdown has been focused.
203* **`autofill`**: Boolean or Object. Optional. Default: `false`. If `true`, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the new value are auto-filled. In order for a field to be auto-filled, the `name` property on the field _must_ match the key inside the new value.
204
205For example, if the new value is `{ "payer": "Availity" }`, in order for the payer input in the form to be auto-filled to "Availity", the `name` prop on the input must be "payer".
206
207If `autofill` is an object, when the value of the dropdown changes, if the `isMulti` prop is `false` _and_ the new value of the dropdown is an object, all fields on the form corresponding to the keys in the `autofill` prop will be auto-filled.
208
209When `autofill` is an object, the values in the object can be a string or a function. When a string, the key in the `autofill` prop gets auto-filled to that path on the selected option. When a function, it gets called with the selected option, and the key in the `autofill` prop gets auto-filled to the return value of the function.
210
211For example, if the new value is `{ "payer": { "name": "Availity", "id": "1" } }`, in order for the "payerName", "payerId", and "payerNameAndId" inputs to be auto-filled to "Availity", "1", and "1 - Availity" respectively, the `autofill` prop should be:
212
213```js
214{
215 payerName: 'payer.name',
216 payerId: 'payer.id',
217 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
218}
219```
220
221#### AvResourceSelect Example usage
222
223```javascript
224import React from 'react';
225import AvApi from '@availity/api-axios';
226import { AvResourceSelect } from '@availity/reactstrap-validation-select';
227import '@availity/reactstrap-validation-select/styles.scss';
228// ...
229const avCustomResource = new AvApi({ name: 'my-custom-resource' });
230// ...
231<AvForm>
232 <AvResourceSelect
233 name="fieldWithLabel"
234 label="Label Made For Me"
235 resource={avCustomResource}
236 required
237 />
238</AvForm>;
239```
240
241#### Pre-made Resource Selects
242
243The follow components exist and can be imported by name from `@availity/reactstrap-validation-select/resources`
244
245* AvProviderSelect
246* AvOrganizationSelect
247* AvRegionSelect
248* AvPermissionSelect
249* AvNavigationSelect
250* AvUserSelect
251
252These components are `AvResourceSelect` with pre-configured `resource`, `valueKey`, and `labelKey` to make it easy to use. All of the props for `AvResourceSelect` can be provided to override the defaults of these pre-made components. For some of the component you will want to provide the `customerId` prop.
253
254##### Pre-made Resource Selects Example Usage
255
256
257```javascript
258import React from 'react';
259import AvApi from '@availity/api-axios';
260import {
261 AvProviderSelect,
262 AvOrganizationSelect,
263 AvRegionSelect,
264 AvPermissionSelect,
265 AvNavigationSelect,
266 AvUserSelect,
267} from '@availity/reactstrap-validation-select/resources';
268import '@availity/reactstrap-validation-select/styles.scss';
269// ...
270<AvForm>
271 <AvRegionSelect
272 name="region"
273 label="Select a Region"
274 required
275 />
276 <AvOrganizationSelect
277 name="organization"
278 label="Select a Organization"
279 required
280 />
281 <AvProviderSelect
282 name="provider"
283 customerId="1234"
284 requiredParams={['customerId']}
285 watchParams={['customerId']}
286 label="Select a provider"
287 customerId={customerId}
288 required
289 />
290 <AvPermissionSelect
291 name="permissions"
292 label="Select a provider"
293 customerId={customerId}
294 isMulti
295 required
296 />
297 <AvNavigationSelect
298 name="payerSpace"
299 label="Select a Payer Space"
300 customerId={customerId}
301 required
302 />
303 <AvUserSelect
304 name="user"
305 label="Select a User"
306 customerId={customerId}
307 />
308</AvForm>;
309```