UNPKG

16.8 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```js
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
28const Example = () => (
29 <AvForm>
30 <AvGroup>
31 <Label for="justTheInput">My Input Label</Label>
32 <AvSelect name="justTheInput" options={options} required />
33 <AvFeedback>Some error message</AvFeedback>
34 </AvGroup>
35 <AvSelectField name="fieldWithLabel" label="Label Made For Me" options={options} required />
36 </AvForm>
37);
38```
39
40Note: 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.
41
42### AvSelect (Default export)
43
44This is the underlying select without the `AvGroup`, `Label` or `AvFeedback`
45
46#### AvSelect Props
47
48Please 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.
49
50- **`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.
51- **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
52- **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
53- **`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.
54
55For 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".
56
57If `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.
58
59When `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.
60
61For 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:
62
63```js
64{
65 payerName: 'payer.name',
66 payerId: 'payer.id',
67 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
68}
69```
70
71#### AvSelect Example usage
72
73```js
74import React from 'react';
75import { Label } from 'reactstrap';
76import { AvForm, AvGroup, AvFeedback } from 'availity-reactstrap-validation';
77import AvSelect from '@availity/reactstrap-validation-select';
78import '@availity/reactstrap-validation-select/styles.scss';
79
80const options = [
81 { label: 'Option 1', value: 1 },
82 { label: 'Option 2', value: 2 },
83 { label: 'Option 3', value: 3 },
84];
85
86const Example = () => (
87 <AvForm>
88 <AvGroup>
89 <Label for="justTheInput">My Input Label</Label>
90 <AvSelect name="justTheInput" options={options} required />
91 <AvFeedback>Some error message</AvFeedback>
92 </AvGroup>
93 </AvForm>
94);
95```
96
97### AvSelectField
98
99Please 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.
100
101#### AvSelectField Props
102
103Please 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.
104
105- **`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.
106- **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
107- **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
108- **`groupClass`**: String. Optional. ClassName to add to the wrapping AvGroup
109- **`labelClass`**: String. Optional. ClassName to add to the label
110- **`feedbackClass`**: String. Optional. ClassName to add to the AvFeedback
111- **`creatable`**: boolean. Optional. Whether or not to allow new items to be created if not found.
112- **`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.
113
114For 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".
115
116If `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.
117
118When `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.
119
120For 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:
121
122```js
123{
124 payerName: 'payer.name',
125 payerId: 'payer.id',
126 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
127}
128```
129
130#### AvSelectField Example usage
131
132```js
133import React from 'react';
134import { AvSelectField } from '@availity/reactstrap-validation-select';
135import '@availity/reactstrap-validation-select/styles.scss';
136
137const options = [
138 { label: 'Option 1', value: 1 },
139 { label: 'Option 2', value: 2 },
140 { label: 'Option 3', value: 3 },
141];
142
143const Example = () => (
144 <AvForm>
145 <AvSelectField name="fieldWithLabel" label="Label Made For Me" options={options} required />
146 </AvForm>
147);
148```
149
150### AvResourceSelect
151
152A select list which automatically loads and pages though a resource when the user scrolls down.
153
154The search field will fire a request after the debounce timer (see `debounceTimeout` prop) using the given `resource` prop with the payload:
155
156```js
157{
158 limit: "50", //limit quantity can be changed with `itemsPerPage` prop
159 offset: "0",
160 q: "user typed search text after debounce"
161}
162```
163
164#### AvResourceSelect Props
165
166Please 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.
167
168- **`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.
169- **`valueKey`**: String. Optional. The key of the value you want returned when selected. Default: `value`
170- **`labelKey`**: String. Optional. The key for the label you want to appear in the dropdown for the user to see. Default `label`
171- **`label`**: String. Optional. If provided, the rendered component will mimic `AvSelectField` instead of `AvSelect` (it will create a group with a label and feedback).
172- **`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.
173- **`parameters`**: Object. Optional. Object which will be used to create querystring parameters in the request.
174- **`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.
175- **`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.
176- **`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))
177- **`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.
178- **`resource`**: Availity API resource (see [@availity/api-axios](https://github.com/Availity/sdk-js/tree/master/packages/api-axios)). Required.
179- **`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.)
180- **`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).
181- **`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))
182- **`itemsPerPage`**: Number. Optional. Default: `50`. The number of items to fetched be displayed per page when the usr scrolls down.
183- **`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.
184- **`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.
185- **`additional`**: Object. Optional. Additional properties to pass to `AsyncPaginate` (see [react-select-async-paginate](https://github.com/vtaits/react-select-async-paginate#additional)).
186- **`graphqlConfig`**: Object{ type, query }. Optional. `type` String. is the type of asset returned. `query` String. is the GraphQL query to use in the request.
187- **`creatable`**: boolean. Optional. Whether or not to allow new items to be created if not found.
188- **`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.
189- **`waitUntilFocused`**: Boolean. Optional. When true, the network request is not made until the dropdown has been focused.
190- **`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.
191
192For 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".
193
194If `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.
195
196When `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.
197
198For 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:
199
200```js
201{
202 payerName: 'payer.name',
203 payerId: 'payer.id',
204 payerNameAndId: opt => `${opt.payer.id} - ${opt.payer.name}`,
205}
206```
207
208#### AvResourceSelect Example usage
209
210```js
211import React from 'react';
212import AvApi from '@availity/api-axios';
213import { AvResourceSelect } from '@availity/reactstrap-validation-select';
214import '@availity/reactstrap-validation-select/styles.scss';
215
216const avCustomResource = new AvApi({ name: 'my-custom-resource' });
217
218const Example = () => (
219 <AvForm>
220 <AvResourceSelect name="fieldWithLabel" label="Label Made For Me" resource={avCustomResource} required />
221 </AvForm>
222);
223```
224
225#### Pre-made Resource Selects
226
227The follow components exist and can be imported by name from `@availity/reactstrap-validation-select/resources`
228
229- AvProviderSelect
230- AvOrganizationSelect
231- AvRegionSelect
232- AvPermissionSelect
233- AvNavigationSelect
234- AvUserSelect
235
236These 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.
237
238##### Pre-made Resource Selects Example Usage
239
240```js
241import React from 'react';
242import AvApi from '@availity/api-axios';
243import {
244 AvProviderSelect,
245 AvOrganizationSelect,
246 AvRegionSelect,
247 AvPermissionSelect,
248 AvNavigationSelect,
249 AvUserSelect,
250} from '@availity/reactstrap-validation-select/resources';
251import '@availity/reactstrap-validation-select/styles.scss';
252
253const Example = () => (
254 <AvForm>
255 <AvRegionSelect name="region" label="Select a Region" required />
256 <AvOrganizationSelect name="organization" label="Select a Organization" required />
257 <AvProviderSelect
258 name="provider"
259 customerId="1234"
260 requiredParams={['customerId']}
261 watchParams={['customerId']}
262 label="Select a provider"
263 customerId={customerId}
264 required
265 />
266 <AvPermissionSelect name="permissions" label="Select a provider" customerId={customerId} isMulti required />
267 <AvNavigationSelect name="payerSpace" label="Select a Payer Space" customerId={customerId} required />
268 <AvUserSelect name="user" label="Select a User" customerId={customerId} />
269 </AvForm>
270);
271```