@servicetitan/form-state 31.2.0 → 32.0.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.
Files changed (37) hide show
  1. package/dist/async-lazy-dropdown-state/async-lazy-dropdown-state.js +61 -57
  2. package/dist/async-lazy-dropdown-state/async-lazy-dropdown-state.js.map +1 -1
  3. package/dist/async-lazy-dropdown-state/index.js +1 -0
  4. package/dist/async-lazy-dropdown-state/index.js.map +1 -1
  5. package/dist/async-lazy-dropdown-state/use-async-lazy-dropdown-state.js +23 -13
  6. package/dist/async-lazy-dropdown-state/use-async-lazy-dropdown-state.js.map +1 -1
  7. package/dist/date-range.js +2 -1
  8. package/dist/date-range.js.map +1 -1
  9. package/dist/demo/dropdown-state.js +284 -60
  10. package/dist/demo/dropdown-state.js.map +1 -1
  11. package/dist/demo/index.js +1 -0
  12. package/dist/demo/index.js.map +1 -1
  13. package/dist/dropdown-state.js +168 -222
  14. package/dist/dropdown-state.js.map +1 -1
  15. package/dist/form-helpers.js +73 -116
  16. package/dist/form-helpers.js.map +1 -1
  17. package/dist/form-validators.js +25 -39
  18. package/dist/form-validators.js.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/persistent-form-state/domain-storage.js +22 -16
  22. package/dist/persistent-form-state/domain-storage.js.map +1 -1
  23. package/dist/persistent-form-state/in-memory-storage.js +14 -5
  24. package/dist/persistent-form-state/in-memory-storage.js.map +1 -1
  25. package/dist/persistent-form-state/index.js +1 -0
  26. package/dist/persistent-form-state/index.js.map +1 -1
  27. package/dist/persistent-form-state/persistent-form-state.js +40 -71
  28. package/dist/persistent-form-state/persistent-form-state.js.map +1 -1
  29. package/package.json +5 -5
  30. package/dist/__tests__/form-helpers.test.js +0 -263
  31. package/dist/__tests__/form-helpers.test.js.map +0 -1
  32. package/dist/__tests__/form-validators.test.js +0 -48
  33. package/dist/__tests__/form-validators.test.js.map +0 -1
  34. package/dist/async-lazy-dropdown-state/async-lazy-dropdown-state.stories.js +0 -46
  35. package/dist/async-lazy-dropdown-state/async-lazy-dropdown-state.stories.js.map +0 -1
  36. package/dist/persistent-form-state/__tests__/domain-storage.test.js +0 -60
  37. package/dist/persistent-form-state/__tests__/domain-storage.test.js.map +0 -1
@@ -1,80 +1,84 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _ts_decorate(decorators, target, key, desc) {
2
15
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
16
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
18
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
19
+ }
20
+ function _ts_metadata(k, v) {
8
21
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
22
+ }
10
23
  import { computed, makeObservable } from 'mobx';
11
24
  import { AsyncDataSource } from '@servicetitan/data-query';
12
25
  import { DropdownState } from '../dropdown-state';
13
26
  export class AsyncLazyDropdownState extends DropdownState {
14
- constructor(dataFetcher) {
15
- super({
16
- dataSource: new AsyncDataSource({
17
- get: async () => {
18
- const result = await dataFetcher(this.search);
19
- return {
20
- data: result,
21
- total: result.length,
22
- };
23
- },
24
- }),
25
- lazy: true,
26
- });
27
- Object.defineProperty(this, "arrayValue", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: []
32
- });
33
- Object.defineProperty(this, "onChange", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: (value) => {
38
- const isEmptyValue = value === undefined || (Array.isArray(value) && value.length === 0);
39
- this.arrayValue = isEmptyValue ? [] : !Array.isArray(value) ? [value] : value;
40
- }
41
- });
42
- Object.defineProperty(this, "getOptionsWithValue", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: () => {
47
- const options = [...this.options];
48
- if (!this.search) {
49
- const values = this.arrayValue;
50
- const selectedOptions = options.filter(option => !!values.find(value => value.value === option.value));
51
- const unselectedOptions = options.filter(option => !values.find(value => value.value === option.value));
52
- const missingSelectedOptions = values.filter(value => !options.some(option => option.value === value.value));
53
- return [...missingSelectedOptions, ...selectedOptions, ...unselectedOptions];
54
- }
55
- return options;
56
- }
57
- });
58
- makeObservable(this);
59
- }
60
27
  get optionsWithValue() {
61
28
  return this.getOptionsWithValue();
62
29
  }
63
30
  setDataFetcher(dataFetcher) {
64
31
  this.setDataSource(new AsyncDataSource({
65
- get: async () => {
32
+ get: async ()=>{
66
33
  const result = await dataFetcher(this.search);
67
34
  return {
68
35
  data: result,
69
- total: result.length,
36
+ total: result.length
70
37
  };
71
- },
38
+ }
72
39
  }));
73
40
  }
