@box/combobox-with-api 2.1.36 → 2.1.37

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 (25) hide show
  1. package/dist/chunks/combobox-with-api-error.js +1 -18
  2. package/dist/chunks/combobox-with-api-pagination.js +1 -167
  3. package/dist/chunks/combobox-with-api-pagination2.js +1 -229
  4. package/dist/chunks/combobox-with-api-tree-view.js +1 -423
  5. package/dist/chunks/dynamic-loading-tree.js +1 -293
  6. package/dist/chunks/treeReducer.js +1 -60
  7. package/dist/esm/index.js +1 -6
  8. package/dist/esm/lib/components/combobox-with-api/combobox-with-api-container.js +1 -48
  9. package/dist/esm/lib/components/combobox-with-api/combobox-with-api-error.js +1 -2
  10. package/dist/esm/lib/components/combobox-with-api/combobox-with-api.js +1 -17
  11. package/dist/esm/lib/components/combobox-with-api/messages.js +1 -6
  12. package/dist/esm/lib/components/combobox-with-api-pagination/combobox-with-api-pagination.js +1 -2
  13. package/dist/esm/lib/components/combobox-with-api-pagination/index.js +1 -3
  14. package/dist/esm/lib/components/combobox-with-api-pagination/messages.js +1 -32
  15. package/dist/esm/lib/components/combobox-with-api-pagination/request.js +1 -4
  16. package/dist/esm/lib/components/combobox-with-api-pagination/use-async-list.js +1 -118
  17. package/dist/esm/lib/components/combobox-with-api-pagination/utils/is-abort-error.js +1 -11
  18. package/dist/esm/lib/components/combobox-with-api-pagination/utils/use-controllable-state.js +1 -35
  19. package/dist/esm/lib/components/combobox-with-api-pagination/utils/use-intersection-observer.js +1 -75
  20. package/dist/esm/lib/components/combobox-with-api-tree-view/combobox-with-api-tree-view.js +1 -134
  21. package/dist/esm/lib/components/combobox-with-api-tree-view/dynamic-loading-tree.js +1 -2
  22. package/dist/esm/lib/components/combobox-with-api-tree-view/index.js +1 -3
  23. package/dist/esm/lib/components/combobox-with-api-tree-view/tree-utils.js +1 -18
  24. package/dist/esm/lib/components/combobox-with-api-tree-view/treeReducer.js +1 -2
  25. package/package.json +7 -7
