@availity/mui-controlled-form 1.1.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -29,10 +29,30 @@ var __objRest = (source, exclude) => {
29
29
  }
30
30
  return target;
31
31
  };
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
32
52
 
33
53
  // src/lib/AsyncAutocomplete.tsx
34
54
  import { AsyncAutocomplete } from "@availity/mui-autocomplete";
35
- import { Controller } from "react-hook-form";
55
+ import { Controller, useFormContext } from "react-hook-form";
36
56
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
37
57
  var ControlledAsyncAutocomplete = (_a) => {
38
58
  var _b = _a, {
@@ -42,7 +62,9 @@ var ControlledAsyncAutocomplete = (_a) => {
42
62
  rules = {},
43
63
  shouldUnregister,
44
64
  value,
45
- FieldProps
65
+ FieldProps,
66
+ defaultToFirstOption,
67
+ defaultToOnlyOption
46
68
  } = _b, rest = __objRest(_b, [
47
69
  "name",
48
70
  "onBlur",
@@ -50,19 +72,17 @@ var ControlledAsyncAutocomplete = (_a) => {
50
72
  "rules",
51
73
  "shouldUnregister",
52
74
  "value",
53
- "FieldProps"
75
+ "FieldProps",
76
+ "defaultToFirstOption",
77
+ "defaultToOnlyOption"
54
78
  ]);
79
+ const { setValue } = useFormContext();
55
80
  return /* @__PURE__ */ jsx(
56
81
  Controller,
57
82
  {
58
83
  name,
59
84
  defaultValue: rest.defaultValue,
60
- rules: __spreadValues({
61
- onBlur,
62
- onChange,
63
- shouldUnregister,
64
- value
65
- }, rules),
85
+ rules: __spreadValues({ onBlur, onChange, shouldUnregister, value }, rules),
66
86
  shouldUnregister,
67
87
  render: ({ field: { onChange: onChange2, value: value2, onBlur: onBlur2, ref }, fieldState: { error } }) => /* @__PURE__ */ jsx(
68
88
  AsyncAutocomplete,
@@ -85,7 +105,17 @@ var ControlledAsyncAutocomplete = (_a) => {
85
105
  onChange2(value3);
86
106
  },
87
107
  onBlur: onBlur2,
88
- value: value2 || null
108
+ value: value2 || null,
109
+ loadOptions: (offset, limit, inputValue) => __async(void 0, null, function* () {
110
+ const { options, hasMore, offset: returnedOffsetValue } = yield rest.loadOptions(offset, limit, inputValue);
111
+ if (defaultToFirstOption && offset === 0) {
112
+ setValue(name, options[0]);
113
+ }
114
+ if (defaultToOnlyOption && offset === 0 && options.length === 1) {
115
+ setValue(name, options[0]);
116
+ }
117
+ return { options, hasMore, offset: returnedOffsetValue };
118
+ })
89
119
  })
90
120
  )
91
121
  }
@@ -207,9 +237,8 @@ var ControlledCheckbox = (_a) => {
207
237
  };
208
238
 
209
239
  // src/lib/CodesAutocomplete.tsx
210
- import { CodesAutocomplete } from "@availity/mui-autocomplete";
211
- import { Controller as Controller4 } from "react-hook-form";
212
- import { Fragment as Fragment3, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
240
+ import { fetchCodes, handleGetCodesOptionLabel } from "@availity/mui-autocomplete";
241
+ import { jsx as jsx4 } from "react/jsx-runtime";
213
242
  var ControlledCodesAutocomplete = (_a) => {
214
243
  var _b = _a, {
215
244
  name,
@@ -219,7 +248,12 @@ var ControlledCodesAutocomplete = (_a) => {
219
248
  rules = {},
220
249
  shouldUnregister,
221
250
  value,
222
- FieldProps
251
+ FieldProps,
252
+ apiConfig = {},
253
+ queryOptions,
254
+ queryKey = "codes-autocomplete",
255
+ list,
256
+ watchParams
223
257
  } = _b, rest = __objRest(_b, [
224
258
  "name",
225
259
  "defaultValue",
@@ -228,52 +262,44 @@ var ControlledCodesAutocomplete = (_a) => {
228
262
  "rules",
229
263
  "shouldUnregister",
230
264
  "value",
231
- "FieldProps"
265
+ "FieldProps",
266
+ "apiConfig",
267
+ "queryOptions",
268
+ "queryKey",
269
+ "list",
270
+ "watchParams"
232
271
  ]);
272
+ const handleLoadOptions = (offset, limit, inputValue) => __async(void 0, null, function* () {
273
+ const resp = yield fetchCodes(__spreadProps(__spreadValues({}, apiConfig), {
274
+ params: __spreadProps(__spreadValues({}, apiConfig.params), { list, offset, limit, q: inputValue })
275
+ }));
276
+ return resp;
277
+ });
233
278
  return /* @__PURE__ */ jsx4(
234
- Controller4,
235
- {
279
+ ControlledAsyncAutocomplete,
280
+ __spreadProps(__spreadValues({
236
281
  name,
237
282
  defaultValue,
238
- rules: __spreadValues({
239
- onBlur,
240
- onChange,
241
- shouldUnregister,
242
- value
243
- }, rules),
283
+ onBlur,
284
+ onChange,
285
+ rules,
244
286
  shouldUnregister,
245
- render: ({ field: { onChange: onChange2, value: value2, onBlur: onBlur2, ref }, fieldState: { error } }) => /* @__PURE__ */ jsx4(
246
- CodesAutocomplete,
247
- __spreadProps(__spreadValues({}, rest), {
248
- FieldProps: __spreadProps(__spreadValues({
249
- required: typeof rules.required === "object" ? rules.required.value : !!rules.required
250
- }, FieldProps), {
251
- error: !!error,
252
- helperText: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs3(Fragment3, { children: [
253
- error.message,
254
- /* @__PURE__ */ jsx4("br", {}),
255
- FieldProps == null ? void 0 : FieldProps.helperText
256
- ] }) : FieldProps == null ? void 0 : FieldProps.helperText,
257
- inputRef: ref
258
- }),
259
- onChange: (event, value3, reason) => {
260
- if (reason === "clear") {
261
- onChange2(null);
262
- }
263
- onChange2(value3);
264
- },
265
- onBlur: onBlur2,
266
- value: value2 || null
267
- })
268
- )
269
- }
287
+ value,
288
+ FieldProps,
289
+ getOptionLabel: handleGetCodesOptionLabel,
290
+ queryKey,
291
+ queryOptions: __spreadValues({ enabled: !!list }, queryOptions),
292
+ watchParams: __spreadValues({ list }, watchParams)
293
+ }, rest), {
294
+ loadOptions: handleLoadOptions
295
+ })
270
296
  );
271
297
  };
272
298
 
273
299
  // src/lib/Datepicker.tsx
274
300
  import { Datepicker } from "@availity/mui-datepicker";
275
- import { Controller as Controller5 } from "react-hook-form";
276
- import { Fragment as Fragment4, jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
301
+ import { Controller as Controller4 } from "react-hook-form";
302
+ import { Fragment as Fragment3, jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
277
303
  var ControlledDatepicker = (_a) => {
278
304
  var _b = _a, {
279
305
  name,
@@ -295,7 +321,7 @@ var ControlledDatepicker = (_a) => {
295
321
  "FieldProps"
296
322
  ]);
297
323
  return /* @__PURE__ */ jsx5(
298
- Controller5,
324
+ Controller4,
299
325
  {
300
326
  name,
301
327
  defaultValue,
@@ -313,7 +339,7 @@ var ControlledDatepicker = (_a) => {
313
339
  required: typeof rules.required === "object" ? rules.required.value : !!rules.required
314
340
  }, FieldProps), {
315
341
  error: !!error,
316
- helperText: error ? /* @__PURE__ */ jsxs4(Fragment4, { children: [
342
+ helperText: error ? /* @__PURE__ */ jsxs3(Fragment3, { children: [
317
343
  error.message,
318
344
  /* @__PURE__ */ jsx5("br", {}),
319
345
  FieldProps == null ? void 0 : FieldProps.helperText
@@ -333,7 +359,7 @@ var ControlledDatepicker = (_a) => {
333
359
 
334
360
  // src/lib/Input.tsx
335
361
  import { Input } from "@availity/mui-form-utils";
336
- import { Controller as Controller6 } from "react-hook-form";
362
+ import { Controller as Controller5 } from "react-hook-form";
337
363
  import { jsx as jsx6 } from "react/jsx-runtime";
338
364
  var ControlledInput = (_a) => {
339
365
  var _b = _a, {
@@ -356,7 +382,7 @@ var ControlledInput = (_a) => {
356
382
  "value"
357
383
  ]);
358
384
  return /* @__PURE__ */ jsx6(
359
- Controller6,
385
+ Controller5,
360
386
  {
361
387
  name,
362
388
  defaultValue,
@@ -381,9 +407,8 @@ var ControlledInput = (_a) => {
381
407
  };
382
408
 
383
409
  // src/lib/OrganizationAutocomplete.tsx
384
- import { OrganizationAutocomplete } from "@availity/mui-autocomplete";
385
- import { Controller as Controller7 } from "react-hook-form";
386
- import { Fragment as Fragment5, jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
410
+ import { handleGetOrgOptionLabel, fetchOrgs } from "@availity/mui-autocomplete";
411
+ import { jsx as jsx7 } from "react/jsx-runtime";
387
412
  var ControlledOrganizationAutocomplete = (_a) => {
388
413
  var _b = _a, {
389
414
  name,
@@ -393,7 +418,9 @@ var ControlledOrganizationAutocomplete = (_a) => {
393
418
  rules = {},
394
419
  shouldUnregister,
395
420
  value,
396
- FieldProps
421
+ FieldProps,
422
+ queryKey = "org-autocomplete",
423
+ apiConfig = {}
397
424
  } = _b, rest = __objRest(_b, [
398
425
  "name",
399
426
  "defaultValue",
@@ -402,52 +429,36 @@ var ControlledOrganizationAutocomplete = (_a) => {
402
429
  "rules",
403
430
  "shouldUnregister",
404
431
  "value",
405
- "FieldProps"
432
+ "FieldProps",
433
+ "queryKey",
434
+ "apiConfig"
406
435
  ]);
436
+ const handleLoadOptions = (offset, limit) => __async(void 0, null, function* () {
437
+ const resp = yield fetchOrgs(__spreadProps(__spreadValues({}, apiConfig), { params: __spreadProps(__spreadValues({ dropdown: true }, apiConfig.params), { offset, limit }) }));
438
+ return resp;
439
+ });
407
440
  return /* @__PURE__ */ jsx7(
408
- Controller7,
409
- {
441
+ ControlledAsyncAutocomplete,
442
+ __spreadProps(__spreadValues({
410
443
  name,
411
444
  defaultValue,
412
- rules: __spreadValues({
413
- onBlur,
414
- onChange,
415
- shouldUnregister,
416
- value
417
- }, rules),
445
+ onBlur,
446
+ onChange,
447
+ rules,
418
448
  shouldUnregister,
419
- render: ({ field: { onChange: onChange2, value: value2, onBlur: onBlur2, ref }, fieldState: { error } }) => /* @__PURE__ */ jsx7(
420
- OrganizationAutocomplete,
421
- __spreadProps(__spreadValues({}, rest), {
422
- FieldProps: __spreadProps(__spreadValues({
423
- required: typeof rules.required === "object" ? rules.required.value : !!rules.required
424
- }, FieldProps), {
425
- error: !!error,
426
- helperText: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs5(Fragment5, { children: [
427
- error.message,
428
- /* @__PURE__ */ jsx7("br", {}),
429
- FieldProps == null ? void 0 : FieldProps.helperText
430
- ] }) : FieldProps == null ? void 0 : FieldProps.helperText,
431
- inputRef: ref
432
- }),
433
- onChange: (event, value3, reason) => {
434
- if (reason === "clear") {
435
- onChange2(null);
436
- }
437
- onChange2(value3);
438
- },
439
- onBlur: onBlur2,
440
- value: value2 || null
441
- })
442
- )
443
- }
449
+ value,
450
+ FieldProps,
451
+ getOptionLabel: handleGetOrgOptionLabel,
452
+ queryKey
453
+ }, rest), {
454
+ loadOptions: handleLoadOptions
455
+ })
444
456
  );
445
457
  };
446
458
 
447
459
  // src/lib/ProviderAutocomplete.tsx
448
- import { ProviderAutocomplete } from "@availity/mui-autocomplete";
449
- import { Controller as Controller8 } from "react-hook-form";
450
- import { Fragment as Fragment6, jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
460
+ import { handleGetProviderOptionLabel, fetchProviders } from "@availity/mui-autocomplete";
461
+ import { jsx as jsx8 } from "react/jsx-runtime";
451
462
  var ControlledProviderAutocomplete = (_a) => {
452
463
  var _b = _a, {
453
464
  name,
@@ -457,7 +468,10 @@ var ControlledProviderAutocomplete = (_a) => {
457
468
  rules = {},
458
469
  shouldUnregister,
459
470
  value,
460
- FieldProps
471
+ FieldProps,
472
+ apiConfig = {},
473
+ customerId,
474
+ queryKey = "prov-autocomplete"
461
475
  } = _b, rest = __objRest(_b, [
462
476
  "name",
463
477
  "defaultValue",
@@ -466,52 +480,43 @@ var ControlledProviderAutocomplete = (_a) => {
466
480
  "rules",
467
481
  "shouldUnregister",
468
482
  "value",
469
- "FieldProps"
483
+ "FieldProps",
484
+ "apiConfig",
485
+ "customerId",
486
+ "queryKey"
470
487
  ]);
488
+ const handleLoadOptions = (offset, limit, inputValue) => __async(void 0, null, function* () {
489
+ const resp = yield fetchProviders(customerId, __spreadProps(__spreadValues({}, apiConfig), {
490
+ params: __spreadProps(__spreadValues({}, apiConfig.params), { offset, limit, q: inputValue })
491
+ }));
492
+ return resp;
493
+ });
471
494
  return /* @__PURE__ */ jsx8(
472
- Controller8,
473
- {
495
+ ControlledAsyncAutocomplete,
496
+ __spreadProps(__spreadValues({
474
497
  name,
475
498
  defaultValue,
476
- rules: __spreadValues({
477
- onBlur,
478
- onChange,
479
- shouldUnregister,
480
- value
481
- }, rules),
499
+ onBlur,
500
+ onChange,
501
+ rules,
482
502
  shouldUnregister,
483
- render: ({ field: { onChange: onChange2, value: value2, onBlur: onBlur2 }, fieldState: { error } }) => /* @__PURE__ */ jsx8(
484
- ProviderAutocomplete,
485
- __spreadProps(__spreadValues({}, rest), {
486
- FieldProps: __spreadProps(__spreadValues({
487
- required: typeof rules.required === "object" ? rules.required.value : !!rules.required
488
- }, FieldProps), {
489
- error: !!error,
490
- helperText: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs6(Fragment6, { children: [
491
- error.message,
492
- /* @__PURE__ */ jsx8("br", {}),
493
- FieldProps == null ? void 0 : FieldProps.helperText
494
- ] }) : FieldProps == null ? void 0 : FieldProps.helperText
495
- }),
496
- onChange: (event, value3, reason) => {
497
- if (reason === "clear") {
498
- onChange2(null);
499
- }
500
- onChange2(value3);
501
- },
502
- onBlur: onBlur2,
503
- value: value2 || null
504
- })
505
- )
506
- }
503
+ value,
504
+ FieldProps,
505
+ getOptionLabel: handleGetProviderOptionLabel,
506
+ queryOptions: { enabled: !!customerId },
507
+ queryKey,
508
+ watchParams: { customerId }
509
+ }, rest), {
510
+ loadOptions: handleLoadOptions
511
+ })
507
512
  );
508
513
  };
509
514
 
510
515
  // src/lib/RadioGroup.tsx
511
516
  import { RadioGroup } from "@availity/mui-form-utils";
512
- import { Controller as Controller9 } from "react-hook-form";
517
+ import { Controller as Controller6 } from "react-hook-form";
513
518
  import { FormControl, FormLabel, FormHelperText } from "@availity/mui-form-utils";
514
- import { Fragment as Fragment7, jsx as jsx9, jsxs as jsxs7 } from "react/jsx-runtime";
519
+ import { Fragment as Fragment4, jsx as jsx9, jsxs as jsxs4 } from "react/jsx-runtime";
515
520
  var ControlledRadioGroup = (_a) => {
516
521
  var _b = _a, {
517
522
  name,
@@ -537,7 +542,7 @@ var ControlledRadioGroup = (_a) => {
537
542
  "value"
538
543
  ]);
539
544
  return /* @__PURE__ */ jsx9(
540
- Controller9,
545
+ Controller6,
541
546
  {
542
547
  name,
543
548
  defaultValue,
@@ -545,7 +550,7 @@ var ControlledRadioGroup = (_a) => {
545
550
  shouldUnregister,
546
551
  render: (_a2) => {
547
552
  var _b2 = _a2, { field: _c } = _b2, _d = _c, { disabled } = _d, field = __objRest(_d, ["disabled"]), { fieldState: { error } } = _b2;
548
- return /* @__PURE__ */ jsxs7(
553
+ return /* @__PURE__ */ jsxs4(
549
554
  FormControl,
550
555
  {
551
556
  error: !!error,
@@ -554,7 +559,7 @@ var ControlledRadioGroup = (_a) => {
554
559
  children: [
555
560
  /* @__PURE__ */ jsx9(FormLabel, { children: label }),
556
561
  /* @__PURE__ */ jsx9(RadioGroup, __spreadValues(__spreadValues({}, field), rest)),
557
- /* @__PURE__ */ jsx9(FormHelperText, { children: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs7(Fragment7, { children: [
562
+ /* @__PURE__ */ jsx9(FormHelperText, { children: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs4(Fragment4, { children: [
558
563
  error.message,
559
564
  /* @__PURE__ */ jsx9("br", {}),
560
565
  helperText
@@ -569,7 +574,7 @@ var ControlledRadioGroup = (_a) => {
569
574
 
570
575
  // src/lib/Select.tsx
571
576
  import { Select } from "@availity/mui-form-utils";
572
- import { Controller as Controller10 } from "react-hook-form";
577
+ import { Controller as Controller7 } from "react-hook-form";
573
578
  import { jsx as jsx10 } from "react/jsx-runtime";
574
579
  var ControlledSelect = (_a) => {
575
580
  var _b = _a, {
@@ -592,7 +597,7 @@ var ControlledSelect = (_a) => {
592
597
  "value"
593
598
  ]);
594
599
  return /* @__PURE__ */ jsx10(
595
- Controller10,
600
+ Controller7,
596
601
  {
597
602
  name,
598
603
  defaultValue,
@@ -618,8 +623,8 @@ var ControlledSelect = (_a) => {
618
623
 
619
624
  // src/lib/TextField.tsx
620
625
  import { TextField } from "@availity/mui-textfield";
621
- import { Controller as Controller11 } from "react-hook-form";
622
- import { Fragment as Fragment8, jsx as jsx11, jsxs as jsxs8 } from "react/jsx-runtime";
626
+ import { Controller as Controller8 } from "react-hook-form";
627
+ import { Fragment as Fragment5, jsx as jsx11, jsxs as jsxs5 } from "react/jsx-runtime";
623
628
  var ControlledTextField = (_a) => {
624
629
  var _b = _a, {
625
630
  name,
@@ -643,7 +648,7 @@ var ControlledTextField = (_a) => {
643
648
  "value"
644
649
  ]);
645
650
  return /* @__PURE__ */ jsx11(
646
- Controller11,
651
+ Controller8,
647
652
  {
648
653
  name,
649
654
  defaultValue,
@@ -670,7 +675,7 @@ var ControlledTextField = (_a) => {
670
675
  }),
671
676
  inputRef: ref,
672
677
  error: !!error,
673
- helperText: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs8(Fragment8, { children: [
678
+ helperText: (error == null ? void 0 : error.message) ? /* @__PURE__ */ jsxs5(Fragment5, { children: [
674
679
  error.message,
675
680
  /* @__PURE__ */ jsx11("br", {}),
676
681
  helperText
@@ -686,7 +691,7 @@ var ControlledTextField = (_a) => {
686
691
  import {
687
692
  FormProvider,
688
693
  useForm,
689
- useFormContext
694
+ useFormContext as useFormContext2
690
695
  } from "react-hook-form";
691
696
  export {
692
697
  ControlledAsyncAutocomplete,
@@ -702,5 +707,5 @@ export {
702
707
  ControlledTextField,
703
708
  FormProvider,
704
709
  useForm,
705
- useFormContext
710
+ useFormContext2 as useFormContext
706
711
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@availity/mui-controlled-form",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "Availity MUI/react-hook-form controlled form components - part of the @availity/element design system",
5
5
  "keywords": [
6
6
  "react",
@@ -40,7 +40,7 @@
40
40
  "publish:canary": "yarn npm publish --access public --tag canary"
41
41
  },
42
42
  "dependencies": {
43
- "@availity/mui-autocomplete": "^1.0.8",
43
+ "@availity/mui-autocomplete": "^1.0.9",
44
44
  "@availity/mui-checkbox": "^1.0.1",
45
45
  "@availity/mui-datepicker": "^1.0.6",
46
46
  "@availity/mui-form-utils": "^1.1.0",
@@ -65,6 +65,7 @@
65
65
  "yup": "^1.6.1"
66
66
  },
67
67
  "peerDependencies": {
68
+ "@availity/api-axios": "^8.0.7",
68
69
  "@mui/material": "^6.4.5",
69
70
  "react": ">=16.3.0"
70
71
  },
@@ -13,54 +13,31 @@ const meta: Meta<typeof ControlledAsyncAutocomplete> = {
13
13
  title: 'Form Components/Controlled Form/Autocomplete/ControlledAsyncAutocomplete',
14
14
  component: ControlledAsyncAutocomplete,
15
15
  tags: ['autodocs'],
16
- argTypes: {...AllControllerPropertiesCategorized, ...AsyncAutocompletePropsCategorized}
16
+ argTypes: { ...AllControllerPropertiesCategorized, ...AsyncAutocompletePropsCategorized },
17
17
  };
18
18
 
19
19
  export default meta;
20
20
 
21
21
  const api = new AvApi({ name: 'example' } as ApiConfig);
22
22
 
23
- type Option = {
24
- label: string;
25
- value: number;
26
- };
23
+ type Option = { label: string; value: number };
27
24
 
28
- type ExampleResponse = {
29
- totalCount: number;
30
- options: Option[];
31
- count: number;
32
- };
25
+ type ExampleResponse = { totalCount: number; options: Option[]; count: number };
33
26
 
34
27
  const getResults = async (offset: number, limit: number) => {
35
28
  // const offset = page * limit;
36
29
  const resp = await api.post<ExampleResponse>({ offset, limit }, { params: {} });
37
30
 
38
- return {
39
- totalCount: resp.data.totalCount,
40
- offset,
41
- limit,
42
- options: resp.data.options,
43
- count: resp.data.count,
44
- };
31
+ return { totalCount: resp.data.totalCount, offset, limit, options: resp.data.options, count: resp.data.count };
45
32
  };
46
33
 
47
34
  const loadOptions = async (offset: number, limit: number) => {
48
35
  const { options, totalCount } = await getResults(offset, limit);
49
36
 
50
- return {
51
- options,
52
- hasMore: offset + limit < totalCount,
53
- offset,
54
- };
37
+ return { options, hasMore: offset + limit < totalCount, offset };
55
38
  };
56
39
 
57
- const client = new QueryClient({
58
- defaultOptions: {
59
- queries: {
60
- refetchOnWindowFocus: false,
61
- },
62
- },
63
- });
40
+ const client = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false } } });
64
41
 
65
42
  export const _ControlledAsyncAutoComplete: StoryObj<typeof ControlledAsyncAutocomplete> = {
66
43
  render: (args) => {
@@ -72,15 +49,60 @@ export const _ControlledAsyncAutoComplete: StoryObj<typeof ControlledAsyncAutoco
72
49
  <form onSubmit={methods.handleSubmit((data) => data)}>
73
50
  <ControlledAsyncAutocomplete {...args} />
74
51
  <Grid container direction="row" justifyContent="space-between" marginTop={1}>
75
- <Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
52
+ <Button
53
+ disabled={!methods?.formState?.isSubmitSuccessful}
54
+ children="Reset"
55
+ color="secondary"
56
+ onClick={() => methods.reset()}
57
+ />
58
+ <Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
59
+ </Grid>
60
+ {methods?.formState?.isSubmitSuccessful ? (
61
+ <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
62
+ <Typography variant="h2">Submitted Values</Typography>
63
+ <pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
64
+ </Paper>
65
+ ) : null}
66
+ </form>
67
+ </FormProvider>
68
+ </QueryClientProvider>
69
+ );
70
+ },
71
+ args: {
72
+ name: 'controlledAsyncAutocomplete',
73
+ FieldProps: { label: 'Async Select', helperText: 'Helper Text', fullWidth: false, required: true },
74
+ getOptionLabel: (val: Option) => val.label,
75
+ loadOptions,
76
+ limit: 10,
77
+ queryKey: 'example',
78
+ rules: { required: 'This is required.' },
79
+ },
80
+ };
81
+
82
+ export const _ControlledAsyncAutoCompleteDefaultOption: StoryObj<typeof ControlledAsyncAutocomplete> = {
83
+ render: (args) => {
84
+ const methods = useForm();
85
+
86
+ return (
87
+ <QueryClientProvider client={client}>
88
+ <FormProvider {...methods}>
89
+ <form onSubmit={methods.handleSubmit((data) => data)}>
90
+ <ControlledAsyncAutocomplete {...args} />
91
+ <Grid container direction="row" justifyContent="space-between" marginTop={1}>
92
+ <Button
93
+ disabled={!methods?.formState?.isSubmitSuccessful}
94
+ children="Reset"
95
+ color="secondary"
96
+ onClick={() => methods.reset()}
97
+ />
76
98
  <Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
77
99
  </Grid>
78
- { methods?.formState?.isSubmitSuccessful ? (
100
+ {methods?.formState?.isSubmitSuccessful ? (
79
101
  <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
80
102
  <Typography variant="h2">Submitted Values</Typography>
81
103
  <pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
82
104
  </Paper>
83
- ) : null }
105
+ ) : null}
84
106
  </form>
85
107
  </FormProvider>
86
108
  </QueryClientProvider>
@@ -93,6 +115,8 @@ export const _ControlledAsyncAutoComplete: StoryObj<typeof ControlledAsyncAutoco
93
115
  loadOptions,
94
116
  limit: 10,
95
117
  queryKey: 'example',
96
- rules: { required:'This is required.' },
118
+ rules: { required: 'This is required.' },
119
+ defaultToFirstOption: true,
120
+ disableClearable: true,
97
121
  },
98
122
  };