41
+ constructor(dataFetcher){
42
+ super({
43
+ dataSource: new AsyncDataSource({
44
+ get: async ()=>{
45
+ const result = await dataFetcher(this.search);
46
+ return {
47
+ data: result,
48
+ total: result.length
49
+ };
50
+ }
51
+ }),
52
+ lazy: true
53
+ }), _define_property(this, "arrayValue", []), _define_property(this, "onChange", (value)=>{
54
+ const isEmptyValue = value === undefined || Array.isArray(value) && value.length === 0;
55
+ this.arrayValue = isEmptyValue ? [] : !Array.isArray(value) ? [
56
+ value
57
+ ] : value;
58
+ }), _define_property(this, "getOptionsWithValue", ()=>{
59
+ const options = [
60
+ ...this.options
61
+ ];
62
+ if (!this.search) {
63
+ const values = this.arrayValue;
64
+ const selectedOptions = options.filter((option)=>!!values.find((value)=>value.value === option.value));
65
+ const unselectedOptions = options.filter((option)=>!values.find((value)=>value.value === option.value));
66
+ const missingSelectedOptions = values.filter((value)=>!options.some((option)=>option.value === value.value));
67
+ return [
68
+ ...missingSelectedOptions,
69
+ ...selectedOptions,
70
+ ...unselectedOptions
71
+ ];
72
+ }
73
+ return options;
74
+ });
75
+ makeObservable(this);
76
+ }
74
77
  }