@@ -1,293 +1 @@
1
- import { RootTreeNode as e, getServiceNode as t, hasValidLevel as n } from "../esm/lib/components/combobox-with-api-tree-view/tree-utils.js";
2
- import { n as r, r as i, t as a } from "./treeReducer.js";
3
- import { LoadingIndicator as o } from "@box/blueprint-web";
4
- import { forwardRef as s, useCallback as c, useEffect as l, useMemo as u, useReducer as d, useState as f } from "react";
5
- import { jsx as p, jsxs as m } from "react/jsx-runtime";
6
- import { InlineError as h, Tree as g } from "@box/tree";
7
- import '../styles/dynamic-loading-tree.css';var _ = {
8
- container: "_container_ajuss_27",
9
- disabled: "_disabled_ajuss_50",
10
- label: "_label_ajuss_53",
11
- hiddenLabel: "_hiddenLabel_ajuss_77",
12
- comboboxContainer: "_comboboxContainer_ajuss_80",
13
- withComboboxButtons: "_withComboboxButtons_ajuss_96",
14
- textInputWrapper: "_textInputWrapper_ajuss_118",
15
- textInput: "_textInput_ajuss_118",
16
- comboboxButtons: "_comboboxButtons_ajuss_152",
17
- withChips: "_withChips_ajuss_159",
18
- popover: "_popover_ajuss_167",
19
- loadingIndicator: "_loadingIndicator_ajuss_205",
20
- noResultOption: "_noResultOption_ajuss_208"
21
- }, v = (e) => {
22
- let t = [...e.children], { metadata: n } = e;
23
- return {
24
- ...e,
25
- children: t,
26
- metadata: {
27
- ...e.metadata,
28
- variant: n.variant,
29
- isLoading: e.status === "loading" && !e.children.length,
30
- labelName: n.labelName || "",
31
- labelValue: n.labelValue || ""
32
- }
33
- };
34
- }, y = (e, n) => {
35
- let r;
36
- return e.children.length && e.status === "loading" && (r = t(n.id, n.metadata.displayValue, "loading")), e.status === "error" && (r = t(n.id, n.metadata.displayValue, "reload")), e.status === "success" && e.marker && (r = t(n.id, n.metadata.displayValue, "loadMore")), r;
37
- }, b = /*#__PURE__*/ s(({ ariaLabel: t, onSelect: s, selectedNodes: b, defaultFetcher: x, levels: S, selectableLevels: C, multiselect: w, open: T, searchInput: E, noResultMessage: D, loadingAriaLabel: O }, k) => {
38
- let [A, j] = d(r, a), [M, N] = f({
39
- nodes: [],
40
- selectedIds: []
41
- }), [P, F] = f(!1), [I, L] = f(!1), R = u(() => Math.max(...C), [C]), z = c((e = A) => {
42
- let t = [];
43
- return [...Object.values(e).map((e) => {
44
- let n = v(e), r = y(e, n);
45
- return r && (t.push(r), n.children.push(r.id)), n;
46
- }), ...t];
47
- }, [A]), B = c((e) => {
48
- let t = e.ancestors?.find((t) => t.level === e.level - 1), n = S.find((t) => t.level === e.level);
49
- return e.level === 1 || E === "" ? e.displayValue : {
50
- labelName: n.displayName,
51
- labelValue: t.displayName
52
- };
53
- }, [S, E]), V = c((t) => ({
54
- id: t.value,
55
- name: t.displayValue,
56
- parent: e.id,
57
- isBranch: t.level !== R,
58
- children: [],
59
- metadata: {
60
- variant: "content",
61
- displayValue: t.displayValue,
62
- level: t.level,
63
- selectable: t.selectable,
64
- marker: t.marker,
65
- ancestors: t.ancestors
66
- }
67
- }), [R]), H = c((t, n) => {
68
- j({
69
- type: i.RESET,
70
- payload: {
71
- id: e.id,
72
- items: []
73
- }
74
- }), j({
75
- type: i.LOAD_SUCCESS,
76
- payload: {
77
- id: e.id,
78
- items: t,
79
- marker: n
80
- }
81
- }), F(!1);
82
- }, [j, F]), U = c(() => {
83
- j({
84
- type: i.LOAD_ERROR,
85
- payload: { id: e.id }
86
- }), F(!1), L(!0);
87
- }, [
88
- j,
89
- F,
90
- L
91
- ]), W = c((e, t) => {
92
- let n = new Set(e.map((e) => e.id));
93
- return t.filter((e) => e != null && n.has(e));
94
- }, []);
95
- l(() => {
96
- let e = z(A);
97
- N({
98
- nodes: e,
99
- selectedIds: W(e, b)
100
- });
101
- }, [
102
- W,
103
- z,
104
- b,
105
- A,
106
- I
107
- ]), l(() => {
108
- if (!T) return;
109
- let { signal: t } = new AbortController();
110
- F(!0), L(!1), E ? (j({
111
- type: i.RESET,
112
- payload: {
113
- id: e.id,
114
- items: []
115
- }
116
- }), x({
117
- signal: t,
118
- marker: null,
119
- searchInput: E,
120
- level: null,
121
- onlySelectableOptions: !1,
122
- ancestorId: null
123
- }).then((t) => {
124
- H(t.options.filter((e) => e.selectable).map((t) => {
125
- let { labelName: n, labelValue: r } = B(t);
126
- return {
127
- id: t.value,
128
- name: t.displayValue,
129
- parent: e.id,
130
- isBranch: t.level < R,
131
- children: [],
132
- metadata: {
133
- variant: t.level === 1 ? "content" : "labeled",
134
- displayValue: t.displayValue,
135
- level: t.level,
136
- selectable: t.selectable,
137
- ancestors: t.ancestors,
138
- labelName: n,
139
- labelValue: r
140
- }
141
- };
142
- }), t.marker);
143
- }).catch(() => {
144
- U();
145
- })) : (j({
146
- type: i.RESET,
147
- payload: {
148
- id: e.id,
149
- items: []
150
- }
151
- }), j({
152
- type: i.LOAD_DATA,
153
- payload: { id: e.id }
154
- }), x({
155
- signal: t,
156
- marker: null,
157
- searchInput: "",
158
- level: 1,
159
- onlySelectableOptions: !1,
160
- ancestorId: null
161
- }).then((e) => {
162
- H(e.options.map(V), e.marker);
163
- }).catch(() => {
164
- U();
165
- }));
166
- }, [
167
- x,
168
- V,
169
- T,
170
- E,
171
- S,
172
- R,
173
- B,
174
- H,
175
- U
176
- ]);
177
- let G = c((e) => {
178
- let { isExpanded: t, isDisabled: n, element: r } = e;
179
- if (!t || n) {
180
- j({
181
- type: i.RESET,
182
- payload: {
183
- id: r.id,
184
- items: []
185
- }
186
- });
187
- return;
188
- }
189
- let { level: a } = r.metadata, { id: o, marker: s } = r, { signal: c } = new AbortController();
190
- j({
191
- type: i.LOAD_DATA,
192
- payload: { id: o }
193
- }), x({
194
- signal: c,
195
- marker: s,
196
- searchInput: "",
197
- level: a + 1,
198
- ancestorId: o,
199
- onlySelectableOptions: !1
200
- }).then((e) => {
201
- let t = e.options.map((e) => (r.children.push(e.value), V(e)));
202
- j({
203
- type: i.LOAD_SUCCESS,
204
- payload: {
205
- id: o,
206
- items: t,
207
- marker: e.marker
208
- }
209
- });
210
- }).catch(() => {
211
- j({
212
- type: i.LOAD_ERROR,
213
- payload: { id: o }
214
- });
215
- });
216
- }, [x, V]), K = c((t) => {
217
- I && (L(!1), F(!0));
218
- let n = A[t], { level: r } = n.metadata, { id: a, marker: o } = n, { signal: s } = new AbortController();
219
- j({
220
- type: i.LOAD_DATA,
221
- payload: { id: a }
222
- }), x({
223
- signal: s,
224
- marker: o,
225
- searchInput: E,
226
- level: E === "" ? r + 1 : null,
227
- ancestorId: a === e.id ? null : a.toString(),
228
- onlySelectableOptions: !1
229
- }).then((e) => {
230
- let t = e.options.map(V);
231
- j({
232
- type: i.LOAD_SUCCESS,
233
- payload: {
234
- id: a,
235
- items: t,
236
- marker: e.marker
237
- }
238
- }), F(!1);
239
- }).catch(() => {
240
- j({
241
- type: i.LOAD_ERROR,
242
- payload: {
243
- id: a,
244
- marker: o
245
- }
246
- });
247
- });
248
- }, [
249
- x,
250
- I,
251
- V,
252
- E,
253
- A
254
- ]), { isTreeEmpty: q, focusedId: J } = u(() => {
255
- let t = M.nodes.find(({ id: t }) => t === e.id)?.children ?? [], n = t[0], r = !w && M.selectedIds.length === 0 && n != null;
256
- return {
257
- isTreeEmpty: M.nodes.length === 0 || t.length === 0,
258
- focusedId: r ? n : void 0
259
- };
260
- }, [
261
- w,
262
- M.nodes,
263
- M.selectedIds.length
264
- ]), Y = c((e) => {
265
- let { metadata: t } = e.element;
266
- n(t) && C.includes(t.level) ? s(e) : (e.isSelected = !1, e.treeState.selectedIds.delete(e.element.id));
267
- }, [C, s]);
268
- return I ? /* @__PURE__ */ p(h, { onReload: () => {
269
- K("root");
270
- } }) : /* @__PURE__ */ m("div", { children: [P && /* @__PURE__ */ p("div", {
271
- className: _.noResultOption,
272
- children: /* @__PURE__ */ p(o, {
273
- "aria-label": O,
274
- className: _.loadingIndicator
275
- })
276
- }), q && !P ? /* @__PURE__ */ p("div", {
277
- className: _.noResultOption,
278
- children: D
279
- }) : !P && /* @__PURE__ */ p(g, {
280
- ref: k,
281
- "aria-label": t,
282
- data: M.nodes,
283
- focusedId: J,
284
- multiSelect: w,
285
- onExpand: G,
286
- onLoadData: K,
287
- onNodeSelect: Y,
288
- selectedIds: M.selectedIds,
289
- size: "large",
290
- togglableSelect: !0
291
- })] });
292
- });
293
- export { _ as n, b as t };
1
+ import{RootTreeNode,getServiceNode,hasValidLevel}from"../esm/lib/components/combobox-with-api-tree-view/tree-utils.js";import{n as treeReducer,r as ActionTypes,t as initialTreeState}from"./treeReducer.js";import{LoadingIndicator}from"@box/blueprint-web";import{forwardRef,useCallback,useEffect,useMemo,useReducer,useState}from"react";import{jsx,jsxs}from"react/jsx-runtime";import{InlineError,Tree}from"@box/tree";import '../styles/dynamic-loading-tree.css';var combobox_with_api_tree_module_default={container:`_container_ajuss_27`,disabled:`_disabled_ajuss_50`,label:`_label_ajuss_53`,hiddenLabel:`_hiddenLabel_ajuss_77`,comboboxContainer:`_comboboxContainer_ajuss_80`,withComboboxButtons:`_withComboboxButtons_ajuss_96`,textInputWrapper:`_textInputWrapper_ajuss_118`,textInput:`_textInput_ajuss_118`,comboboxButtons:`_comboboxButtons_ajuss_152`,withChips:`_withChips_ajuss_159`,popover:`_popover_ajuss_167`,loadingIndicator:`_loadingIndicator_ajuss_205`,noResultOption:`_noResultOption_ajuss_208`},getTreeNodeFromItem=item=>{let children=[...item.children],{metadata}=item;return{...item,children,metadata:{...item.metadata,variant:metadata.variant,isLoading:item.status===`loading`&&!item.children.length,labelName:metadata.labelName||``,labelValue:metadata.labelValue||``}}},getServiceNodeFromItem=(item,parentNode)=>{let serviceNode;return item.children.length&&item.status===`loading`&&(serviceNode=getServiceNode(parentNode.id,parentNode.metadata.displayValue,`loading`)),item.status===`error`&&(serviceNode=getServiceNode(parentNode.id,parentNode.metadata.displayValue,`reload`)),item.status===`success`&&item.marker&&(serviceNode=getServiceNode(parentNode.id,parentNode.metadata.displayValue,`loadMore`)),serviceNode},DynamicLoadingTree=forwardRef(({ariaLabel,onSelect,selectedNodes,defaultFetcher,levels,selectableLevels,multiselect,open,searchInput,noResultMessage,loadingAriaLabel},forwardedRef)=>{let[treeState,dispatch]=useReducer(treeReducer,initialTreeState),[treeData,setTreeData]=useState({nodes:[],selectedIds:[]}),[isLoading,setIsLoading]=useState(!1),[isTreeError,setIsTreeError]=useState(!1),maxSelectableLevel=useMemo(()=>Math.max(...selectableLevels),[selectableLevels]),getTreeDataFromState=useCallback((rawState=treeState)=>{let serviceNodes=[];return[...Object.values(rawState).map(item=>{let node=getTreeNodeFromItem(item),serviceNode=getServiceNodeFromItem(item,node);return serviceNode&&(serviceNodes.push(serviceNode),node.children.push(serviceNode.id)),node}),...serviceNodes]},[treeState]),getTreeNodeName=useCallback(option=>{let parentOption=option.ancestors?.find(a=>a.level===option.level-1),levelDesc=levels.find(l=>l.level===option.level);return option.level===1||searchInput===``?option.displayValue:{labelName:levelDesc.displayName,labelValue:parentOption.displayName}},[levels,searchInput]),mapFetcherOptionToStateItem=useCallback(option=>({id:option.value,name:option.displayValue,parent:RootTreeNode.id,isBranch:option.level!==maxSelectableLevel,children:[],metadata:{variant:`content`,displayValue:option.displayValue,level:option.level,selectable:option.selectable,marker:option.marker,ancestors:option.ancestors}}),[maxSelectableLevel]),dispatchTreeLoadingSuccess=useCallback((resNodes,resMarker)=>{dispatch({type:ActionTypes.RESET,payload:{id:RootTreeNode.id,items:[]}}),dispatch({type:ActionTypes.LOAD_SUCCESS,payload:{id:RootTreeNode.id,items:resNodes,marker:resMarker}}),setIsLoading(!1)},[dispatch,setIsLoading]),dispatchTreeLoadingError=useCallback(()=>{dispatch({type:ActionTypes.LOAD_ERROR,payload:{id:RootTreeNode.id}}),setIsLoading(!1),setIsTreeError(!0)},[dispatch,setIsLoading,setIsTreeError]),filterValidSelectedIds=useCallback((nodes,selectedIds)=>{let allNodeIds=new Set(nodes.map(node=>node.id));return selectedIds.filter(id=>id!=null&&allNodeIds.has(id))},[]);useEffect(()=>{let nodes=getTreeDataFromState(treeState);setTreeData({nodes,selectedIds:filterValidSelectedIds(nodes,selectedNodes)})},[filterValidSelectedIds,getTreeDataFromState,selectedNodes,treeState,isTreeError]),useEffect(()=>{if(!open)return;let{signal}=new AbortController;setIsLoading(!0),setIsTreeError(!1),searchInput?(dispatch({type:ActionTypes.RESET,payload:{id:RootTreeNode.id,items:[]}}),defaultFetcher({signal,marker:null,searchInput,level:null,onlySelectableOptions:!1,ancestorId:null}).then(res=>{dispatchTreeLoadingSuccess(res.options.filter(option=>option.selectable).map(option=>{let{labelName,labelValue}=getTreeNodeName(option);return{id:option.value,name:option.displayValue,parent:RootTreeNode.id,isBranch:option.level<maxSelectableLevel,children:[],metadata:{variant:option.level===1?`content`:`labeled`,displayValue:option.displayValue,level:option.level,selectable:option.selectable,ancestors:option.ancestors,labelName,labelValue}}}),res.marker)}).catch(()=>{dispatchTreeLoadingError()})):(dispatch({type:ActionTypes.RESET,payload:{id:RootTreeNode.id,items:[]}}),dispatch({type:ActionTypes.LOAD_DATA,payload:{id:RootTreeNode.id}}),defaultFetcher({signal,marker:null,searchInput:``,level:1,onlySelectableOptions:!1,ancestorId:null}).then(res=>{dispatchTreeLoadingSuccess(res.options.map(mapFetcherOptionToStateItem),res.marker)}).catch(()=>{dispatchTreeLoadingError()}))},[defaultFetcher,mapFetcherOptionToStateItem,open,searchInput,levels,maxSelectableLevel,getTreeNodeName,dispatchTreeLoadingSuccess,dispatchTreeLoadingError]);let onExpand=useCallback(item=>{let{isExpanded,isDisabled,element}=item;if(!isExpanded||isDisabled){dispatch({type:ActionTypes.RESET,payload:{id:element.id,items:[]}});return}let{level}=element.metadata,{id,marker}=element,{signal}=new AbortController;dispatch({type:ActionTypes.LOAD_DATA,payload:{id}}),defaultFetcher({signal,marker,searchInput:``,level:level+1,ancestorId:id,onlySelectableOptions:!1}).then(res=>{let resNodes=res.options.map(option=>(element.children.push(option.value),mapFetcherOptionToStateItem(option)));dispatch({type:ActionTypes.LOAD_SUCCESS,payload:{id,items:resNodes,marker:res.marker}})}).catch(()=>{dispatch({type:ActionTypes.LOAD_ERROR,payload:{id}})})},[defaultFetcher,mapFetcherOptionToStateItem]),handleNodeReload=useCallback(nodeId=>{isTreeError&&(setIsTreeError(!1),setIsLoading(!0));let element=treeState[nodeId],{level}=element.metadata,{id,marker}=element,{signal}=new AbortController;dispatch({type:ActionTypes.LOAD_DATA,payload:{id}}),defaultFetcher({signal,marker,searchInput,level:searchInput===``?level+1:null,ancestorId:id===RootTreeNode.id?null:id.toString(),onlySelectableOptions:!1}).then(res=>{let resNodes=res.options.map(mapFetcherOptionToStateItem);dispatch({type:ActionTypes.LOAD_SUCCESS,payload:{id,items:resNodes,marker:res.marker}}),setIsLoading(!1)}).catch(()=>{dispatch({type:ActionTypes.LOAD_ERROR,payload:{id,marker}})})},[defaultFetcher,isTreeError,mapFetcherOptionToStateItem,searchInput,treeState]),{isTreeEmpty,focusedId}=useMemo(()=>{let rootChildren=treeData.nodes.find(({id})=>id===RootTreeNode.id)?.children??[],firstRootChildId=rootChildren[0],shouldForceFocusFirstChild=!multiselect&&treeData.selectedIds.length===0&&firstRootChildId!=null;return{isTreeEmpty:treeData.nodes.length===0||rootChildren.length===0,focusedId:shouldForceFocusFirstChild?firstRootChildId:void 0}},[multiselect,treeData.nodes,treeData.selectedIds.length]),handleNodeSelection=useCallback(nodeSelectProps=>{let{metadata}=nodeSelectProps.element;hasValidLevel(metadata)&&selectableLevels.includes(metadata.level)?onSelect(nodeSelectProps):(nodeSelectProps.isSelected=!1,nodeSelectProps.treeState.selectedIds.delete(nodeSelectProps.element.id))},[selectableLevels,onSelect]);return isTreeError?jsx(InlineError,{onReload:()=>{handleNodeReload(`root`)}}):jsxs(`div`,{children:[isLoading&&jsx(`div`,{className:combobox_with_api_tree_module_default.noResultOption,children:jsx(LoadingIndicator,{"aria-label":loadingAriaLabel,className:combobox_with_api_tree_module_default.loadingIndicator})}),isTreeEmpty&&!isLoading?jsx(`div`,{className:combobox_with_api_tree_module_default.noResultOption,children:noResultMessage}):!isLoading&&jsx(Tree,{ref:forwardedRef,"aria-label":ariaLabel,data:treeData.nodes,focusedId,multiSelect:multiselect,onExpand,onLoadData:handleNodeReload,onNodeSelect:handleNodeSelection,selectedIds:treeData.selectedIds,size:`large`,togglableSelect:!0})]})});export{combobox_with_api_tree_module_default as n,DynamicLoadingTree as t};
@@ -1,60 +1 @@
1
- import { RootTreeNode as e } from "../esm/lib/components/combobox-with-api-tree-view/tree-utils.js";
2
- var t = /* @__PURE__ */ function(e) {
3
- return e.LOAD_DATA = "LOAD_DATA", e.LOAD_ERROR = "LOAD_DATA_ERROR", e.LOAD_SUCCESS = "LOAD_DATA_SUCCESS", e.LOAD_MORE = "LOAD_DATA_SUCCESS", e.RESET = "RESET", e;
4
- }({}), n = (e, n) => {
5
- let { id: r, items: i, marker: a } = n.payload;
6
- switch (n.type) {
7
- case t.LOAD_DATA: return {
8
- ...e,
9
- [r]: {
10
- ...e[r],
11
- status: "loading"
12
- }
13
- };
14
- case t.LOAD_ERROR: return {
15
- ...e,
16
- [r]: {
17
- ...e[r],
18
- marker: a,
19
- status: "error"
20
- }
21
- };
22
- case t.LOAD_SUCCESS: {
23
- let { itemsWithParent: t, itemsIds: n } = i.reduce((e, t) => ({
24
- itemsWithParent: {
25
- ...e.itemsWithParent,
26
- [t.id]: {
27
- ...t,
28
- parent: r,
29
- children: []
30
- }
31
- },
32
- itemsIds: [...e.itemsIds, t.id]
33
- }), {
34
- itemsWithParent: {},
35
- itemsIds: []
36
- });
37
- return {
38
- ...e,
39
- [r]: {
40
- ...e[r],
41
- status: "success",
42
- marker: a,
43
- children: [...e[r].children || [], ...n]
44
- },
45
- ...t
46
- };
47
- }
48
- case t.RESET: return {
49
- ...e,
50
- [r]: {
51
- ...e[r],
52
- status: "success",
53
- marker: a,
54
- children: []
55
- }
56
- };
57
- default: return e;
58
- }
59
- }, r = { [e.id]: e };
60
- export { n, t as r, r as t };
1
+ import{RootTreeNode}from"../esm/lib/components/combobox-with-api-tree-view/tree-utils.js";var ActionTypes=function(ActionTypes){return ActionTypes.LOAD_DATA=`LOAD_DATA`,ActionTypes.LOAD_ERROR=`LOAD_DATA_ERROR`,ActionTypes.LOAD_SUCCESS=`LOAD_DATA_SUCCESS`,ActionTypes.LOAD_MORE=`LOAD_DATA_SUCCESS`,ActionTypes.RESET=`RESET`,ActionTypes}({}),treeReducer=(state,action)=>{let{id,items,marker}=action.payload;switch(action.type){case ActionTypes.LOAD_DATA:return{...state,[id]:{...state[id],status:`loading`}};case ActionTypes.LOAD_ERROR:return{...state,[id]:{...state[id],marker,status:`error`}};case ActionTypes.LOAD_SUCCESS:{let{itemsWithParent,itemsIds}=items.reduce((acc,item)=>({itemsWithParent:{...acc.itemsWithParent,[item.id]:{...item,parent:id,children:[]}},itemsIds:[...acc.itemsIds,item.id]}),{itemsWithParent:{},itemsIds:[]});return{...state,[id]:{...state[id],status:`success`,marker,children:[...state[id].children||[],...itemsIds]},...itemsWithParent}}case ActionTypes.RESET:return{...state,[id]:{...state[id],status:`success`,marker,children:[]}};default:return state}},initialTreeState={[RootTreeNode.id]:RootTreeNode};export{treeReducer as n,ActionTypes as r,initialTreeState as t};
package/dist/esm/index.js CHANGED
@@ -1,6 +1 @@
1
- import { t as e } from "../chunks/combobox-with-api-pagination.js";
2
- import { n as t, r as n, t as r } from "../chunks/combobox-with-api-pagination2.js";
3
- import { ComboboxWithApiTreeView as i } from "./lib/components/combobox-with-api-tree-view/combobox-with-api-tree-view.js";
4
- import { n as a, r as o, t as s } from "../chunks/combobox-with-api-tree-view.js";
5
- import { ComboboxWithApiContainer as c } from "./lib/components/combobox-with-api/combobox-with-api-container.js";
6
- export { c as ComboboxWithApi, e as ComboboxWithApiPagination, i as ComboboxWithApiTreeView, o as apiResponse, s as findNode, r as findState, t as mockFetcher, a as mockTreeFetcher, n as states };
1
+ import{t as ComboboxWithApiPagination}from"../chunks/combobox-with-api-pagination.js";import{n as mockFetcher,r as states,t as findState}from"../chunks/combobox-with-api-pagination2.js";import{ComboboxWithApiTreeView}from"./lib/components/combobox-with-api-tree-view/combobox-with-api-tree-view.js";import{n as mockTreeFetcher,r as apiResponse,t as findNode}from"../chunks/combobox-with-api-tree-view.js";import{ComboboxWithApiContainer}from"./lib/components/combobox-with-api/combobox-with-api-container.js";export{ComboboxWithApiContainer as ComboboxWithApi,ComboboxWithApiPagination,ComboboxWithApiTreeView,apiResponse,findNode,findState,mockFetcher,mockTreeFetcher,states};
@@ -1,48 +1 @@
1
- import { ComboboxWithApi as e } from "./combobox-with-api.js";
2
- import { useCallback as t, useEffect as n, useMemo as r, useState as i } from "react";
3
- import { jsx as a } from "react/jsx-runtime";
4
- import o from "lodash/debounce";
5
- var s = "AbortError", c = (c) => {
6
- let { debounceTime: l = 250, defaultOptions: u, fetcher: d, includeTextInputOption: f = !1, onInputValueChange: p, ...m } = c, [h, g] = i(!1), [_, v] = i(!1), [y, b] = i(!1), [x, S] = i(""), [C, w] = i([]), T = r(() => o(S, l), [l]), E = (e) => {
7
- T(e), p?.(e);
8
- }, D = t(async (e) => {
9
- let t = e.trim();
10
- if (t) {
11
- v(!1), g(!0);
12
- try {
13
- let e = await d(t);
14
- (!(e.length > 0) || typeof e[0] == "string") && f ? w([t.toLocaleLowerCase(), ...e]) : (b(e?.length === 0), w(e));
15
- } catch (e) {
16
- e?.name !== s && v(!0);
17
- } finally {
18
- g(!1);
19
- }
20
- }
21
- }, [d, f]), O = () => {
22
- D(x);
23
- };
24
- n(() => (x.trim().length ? D(x) : (w([]), b(!1)), () => T.cancel()), [
25
- T,
26
- D,
27
- x
28
- ]);
29
- let k = r(() => {
30
- let e = x.trim().length > 0;
31
- return u?.length > 0 && !e ? u : C;
32
- }, [
33
- x,
34
- u,
35
- C
36
- ]);
37
- return /* @__PURE__ */ a(e, {
38
- ...m,
39
- isEmpty: y,
40
- isError: _,
41
- isLoading: h,
42
- loading: h,
43
- onInputValueChange: E,
44
- onTryAgain: O,
45
- options: k
46
- });
47
- };
48
- export { c as ComboboxWithApiContainer };
1
+ import{ComboboxWithApi}from"./combobox-with-api.js";import{useCallback,useEffect,useMemo,useState}from"react";import{jsx}from"react/jsx-runtime";import _debounce from"lodash/debounce";var abortErrorName=`AbortError`,ComboboxWithApiContainer=props=>{let{debounceTime=250,defaultOptions,fetcher,includeTextInputOption=!1,onInputValueChange,...rest}=props,[isLoading,setIsLoading]=useState(!1),[isError,setIsError]=useState(!1),[isEmpty,setIsEmpty]=useState(!1),[queryValue,setQueryValue]=useState(``),[fetchedOptions,setFetchedOptions]=useState([]),debouncedSetQueryValue=useMemo(()=>_debounce(setQueryValue,debounceTime),[debounceTime]),handleOnInputValueChange=inputVal=>{debouncedSetQueryValue(inputVal),onInputValueChange?.(inputVal)},fetchOptions=useCallback(async queryVal=>{let trimmedValue=queryVal.trim();if(trimmedValue){setIsError(!1),setIsLoading(!0);try{let data=await fetcher(trimmedValue);(!(data.length>0)||typeof data[0]==`string`)&&includeTextInputOption?setFetchedOptions([trimmedValue.toLocaleLowerCase(),...data]):(setIsEmpty(data?.length===0),setFetchedOptions(data))}catch(err){err?.name!==abortErrorName&&setIsError(!0)}finally{setIsLoading(!1)}}},[fetcher,includeTextInputOption]),onTryAgain=()=>{fetchOptions(queryValue)};useEffect(()=>(queryValue.trim().length?fetchOptions(queryValue):(setFetchedOptions([]),setIsEmpty(!1)),()=>debouncedSetQueryValue.cancel()),[debouncedSetQueryValue,fetchOptions,queryValue]);let options=useMemo(()=>{let hasQuery=queryValue.trim().length>0;return defaultOptions?.length>0&&!hasQuery?defaultOptions:fetchedOptions},[queryValue,defaultOptions,fetchedOptions]);return jsx(ComboboxWithApi,{...rest,isEmpty,isError,isLoading,loading:isLoading,onInputValueChange:handleOnInputValueChange,onTryAgain,options})};export{ComboboxWithApiContainer};
@@ -1,2 +1 @@
1
- import { t as e } from "../../../../chunks/combobox-with-api-error.js";
2
- export { e as default };
1
+ import{t as ComboboxWithApiError}from"../../../../chunks/combobox-with-api-error.js";export{ComboboxWithApiError as default};
@@ -1,17 +1 @@
1
- import { t as e } from "../../../../chunks/combobox-with-api-error.js";
2
- import { Combobox as t } from "@box/blueprint-web";
3
- import { useMemo as n } from "react";
4
- import { jsx as r } from "react/jsx-runtime";
5
- import i from "lodash/uniqBy";
6
- var a = (a) => {
7
- let { isEmpty: o, isError: s, isLoading: c, noResultMessage: l, onTryAgain: u, options: d, value: f, ...p } = a, m = n(() => Array.isArray(f) ? i([...d, ...f].reverse(), (e) => (typeof e == "string" ? e : e.value).toLocaleLowerCase().trim()).reverse() : d, [d, f]), h;
8
- return h = s && !c ? /* @__PURE__ */ r(e, { onTryAgain: u }) : o && l && !c ? l : void 0, /* @__PURE__ */ r(t, {
9
- ...p,
10
- hideSelectedOptions: !0,
11
- loading: c,
12
- noResultMessage: h,
13
- options: m,
14
- value: f
15
- });
16
- };
17
- export { a as ComboboxWithApi };
1
+ import{t as ComboboxWithApiError}from"../../../../chunks/combobox-with-api-error.js";import{Combobox}from"@box/blueprint-web";import{useMemo}from"react";import{jsx}from"react/jsx-runtime";import _uniqBy from"lodash/uniqBy";var ComboboxWithApi=props=>{let{isEmpty,isError,isLoading,noResultMessage,onTryAgain,options,value,...rest}=props,combinedOptions=useMemo(()=>Array.isArray(value)?_uniqBy([...options,...value].reverse(),item=>(typeof item==`string`?item:item.value).toLocaleLowerCase().trim()).reverse():options,[options,value]),noResultMessageFromState;return noResultMessageFromState=isError&&!isLoading?jsx(ComboboxWithApiError,{onTryAgain}):isEmpty&&noResultMessage&&!isLoading?noResultMessage:void 0,jsx(Combobox,{...rest,hideSelectedOptions:!0,loading:isLoading,noResultMessage:noResultMessageFromState,options:combinedOptions,value})};export{ComboboxWithApi};
@@ -1,6 +1 @@
1
- import { defineMessages as e } from "react-intl";
2
- var t = e({ loadingResultsError: {
3
- id: "comboboxWithApi.apiError",
4
- defaultMessage: "Couldn't load results. Click to try again."
5
- } });
6
- export { t as default };
1
+ import{defineMessages}from"react-intl";var messages=defineMessages({loadingResultsError:{id:`comboboxWithApi.apiError`,defaultMessage:`Couldn't load results. Click to try again.`}});export{messages as default};
@@ -1,2 +1 @@
1
- import { t as e } from "../../../../chunks/combobox-with-api-pagination.js";
2
- export { e as ComboboxWithApiPagination };
1
+ import{t as ComboboxWithApiPagination}from"../../../../chunks/combobox-with-api-pagination.js";export{ComboboxWithApiPagination};
@@ -1,3 +1 @@
1
- import { t as e } from "../../../../chunks/combobox-with-api-pagination.js";
2
- import { n as t, r as n, t as r } from "../../../../chunks/combobox-with-api-pagination2.js";
3
- export { e as ComboboxWithApiPagination, r as findState, t as mockFetcher, n as states };
1
+ import{t as ComboboxWithApiPagination}from"../../../../chunks/combobox-with-api-pagination.js";import{n as mockFetcher,r as states,t as findState}from"../../../../chunks/combobox-with-api-pagination2.js";export{ComboboxWithApiPagination,findState,mockFetcher,states};
@@ -1,32 +1 @@
1
- import { defineMessages as e } from "react-intl";
2
- var t = e({
3
- loadingResultsErrorAction: {
4
- id: "comboboxWithApiPagination.errorAction",
5
- defaultMessage: "Reload"
6
- },
7
- loadingResultsErrorMessage: {
8
- id: "comboboxWithApiPagination.errorMessage",
9
- defaultMessage: "Failed to load options"
10
- },
11
- loadingResultsErrorIconLabel: {
12
- id: "comboboxWithApiPagination.errorIconLabel",
13
- defaultMessage: "Error"
14
- },
15
- loadingAriaLabel: {
16
- id: "comboboxWithApiPagination.loadingAriaLabel",
17
- defaultMessage: "Loading..."
18
- },
19
- loadingMoreAriaLabel: {
20
- id: "comboboxWithApiPagination.loadingMoreAriaLabel",
21
- defaultMessage: "Loading more..."
22
- },
23
- noResults: {
24
- id: "comboboxWithApiPagination.noResults",
25
- defaultMessage: "No results found"
26
- },
27
- clearButtonAriaLabel: {
28
- id: "comboboxWithApiPagination.clearButtonAriaLabel",
29
- defaultMessage: "Clear input and selection"
30
- }
31
- });
32
- export { t as default };
1
+ import{defineMessages}from"react-intl";var messages=defineMessages({loadingResultsErrorAction:{id:`comboboxWithApiPagination.errorAction`,defaultMessage:`Reload`},loadingResultsErrorMessage:{id:`comboboxWithApiPagination.errorMessage`,defaultMessage:`Failed to load options`},loadingResultsErrorIconLabel:{id:`comboboxWithApiPagination.errorIconLabel`,defaultMessage:`Error`},loadingAriaLabel:{id:`comboboxWithApiPagination.loadingAriaLabel`,defaultMessage:`Loading...`},loadingMoreAriaLabel:{id:`comboboxWithApiPagination.loadingMoreAriaLabel`,defaultMessage:`Loading more...`},noResults:{id:`comboboxWithApiPagination.noResults`,defaultMessage:`No results found`},clearButtonAriaLabel:{id:`comboboxWithApiPagination.clearButtonAriaLabel`,defaultMessage:`Clear input and selection`}});export{messages as default};
@@ -1,4 +1 @@
1
- var e = /* @__PURE__ */ function(e) {
2
- return e.NOT_STARTED = "NOT_STARTED", e.IN_PROGRESS = "IN_PROGRESS", e.ERROR = "ERROR", e.SUCCESS = "SUCCESS", e.CANCELLED = "CANCELLED", e;
3
- }({});
4
- export { e as RequestStates };
1
+ var RequestStates=function(RequestStates){return RequestStates.NOT_STARTED=`NOT_STARTED`,RequestStates.IN_PROGRESS=`IN_PROGRESS`,RequestStates.ERROR=`ERROR`,RequestStates.SUCCESS=`SUCCESS`,RequestStates.CANCELLED=`CANCELLED`,RequestStates}({});export{RequestStates};
@@ -1,118 +1 @@
1
- import { RequestStates as e } from "./request.js";
2
- import { isAbortError as t } from "./utils/is-abort-error.js";
3
- import { useReducer as n } from "react";
4
- function r(t, n) {
5
- switch (n.type) {
6
- case "LOAD_MORE": return {
7
- ...t,
8
- abortController: n.abortController,
9
- error: null,
10
- items: [...t.items, ...n.items],
11
- marker: n.marker,
12
- requestState: e.SUCCESS
13
- };
14
- case "RELOAD": return n.abortController === t.abortController ? {
15
- ...t,
16
- abortController: null,
17
- error: null,
18
- items: [...n.items],
19
- marker: n.marker,
20
- requestState: e.SUCCESS,
21
- searchInput: ""
22
- } : t;
23
- case "SEARCH": return {
24
- ...t,
25
- abortController: n.abortController,
26
- error: null,
27
- items: [...n.items],
28
- marker: n.marker,
29
- requestState: e.SUCCESS,
30
- searchInput: n.searchInput ?? t.searchInput
31
- };
32
- case "SET_ERROR": return {
33
- ...t,
34
- items: t.items,
35
- error: n.error,
36
- requestState: e.ERROR
37
- };
38
- case "SET_LOADING": return {
39
- ...t,
40
- requestInitiator: n.requestInitiator,
41
- requestState: e.IN_PROGRESS,
42
- abortController: n.abortController
43
- };
44
- default: throw Error(`Invalid action type "${n.type}"`);
45
- }
46
- }
47
- function i(t, n) {
48
- return t.type === "SEARCH" && n.requestState === e.IN_PROGRESS;
49
- }
50
- function a(t, n) {
51
- return n.requestInitiator === "LOAD_MORE" && n.requestState === e.IN_PROGRESS && t.type === "LOAD_MORE";
52
- }
53
- function o(e, t) {
54
- return i(e, t) || a(e, t);
55
- }
56
- function s(e, t) {
57
- i(e, t) && t.abortController.abort(), a(e, t) && e.abortController?.abort();
58
- }
59
- function c(e, t) {
60
- return a(e, t);
61
- }
62
- async function l(e, n, r, i) {
63
- let a = new AbortController();
64
- if (o(e, r) && s(e, r), !c(e, r)) {
65
- i({
66
- type: "SET_LOADING",
67
- requestInitiator: e.type,
68
- abortController: a
69
- });
70
- try {
71
- let t = await n({
72
- items: r.items.slice(),
73
- signal: a.signal,
74
- marker: e.type === "LOAD_MORE" ? r.marker : null,
75
- searchInput: e.searchInput ?? r.searchInput
76
- }), o = t.searchInput ?? e.searchInput ?? r.searchInput;
77
- i({
78
- type: e.type,
79
- ...t,
80
- searchInput: o,
81
- abortController: a
82
- });
83
- } catch (e) {
84
- if (t(e)) return;
85
- i({
86
- type: "SET_ERROR",
87
- error: e,
88
- abortController: a
89
- });
90
- }
91
- }
92
- }
93
- function u(t) {
94
- let { load: i } = t, [a, o] = n(r, {
95
- error: null,
96
- items: [],
97
- requestInitiator: null,
98
- requestState: e.NOT_STARTED,
99
- searchInput: ""
100
- });
101
- return {
102
- hasError: a.requestState === e.ERROR,
103
- hasNextPage: a.requestState === e.ERROR ? !1 : a.marker != null,
104
- isEmpty: a.items.length === 0,
105
- isLoading: a.requestState === e.IN_PROGRESS,
106
- isReloading: a.requestState === e.IN_PROGRESS && (a.requestInitiator === "RELOAD" || a.requestInitiator === "SEARCH"),
107
- items: a.items,
108
- loadMore: () => l({ type: "LOAD_MORE" }, i, a, o),
109
- reload: () => l({ type: "RELOAD" }, i, a, o),
110
- requestInitiator: a.requestInitiator,
111
- searchInput: a.searchInput,
112
- search: (e) => l({
113
- type: "SEARCH",
114
- searchInput: e
115
- }, i, a, o)
116
- };
117
- }
118
- export { u as useAsyncList };
1
+ import{RequestStates}from"./request.js";import{isAbortError}from"./utils/is-abort-error.js";import{useReducer}from"react";function reducer(data,action){switch(action.type){case`LOAD_MORE`:return{...data,abortController:action.abortController,error:null,items:[...data.items,...action.items],marker:action.marker,requestState:RequestStates.SUCCESS};case`RELOAD`:return action.abortController===data.abortController?{...data,abortController:null,error:null,items:[...action.items],marker:action.marker,requestState:RequestStates.SUCCESS,searchInput:``}:data;case`SEARCH`:return{...data,abortController:action.abortController,error:null,items:[...action.items],marker:action.marker,requestState:RequestStates.SUCCESS,searchInput:action.searchInput??data.searchInput};case`SET_ERROR`:return{...data,items:data.items,error:action.error,requestState:RequestStates.ERROR};case`SET_LOADING`:return{...data,requestInitiator:action.requestInitiator,requestState:RequestStates.IN_PROGRESS,abortController:action.abortController};default:throw Error(`Invalid action type "${action.type}"`)}}function hasPendingSearch(action,data){return action.type===`SEARCH`&&data.requestState===RequestStates.IN_PROGRESS}function hasPendingLoadMore(action,data){return data.requestInitiator===`LOAD_MORE`&&data.requestState===RequestStates.IN_PROGRESS&&action.type===`LOAD_MORE`}function shouldAbort(action,data){return hasPendingSearch(action,data)||hasPendingLoadMore(action,data)}function handleAbort(action,data){hasPendingSearch(action,data)&&data.abortController.abort(),hasPendingLoadMore(action,data)&&action.abortController?.abort()}function shouldPreventDispatch(action,data){return hasPendingLoadMore(action,data)}async function fetchData(action,fn,data,dispatch){let abortController=new AbortController;if(shouldAbort(action,data)&&handleAbort(action,data),!shouldPreventDispatch(action,data)){dispatch({type:`SET_LOADING`,requestInitiator:action.type,abortController});try{let response=await fn({items:data.items.slice(),signal:abortController.signal,marker:action.type===`LOAD_MORE`?data.marker:null,searchInput:action.searchInput??data.searchInput}),searchInput=response.searchInput??action.searchInput??data.searchInput;dispatch({type:action.type,...response,searchInput,abortController})}catch(error){if(isAbortError(error))return;dispatch({type:`SET_ERROR`,error,abortController})}}}function useAsyncList(options){let{load}=options,[data,dispatch]=useReducer(reducer,{error:null,items:[],requestInitiator:null,requestState:RequestStates.NOT_STARTED,searchInput:``});return{hasError:data.requestState===RequestStates.ERROR,hasNextPage:data.requestState===RequestStates.ERROR?!1:data.marker!=null,isEmpty:data.items.length===0,isLoading:data.requestState===RequestStates.IN_PROGRESS,isReloading:data.requestState===RequestStates.IN_PROGRESS&&(data.requestInitiator===`RELOAD`||data.requestInitiator===`SEARCH`),items:data.items,loadMore:()=>fetchData({type:`LOAD_MORE`},load,data,dispatch),reload:()=>fetchData({type:`RELOAD`},load,data,dispatch),requestInitiator:data.requestInitiator,searchInput:data.searchInput,search:searchInput=>fetchData({type:`SEARCH`,searchInput},load,data,dispatch)}}export{useAsyncList};
@@ -1,11 +1 @@
1
- var e = "AbortError";
2
- function t(e) {
3
- return !!(e && e.__CANCEL__);
4
- }
5
- function n(t) {
6
- return !!(t && typeof t == "object" && t.name === e);
7
- }
8
- function r(e) {
9
- return t(e) || n(e);
10
- }
11
- export { r as isAbortError };
1
+ var ABORT_ERROR_NAME=`AbortError`;function isAxiosCancelError(value){return!!(value&&value.__CANCEL__)}function isFetchAbortError(value){return!!(value&&typeof value==`object`&&value.name===ABORT_ERROR_NAME)}function isAbortError(error){return isAxiosCancelError(error)||isFetchAbortError(error)}export{isAbortError};