75
- __decorate([
78
+ _ts_decorate([
76
79
  computed,
77
- __metadata("design:type", Object),
78
- __metadata("design:paramtypes", [])
80
+ _ts_metadata("design:type", void 0),
81
+ _ts_metadata("design:paramtypes", [])
79
82
  ], AsyncLazyDropdownState.prototype, "optionsWithValue", null);
83
+
80
84
  //# sourceMappingURL=async-lazy-dropdown-state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"async-lazy-dropdown-state.js","sourceRoot":"","sources":["../../src/async-lazy-dropdown-state/async-lazy-dropdown-state.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE3D,OAAO,EAAkB,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIlE,MAAM,OAAO,sBAAsD,SAAQ,aAAgB;IAGvF,YAAY,WAAwB;QAChC,KAAK,CAAC;YACF,UAAU,EAAE,IAAI,eAAe,CAAC;gBAC5B,GAAG,EAAE,KAAK,IAAI,EAAE;oBACZ,MAAM,MAAM,GAAU,MAAM,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;oBAErD,OAAO;wBACH,IAAI,EAAE,MAAM;wBACZ,KAAK,EAAE,MAAM,CAAC,MAAM;qBACvB,CAAC;gBACN,CAAC;aACJ,CAAC;YACF,IAAI,EAAE,IAAI;SACb,CAAC,CAAC;QAfC;;;;mBAAwC,EAAE;WAAC;QAyBnD;;;;mBAAW,CAAC,KAAsC,EAAE,EAAE;gBAClD,MAAM,YAAY,GAAG,KAAK,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;gBAEzF,IAAI,CAAC,UAAU,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YACnF,CAAC;WAAC;QAiBM;;;;mBAAsB,GAAG,EAAE;gBAC/B,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;gBAElC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;oBACf,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC;oBAE/B,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,CAClC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CACjE,CAAC;oBAEF,MAAM,iBAAiB,GAAG,OAAO,CAAC,MAAM,CACpC,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAChE,CAAC;oBAEF,MAAM,sBAAsB,GAAG,MAAM,CAAC,MAAM,CACxC,KAAK,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,CAAC,CACjE,CAAC;oBACF,OAAO,CAAC,GAAG,sBAAsB,EAAE,GAAG,eAAe,EAAE,GAAG,iBAAiB,CAAC,CAAC;gBACjF,CAAC;gBAED,OAAO,OAAO,CAAC;YACnB,CAAC;WAAC;QAlDE,cAAc,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,IACI,gBAAgB;QAChB,OAAO,IAAI,CAAC,mBAAmB,EAAE,CAAC;IACtC,CAAC;IAQD,cAAc,CAAC,WAAwB;QACnC,IAAI,CAAC,aAAa,CACd,IAAI,eAAe,CAAC;YAChB,GAAG,EAAE,KAAK,IAAI,EAAE;gBACZ,MAAM,MAAM,GAAU,MAAM,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAErD,OAAO;oBACH,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE,MAAM,CAAC,MAAM;iBACvB,CAAC;YACN,CAAC;SACJ,CAAC,CACL,CAAC;IACN,CAAC;CAwBJ;AAhDG;IAAC,QAAQ;;;8DAGR"}
1
+ {"version":3,"sources":["../../src/async-lazy-dropdown-state/async-lazy-dropdown-state.ts"],"sourcesContent":["import { computed, makeObservable } from 'mobx';\nimport { AsyncDataSource } from '@servicetitan/data-query';\nimport { AnvilSelectOptionsProps, AnvilSelectPropsStrict } from '@servicetitan/design-system';\nimport { DropdownOption, DropdownState } from '../dropdown-state';\n\nexport type DataFetcher = (searchQuery: string) => Promise<DropdownOption<any>[]>;\n\nexport class AsyncLazyDropdownState<T extends DropdownOption<any>> extends DropdownState<T> {\n private arrayValue: AnvilSelectOptionsProps[] = [];\n\n constructor(dataFetcher: DataFetcher) {\n super({\n dataSource: new AsyncDataSource({\n get: async () => {\n const result: any[] = await dataFetcher(this.search);\n\n return {\n data: result,\n total: result.length,\n };\n },\n }),\n lazy: true,\n });\n\n makeObservable(this);\n }\n\n @computed\n get optionsWithValue() {\n return this.getOptionsWithValue();\n }\n\n onChange = (value: AnvilSelectPropsStrict['value']) => {\n const isEmptyValue = value === undefined || (Array.isArray(value) && value.length === 0);\n\n this.arrayValue = isEmptyValue ? [] : !Array.isArray(value) ? [value!] : value;\n };\n\n setDataFetcher(dataFetcher: DataFetcher) {\n this.setDataSource(\n new AsyncDataSource({\n get: async () => {\n const result: any[] = await dataFetcher(this.search);\n\n return {\n data: result,\n total: result.length,\n };\n },\n })\n );\n }\n\n private getOptionsWithValue = () => {\n const options = [...this.options];\n\n if (!this.search) {\n const values = this.arrayValue;\n\n const selectedOptions = options.filter(\n option => !!values.find(value => value.value === option.value)\n );\n\n const unselectedOptions = options.filter(\n option => !values.find(value => value.value === option.value)\n );\n\n const missingSelectedOptions = values.filter(\n value => !options.some(option => option.value === value.value)\n );\n return [...missingSelectedOptions, ...selectedOptions, ...unselectedOptions];\n }\n\n return options;\n };\n}\n"],"names":["computed","makeObservable","AsyncDataSource","DropdownState","AsyncLazyDropdownState","optionsWithValue","getOptionsWithValue","setDataFetcher","dataFetcher","setDataSource","get","result","search","data","total","length","constructor","dataSource","lazy","arrayValue","onChange","value","isEmptyValue","undefined","Array","isArray","options","values","selectedOptions","filter","option","find","unselectedOptions","missingSelectedOptions","some"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,QAAQ,EAAEC,cAAc,QAAQ,OAAO;AAChD,SAASC,eAAe,QAAQ,2BAA2B;AAE3D,SAAyBC,aAAa,QAAQ,oBAAoB;AAIlE,OAAO,MAAMC,+BAA8DD;IAqBvE,IACIE,mBAAmB;QACnB,OAAO,IAAI,CAACC,mBAAmB;IACnC;IAQAC,eAAeC,WAAwB,EAAE;QACrC,IAAI,CAACC,aAAa,CACd,IAAIP,gBAAgB;YAChBQ,KAAK;gBACD,MAAMC,SAAgB,MAAMH,YAAY,IAAI,CAACI,MAAM;gBAEnD,OAAO;oBACHC,MAAMF;oBACNG,OAAOH,OAAOI,MAAM;gBACxB;YACJ;QACJ;IAER;IA1CAC,YAAYR,WAAwB,CAAE;QAClC,KAAK,CAAC;YACFS,YAAY,IAAIf,gBAAgB;gBAC5BQ,KAAK;oBACD,MAAMC,SAAgB,MAAMH,YAAY,IAAI,CAACI,MAAM;oBAEnD,OAAO;wBACHC,MAAMF;wBACNG,OAAOH,OAAOI,MAAM;oBACxB;gBACJ;YACJ;YACAG,MAAM;QACV,IAfJ,uBAAQC,cAAwC,EAAE,GAyBlDC,uBAAAA,YAAW,CAACC;YACR,MAAMC,eAAeD,UAAUE,aAAcC,MAAMC,OAAO,CAACJ,UAAUA,MAAMN,MAAM,KAAK;YAEtF,IAAI,CAACI,UAAU,GAAGG,eAAe,EAAE,GAAG,CAACE,MAAMC,OAAO,CAACJ,SAAS;gBAACA;aAAO,GAAGA;QAC7E,IAiBA,uBAAQf,uBAAsB;YAC1B,MAAMoB,UAAU;mBAAI,IAAI,CAACA,OAAO;aAAC;YAEjC,IAAI,CAAC,IAAI,CAACd,MAAM,EAAE;gBACd,MAAMe,SAAS,IAAI,CAACR,UAAU;gBAE9B,MAAMS,kBAAkBF,QAAQG,MAAM,CAClCC,CAAAA,SAAU,CAAC,CAACH,OAAOI,IAAI,CAACV,CAAAA,QAASA,MAAMA,KAAK,KAAKS,OAAOT,KAAK;gBAGjE,MAAMW,oBAAoBN,QAAQG,MAAM,CACpCC,CAAAA,SAAU,CAACH,OAAOI,IAAI,CAACV,CAAAA,QAASA,MAAMA,KAAK,KAAKS,OAAOT,KAAK;gBAGhE,MAAMY,yBAAyBN,OAAOE,MAAM,CACxCR,CAAAA,QAAS,CAACK,QAAQQ,IAAI,CAACJ,CAAAA,SAAUA,OAAOT,KAAK,KAAKA,MAAMA,KAAK;gBAEjE,OAAO;uBAAIY;uBAA2BL;uBAAoBI;iBAAkB;YAChF;YAEA,OAAON;QACX;QAlDIzB,eAAe,IAAI;IACvB;AAkDJ"}
@@ -1,3 +1,4 @@
1
1
  export * from './async-lazy-dropdown-state';
2
2
  export * from './use-async-lazy-dropdown-state';
3
+
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/async-lazy-dropdown-state/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iCAAiC,CAAC"}
1
+ {"version":3,"sources":["../../src/async-lazy-dropdown-state/index.ts"],"sourcesContent":["export * from './async-lazy-dropdown-state';\nexport * from './use-async-lazy-dropdown-state';\n"],"names":[],"mappings":"AAAA,cAAc,8BAA8B;AAC5C,cAAc,kCAAkC"}
@@ -1,26 +1,36 @@
1
1
  import { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { AsyncLazyDropdownState } from './async-lazy-dropdown-state';
3
- export const useAsyncLazyDropdownState = (value,
4
- // try memoising dataFetcher
5
- dataFetcher) => {
6
- const [state] = useState(() => new AsyncLazyDropdownState(dataFetcher));
7
- useEffect(() => {
3
+ export const useAsyncLazyDropdownState = (value, // try memoising dataFetcher
4
+ dataFetcher)=>{
5
+ const [state] = useState(()=>new AsyncLazyDropdownState(dataFetcher));
6
+ useEffect(()=>{
8
7
  state.onChange(value);
9
- }, [state, value]);
8
+ }, [
9
+ state,
10
+ value
11
+ ]);
10
12
  const isInitialMount = useRef(true);
11
- useEffect(() => {
13
+ useEffect(()=>{
12
14
  if (isInitialMount.current) {
13
15
  isInitialMount.current = false;
14
- }
15
- else {
16
+ } else {
16
17
  state.setDataFetcher(dataFetcher);
17
18
  }
18
- }, [state, dataFetcher]);
19
- const onOpenChange = useCallback((open) => {
19
+ }, [
20
+ state,
21
+ dataFetcher
22
+ ]);
23
+ const onOpenChange = useCallback((open)=>{
20
24
  if (open) {
21
25
  state.setSearch('');
22
26
  }
23
- }, [state]);
24
- return [state, onOpenChange];
27
+ }, [
28
+ state
29
+ ]);
30
+ return [
31
+ state,
32
+ onOpenChange
33
+ ];
25
34
  };
35
+
26
36
  //# sourceMappingURL=use-async-lazy-dropdown-state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-async-lazy-dropdown-state.js","sourceRoot":"","sources":["../../src/async-lazy-dropdown-state/use-async-lazy-dropdown-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEjE,OAAO,EAAE,sBAAsB,EAAe,MAAM,6BAA6B,CAAC;AAGlF,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACrC,KAAsC;AACtC,4BAA4B;AAC5B,WAAwB,EAC1B,EAAE;IACA,MAAM,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,sBAAsB,CAAC,WAAW,CAAC,CAAC,CAAC;IACxE,SAAS,CAAC,GAAG,EAAE;QACX,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAEnB,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;YACzB,cAAc,CAAC,OAAO,GAAG,KAAK,CAAC;QACnC,CAAC;aAAM,CAAC;YACJ,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;QACtC,CAAC;IACL,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC;IAEzB,MAAM,YAAY,GAAG,WAAW,CAC5B,CAAC,IAAa,EAAE,EAAE;QACd,IAAI,IAAI,EAAE,CAAC;YACP,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QACxB,CAAC;IACL,CAAC,EACD,CAAC,KAAK,CAAC,CACV,CAAC;IAEF,OAAO,CAAC,KAAK,EAAE,YAAY,CAG1B,CAAC;AACN,CAAC,CAAC"}
1
+ {"version":3,"sources":["../../src/async-lazy-dropdown-state/use-async-lazy-dropdown-state.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { AnvilSelectPropsStrict } from '@servicetitan/design-system';\nimport { AsyncLazyDropdownState, DataFetcher } from './async-lazy-dropdown-state';\nimport { DropdownOption } from '../dropdown-state';\n\nexport const useAsyncLazyDropdownState = (\n value: AnvilSelectPropsStrict['value'],\n // try memoising dataFetcher\n dataFetcher: DataFetcher\n) => {\n const [state] = useState(() => new AsyncLazyDropdownState(dataFetcher));\n useEffect(() => {\n state.onChange(value);\n }, [state, value]);\n\n const isInitialMount = useRef(true);\n useEffect(() => {\n if (isInitialMount.current) {\n isInitialMount.current = false;\n } else {\n state.setDataFetcher(dataFetcher);\n }\n }, [state, dataFetcher]);\n\n const onOpenChange = useCallback(\n (open: boolean) => {\n if (open) {\n state.setSearch('');\n }\n },\n [state]\n );\n\n return [state, onOpenChange] as [\n AsyncLazyDropdownState<DropdownOption<any>>,\n (open: boolean) => void,\n ];\n};\n"],"names":["useCallback","useEffect","useRef","useState","AsyncLazyDropdownState","useAsyncLazyDropdownState","value","dataFetcher","state","onChange","isInitialMount","current","setDataFetcher","onOpenChange","open","setSearch"],"mappings":"AAAA,SAASA,WAAW,EAAEC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAQ;AAEjE,SAASC,sBAAsB,QAAqB,8BAA8B;AAGlF,OAAO,MAAMC,4BAA4B,CACrCC,OACA,4BAA4B;AAC5BC;IAEA,MAAM,CAACC,MAAM,GAAGL,SAAS,IAAM,IAAIC,uBAAuBG;IAC1DN,UAAU;QACNO,MAAMC,QAAQ,CAACH;IACnB,GAAG;QAACE;QAAOF;KAAM;IAEjB,MAAMI,iBAAiBR,OAAO;IAC9BD,UAAU;QACN,IAAIS,eAAeC,OAAO,EAAE;YACxBD,eAAeC,OAAO,GAAG;QAC7B,OAAO;YACHH,MAAMI,cAAc,CAACL;QACzB;IACJ,GAAG;QAACC;QAAOD;KAAY;IAEvB,MAAMM,eAAeb,YACjB,CAACc;QACG,IAAIA,MAAM;YACNN,MAAMO,SAAS,CAAC;QACpB;IACJ,GACA;QAACP;KAAM;IAGX,OAAO;QAACA;QAAOK;KAAa;AAIhC,EAAE"}
@@ -1,2 +1,3 @@
1
- export {};
1
+ export { };
2
+
2
3
  //# sourceMappingURL=date-range.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"date-range.js","sourceRoot":"","sources":["../src/date-range.ts"],"names":[],"mappings":""}
1
+ {"version":3,"sources":["../src/date-range.ts"],"sourcesContent":["export interface DateRange {\n from?: Date;\n to?: Date;\n}\n"],"names":[],"mappings":"AAAA,WAGC"}
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useRef, Fragment } from 'react';
3
3
  import { InMemoryDataSource, AsyncDataSource } from '@servicetitan/data-query';
4
- import { AnvilSelect, Text, ButtonGroup, Button, } from '@servicetitan/design-system';
4
+ import { AnvilSelect, Text, ButtonGroup, Button } from '@servicetitan/design-system';
5
5
  import { FieldState } from 'formstate';
6
6
  import { observer } from 'mobx-react';
7
7
  import { DropdownState } from '..';
@@ -12,111 +12,335 @@ function useDropdownState(state) {
12
12
  return useRef(state).current;
13
13
  }
14
14
  const options = [
15
- { value: 1, text: 'Alderaan', diameter: 12500, moons: 0 },
16
- { value: 2, text: 'Bespin', diameter: 118000, moons: 2 },
17
- { value: 3, text: 'Coruscant', diameter: 12240, moons: 4 },
18
- { value: 4, text: 'Dagobah', diameter: 14410, moons: 0 },
19
- { value: 5, text: 'Hoth', diameter: 7200, moons: 3 },
20
- { value: 6, text: 'Kashyyyk', diameter: 12765, moons: 3 },
21
- { value: 7, text: 'Naboo', diameter: 12120, moons: 3 },
22
- { value: 8, text: 'Tatooine', diameter: 10465, moons: 3 },
23
- { value: 9, text: 'Yavin', diameter: 200000, moons: 26 },
15
+ {
16
+ value: 1,
17
+ text: 'Alderaan',
18
+ diameter: 12500,
19
+ moons: 0
20
+ },
21
+ {
22
+ value: 2,
23
+ text: 'Bespin',
24
+ diameter: 118000,
25
+ moons: 2
26
+ },
27
+ {
28
+ value: 3,
29
+ text: 'Coruscant',
30
+ diameter: 12240,
31
+ moons: 4
32
+ },
33
+ {
34
+ value: 4,
35
+ text: 'Dagobah',
36
+ diameter: 14410,
37
+ moons: 0
38
+ },
39
+ {
40
+ value: 5,
41
+ text: 'Hoth',
42
+ diameter: 7200,
43
+ moons: 3
44
+ },
45
+ {
46
+ value: 6,
47
+ text: 'Kashyyyk',
48
+ diameter: 12765,
49
+ moons: 3
50
+ },
51
+ {
52
+ value: 7,
53
+ text: 'Naboo',
54
+ diameter: 12120,
55
+ moons: 3
56
+ },
57
+ {
58
+ value: 8,
59
+ text: 'Tatooine',
60
+ diameter: 10465,
61
+ moons: 3
62
+ },
63
+ {
64
+ value: 9,
65
+ text: 'Yavin',
66
+ diameter: 200000,
67
+ moons: 26
68
+ }
24
69
  ];
25
70
  const optionsWithGroups = [
26
- { value: 1, text: 'Hem Dazon', group: 'Arcona' },
27
- { value: 2, text: 'El-Les', group: 'Arcona' },
28
- { value: 3, text: 'Jheeg', group: 'Arcona' },
29
- { value: 4, text: 'Paploo', group: 'Ewok' },
30
- { value: 5, text: 'Gallo', group: 'Gungan' },
31
- { value: 6, text: 'Lyonie', group: 'Gungan' },
32
- { value: 7, text: 'Tobler Ceel', group: 'Gungan' },
33
- { value: 8, text: 'Ganne', group: 'Gungan' },
34
- { value: 9, text: 'Augara Jowil', group: 'Gungan' },
35
- { value: 10, text: 'Reegesk', group: 'Ranat' },
36
- { value: 11, text: 'Rik-tak', group: 'Ranat' },
37
- { value: 12, text: 'Bahb', group: 'Zeltron' },
38
- { value: 13, text: 'Chantique', group: 'Zeltron' },
39
- { value: 14, text: 'Luxa', group: 'Zeltron' },
40
- { value: 15, text: 'Rahuhl', group: 'Zeltron' },
71
+ {
72
+ value: 1,
73
+ text: 'Hem Dazon',
74
+ group: 'Arcona'
75
+ },
76
+ {
77
+ value: 2,
78
+ text: 'El-Les',
79
+ group: 'Arcona'
80
+ },
81
+ {
82
+ value: 3,
83
+ text: 'Jheeg',
84
+ group: 'Arcona'
85
+ },
86
+ {
87
+ value: 4,
88
+ text: 'Paploo',
89
+ group: 'Ewok'
90
+ },
91
+ {
92
+ value: 5,
93
+ text: 'Gallo',
94
+ group: 'Gungan'
95
+ },
96
+ {
97
+ value: 6,
98
+ text: 'Lyonie',
99
+ group: 'Gungan'
100
+ },
101
+ {
102
+ value: 7,
103
+ text: 'Tobler Ceel',
104
+ group: 'Gungan'
105
+ },
106
+ {
107
+ value: 8,
108
+ text: 'Ganne',
109
+ group: 'Gungan'
110
+ },
111
+ {
112
+ value: 9,
113
+ text: 'Augara Jowil',
114
+ group: 'Gungan'
115
+ },
116
+ {
117
+ value: 10,
118
+ text: 'Reegesk',
119
+ group: 'Ranat'
120
+ },
121
+ {
122
+ value: 11,
123
+ text: 'Rik-tak',
124
+ group: 'Ranat'
125
+ },
126
+ {
127
+ value: 12,
128
+ text: 'Bahb',
129
+ group: 'Zeltron'
130
+ },
131
+ {
132
+ value: 13,
133
+ text: 'Chantique',
134
+ group: 'Zeltron'
135
+ },
136
+ {
137
+ value: 14,
138
+ text: 'Luxa',
139
+ group: 'Zeltron'
140
+ },
141
+ {
142
+ value: 15,
143
+ text: 'Rahuhl',
144
+ group: 'Zeltron'
145
+ }
41
146
  ];
42
- const InMemoryDropdownStateExample = observer(() => {
147
+ const InMemoryDropdownStateExample = observer(()=>{
43
148
  const field = useDropdownField([]);
44
149
  const state = useDropdownState(new DropdownState({
45
- dataSource: new InMemoryDataSource(options),
150
+ dataSource: new InMemoryDataSource(options)
46
151
  }));
47
- const handleChange = (data) => {
48
- field.onChange(Array.isArray(data) ? data : [data]);
152
+ const handleChange = (data)=>{
153
+ field.onChange(Array.isArray(data) ? data : [
154
+ data
155
+ ]);
49
156
  };
50
- const withReset = (handler) => {
51
- return async () => {
157
+ const withReset = (handler)=>{
158
+ return async ()=>{
52
159
  handleChange([]);
53
160
  state.setSearch('');
54
161
  state.setDataSource(null);
55
162
  state.setSearchByGroup(false);
56
- state.setSort([{ field: 'text' }]);
163
+ state.setSort([
164
+ {
165
+ field: 'text'
166
+ }
167
+ ]);
57
168
  state.setFilter(null);
58
- state.setGroup([{ field: 'group' }]);
169
+ state.setGroup([
170
+ {
171
+ field: 'group'
172
+ }
173
+ ]);
59
174
  await Promise.resolve();
60
175
  handler();
61
176
  };
62
177
  };
63
- const withoutGroups = withReset(() => {
178
+ const withoutGroups = withReset(()=>{
64
179
  state.setDataSource(new InMemoryDataSource(options));
65
180
  });
66
- const withGroups = withReset(() => {
181
+ const withGroups = withReset(()=>{
67
182
  state.setDataSource(new InMemoryDataSource(optionsWithGroups));
68
183
  });
69
- const withGroupsAndSearch = withReset(() => {
184
+ const withGroupsAndSearch = withReset(()=>{
70
185
  state.setDataSource(new InMemoryDataSource(optionsWithGroups));
71
186
  state.setSearchByGroup(true);
72
187
  });
73
- const customSort = withReset(() => {
188
+ const customSort = withReset(()=>{
74
189
  state.setDataSource(new InMemoryDataSource(options));
75
- state.setSort([{ field: 'diameter', dir: 'desc' }]);
190
+ state.setSort([
191
+ {
192
+ field: 'diameter',
193
+ dir: 'desc'
194
+ }
195
+ ]);
76
196
  });
77
- const customFilter = withReset(() => {
197
+ const customFilter = withReset(()=>{
78
198
  state.setDataSource(new InMemoryDataSource(options));
79
199
  state.setFilter({
80
200
  logic: 'and',
81
- filters: [{ field: 'diameter', value: 12250, operator: 'gte' }],
201
+ filters: [
202
+ {
203
+ field: 'diameter',
204
+ value: 12250,
205
+ operator: 'gte'
206
+ }
207
+ ]
82
208
  });
83
209
  });
84
- const customGroup = withReset(() => {
210
+ const customGroup = withReset(()=>{
85
211
  state.setDataSource(new InMemoryDataSource(options));
86
- state.setGroup([{ field: 'moons', dir: 'desc' }]);
212
+ state.setGroup([
213
+ {
214
+ field: 'moons',
215
+ dir: 'desc'
216
+ }
217
+ ]);
218
+ });
219
+ return /*#__PURE__*/ _jsxs(Fragment, {
220
+ children: [
221
+ /*#__PURE__*/ _jsx(AnvilSelect, {
222
+ value: field.value,
223
+ onChange: handleChange,
224
+ options: state.options,
225
+ search: {
226
+ value: state.search,
227
+ onChange: state.onSearchChange
228
+ },
229
+ multiple: true
230
+ }),
231
+ /*#__PURE__*/ _jsx(Text, {
232
+ size: 4,
233
+ className: "m-t-4 m-b-half",
234
+ children: "Without groups"
235
+ }),
236
+ /*#__PURE__*/ _jsx(Button, {
237
+ onClick: withoutGroups,
238
+ children: "Apply"
239
+ }),
240
+ /*#__PURE__*/ _jsx(Text, {
241
+ size: 4,
242
+ className: "m-t-4 m-b-half",
243
+ children: "With groups"
244
+ }),
245
+ /*#__PURE__*/ _jsxs(ButtonGroup, {
246
+ children: [
247
+ /*#__PURE__*/ _jsx(Button, {
248
+ onClick: withGroups,
249
+ children: "Search in options"
250
+ }),
251
+ /*#__PURE__*/ _jsx(Button, {
252
+ onClick: withGroupsAndSearch,
253
+ children: "Search in options & groups"
254
+ })
255
+ ]
256
+ }),
257
+ /*#__PURE__*/ _jsx(Text, {
258
+ size: 4,
259
+ className: "m-t-4 m-b-half",
260
+ children: "Custom sort"
261
+ }),
262
+ /*#__PURE__*/ _jsx(Button, {
263
+ onClick: customSort,
264
+ children: "Apply"
265
+ }),
266
+ /*#__PURE__*/ _jsx(Text, {
267
+ size: 4,
268
+ className: "m-t-4 m-b-half",
269
+ children: "Custom filter"
270
+ }),
271
+ /*#__PURE__*/ _jsx(Button, {
272
+ onClick: customFilter,
273
+ children: "Apply"
274
+ }),
275
+ /*#__PURE__*/ _jsx(Text, {
276
+ size: 4,
277
+ className: "m-t-4 m-b-half",
278
+ children: "Custom group"
279
+ }),
280
+ /*#__PURE__*/ _jsx(Button, {
281
+ onClick: customGroup,
282
+ children: "Apply"
283
+ })
284
+ ]
87
285
  });
88
- return (_jsxs(Fragment, { children: [_jsx(AnvilSelect, { value: field.value, onChange: handleChange, options: state.options, search: { value: state.search, onChange: state.onSearchChange }, multiple: true }), _jsx(Text, { size: 4, className: "m-t-4 m-b-half", children: "Without groups" }), _jsx(Button, { onClick: withoutGroups, children: "Apply" }), _jsx(Text, { size: 4, className: "m-t-4 m-b-half", children: "With groups" }), _jsxs(ButtonGroup, { children: [_jsx(Button, { onClick: withGroups, children: "Search in options" }), _jsx(Button, { onClick: withGroupsAndSearch, children: "Search in options & groups" })] }), _jsx(Text, { size: 4, className: "m-t-4 m-b-half", children: "Custom sort" }), _jsx(Button, { onClick: customSort, children: "Apply" }), _jsx(Text, { size: 4, className: "m-t-4 m-b-half", children: "Custom filter" }), _jsx(Button, { onClick: customFilter, children: "Apply" }), _jsx(Text, { size: 4, className: "m-t-4 m-b-half", children: "Custom group" }), _jsx(Button, { onClick: customGroup, children: "Apply" })] }));
89
286
  });
90
- const AsyncDropdownStateExample = observer(() => {
287
+ const AsyncDropdownStateExample = observer(()=>{
91
288
  const field = useDropdownField([]);
92
289
  const state = useDropdownState(new DropdownState({
93
290
  dataSource: new AsyncDataSource({
94
- async get({ filter }) {
95
- var _a, _b;
96
- const search = (_b = (_a = filter === null || filter === void 0 ? void 0 : filter.filters) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value;
97
- const result = search
98
- ? options.filter(({ text }) => text.toLowerCase().includes(search.toLowerCase()))
99
- : options;
100
- await new Promise(resolver => setTimeout(resolver, 1000));
291
+ async get ({ filter }) {
292
+ var _filter_filters_, _filter_filters;
293
+ const search = filter === null || filter === void 0 ? void 0 : (_filter_filters = filter.filters) === null || _filter_filters === void 0 ? void 0 : (_filter_filters_ = _filter_filters[0]) === null || _filter_filters_ === void 0 ? void 0 : _filter_filters_.value;
294
+ const result = search ? options.filter(({ text })=>text.toLowerCase().includes(search.toLowerCase())) : options;
295
+ await new Promise((resolver)=>setTimeout(resolver, 1000));
101
296
  return {
102
297
  data: result,
103
- total: result.length,
298
+ total: result.length
104
299
  };
105
- },
300
+ }
106
301
  }, undefined, true),
107
- lazy: true,
302
+ lazy: true
108
303
  }));
109
- const handleChange = (data) => {
110
- field.onChange(Array.isArray(data) ? data : [data]);
304
+ const handleChange = (data)=>{
305
+ field.onChange(Array.isArray(data) ? data : [
306
+ data
307
+ ]);
111
308
  };
112
- const handleOpenChange = (open) => {
309
+ const handleOpenChange = (open)=>{
113
310
  if (open) {
114
311
  state.fetch();
115
312
  }
116
313
  };
117
- return (_jsx(AnvilSelect, { value: field.value, onChange: handleChange, options: state.options, search: { value: state.search, onChange: state.onSearchChange }, onOpenChange: handleOpenChange, loading: state.loading, multiple: true }));
314
+ return /*#__PURE__*/ _jsx(AnvilSelect, {
315
+ value: field.value,
316
+ onChange: handleChange,
317
+ options: state.options,
318
+ search: {
319
+ value: state.search,
320
+ onChange: state.onSearchChange
321
+ },
322
+ onOpenChange: handleOpenChange,
323
+ loading: state.loading,
324
+ multiple: true
325
+ });
118
326
  });
119
- export const DropdownStateExample = () => {
120
- return (_jsxs(Fragment, { children: [_jsx(Text, { size: 5, className: "m-b-half", children: "InMemoryDataSource" }), _jsx(InMemoryDropdownStateExample, {}), _jsx(Text, { size: 5, className: "m-t-5 m-b-half", children: "AsyncDataSource" }), _jsx(AsyncDropdownStateExample, {})] }));
327
+ export const DropdownStateExample = ()=>{
328
+ return /*#__PURE__*/ _jsxs(Fragment, {
329
+ children: [
330
+ /*#__PURE__*/ _jsx(Text, {
331
+ size: 5,
332
+ className: "m-b-half",
333
+ children: "InMemoryDataSource"
334
+ }),
335
+ /*#__PURE__*/ _jsx(InMemoryDropdownStateExample, {}),
336
+ /*#__PURE__*/ _jsx(Text, {
337
+ size: 5,
338
+ className: "m-t-5 m-b-half",
339
+ children: "AsyncDataSource"
340
+ }),
341
+ /*#__PURE__*/ _jsx(AsyncDropdownStateExample, {})
342
+ ]
343
+ });
121
344
  };
345
+
122
346
  //# sourceMappingURL=dropdown-state.js.map