@box/combobox-with-api 2.2.22 → 2.2.24

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.
@@ -1 +1 @@
1
- import{ComboboxWithApi}from"./combobox-with-api.js";import{forwardRef,useCallback,useEffect,useMemo,useState}from"react";import{jsx}from"react/jsx-runtime";import _debounce from"lodash/debounce";var abortErrorName=`AbortError`,ComboboxWithApiContainer=forwardRef((props,forwardedRef)=>{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,ref:forwardedRef})});export{ComboboxWithApiContainer};
1
+ import{ComboboxWithApi}from"./combobox-with-api.js";import{forwardRef,useCallback,useEffect,useMemo,useState}from"react";import{jsx}from"react/jsx-runtime";import _debounce from"lodash/debounce";var abortErrorName=`AbortError`,ComboboxWithApiContainer=forwardRef((props,forwardedRef)=>{let{debounceTime=250,defaultOptions,fetcher,includeTextInputOption=!1,onInputValueChange,transformOptions,...rest}=props,[isLoading,setIsLoading]=useState(!1),[isError,setIsError]=useState(!1),[isEmpty,setIsEmpty]=useState(!1),[queryValue,setQueryValue]=useState(``),[fetchedOptions,setFetchedOptions]=useState([]),hasQuery=queryValue.trim().length>0,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 optionsToRender=defaultOptions?.length>0&&!hasQuery?defaultOptions??[]:fetchedOptions;return transformOptions?transformOptions(optionsToRender,queryValue):optionsToRender},[hasQuery,defaultOptions,fetchedOptions,queryValue,transformOptions]);return jsx(ComboboxWithApi,{...rest,isEmpty:isEmpty||hasQuery&&options.length===0,isError,isLoading,loading:isLoading,onInputValueChange:handleOnInputValueChange,onTryAgain,options,ref:forwardedRef})});export{ComboboxWithApiContainer};
@@ -1,8 +1,8 @@
1
1
  import { ForwardedRef } from 'react';
2
2
  import { OptionValue } from '@box/blueprint-web';
3
- import { ComboboxWithApiContainerProps } from './types';
4
- declare const ComboboxWithApiContainerInner: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue>(props: ComboboxWithApiContainerProps<Multiple, FreeInput, T>, forwardedRef: ForwardedRef<HTMLInputElement>) => import("react/jsx-runtime").JSX.Element;
5
- export declare const ComboboxWithApiContainer: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue>(props: ComboboxWithApiContainerProps<Multiple, FreeInput, T> & {
3
+ import { ComboboxWithApiContainerProps, NonSelectableAdditionalOption } from './types';
4
+ declare const ComboboxWithApiContainerInner: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T>(props: ComboboxWithApiContainerProps<Multiple, FreeInput, T, AdditionalOption, TransformedOption>, forwardedRef: ForwardedRef<HTMLInputElement>) => import("react/jsx-runtime").JSX.Element;
5
+ export declare const ComboboxWithApiContainer: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T>(props: ComboboxWithApiContainerProps<Multiple, FreeInput, T, AdditionalOption, TransformedOption> & {
6
6
  ref?: ForwardedRef<HTMLInputElement>;
7
7
  }) => ReturnType<typeof ComboboxWithApiContainerInner>;
8
8
  export {};
@@ -1,8 +1,8 @@
1
1
  import { ForwardedRef } from 'react';
2
2
  import { OptionValue } from '@box/blueprint-web';
3
- import { ComboboxWithApiProps } from './types';
4
- declare const ComboboxWithApiInner: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue>(props: ComboboxWithApiProps<Multiple, FreeInput, T>, forwardedRef: ForwardedRef<HTMLInputElement>) => import("react/jsx-runtime").JSX.Element;
5
- export declare const ComboboxWithApi: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue>(props: ComboboxWithApiProps<Multiple, FreeInput, T> & {
3
+ import { ComboboxWithApiProps, NonSelectableAdditionalOption } from './types';
4
+ declare const ComboboxWithApiInner: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T>(props: ComboboxWithApiProps<Multiple, FreeInput, T, AdditionalOption, TransformedOption>, forwardedRef: ForwardedRef<HTMLInputElement>) => import("react/jsx-runtime").JSX.Element;
5
+ export declare const ComboboxWithApi: <Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T>(props: ComboboxWithApiProps<Multiple, FreeInput, T, AdditionalOption, TransformedOption> & {
6
6
  ref?: ForwardedRef<HTMLInputElement>;
7
7
  }) => ReturnType<typeof ComboboxWithApiInner>;
8
8
  export {};
@@ -1,5 +1,10 @@
1
- import { ComboboxProps, OptionValue } from '@box/blueprint-web';
2
- export type ComboboxWithApiProps<Multiple extends boolean, FreeInput extends boolean, T extends OptionValue> = Omit<ComboboxProps<Multiple, FreeInput, T>, 'error' | 'hideSelectedOptions'> & Required<Pick<ComboboxProps<Multiple, FreeInput, T>, 'value' | 'onValueChange' | 'loadingAriaLabel'>> & {
1
+ import { ComboboxProps, ComboboxValue, OptionValue } from '@box/blueprint-web';
2
+ export type NonSelectableAdditionalOption = Extract<OptionValue, {
3
+ value: string;
4
+ }> & {
5
+ disabled: true;
6
+ };
7
+ export type ComboboxWithApiProps<Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T> = Omit<ComboboxProps<Multiple, FreeInput, T | TransformedOption | AdditionalOption>, 'error' | 'hideSelectedOptions' | 'onValueChange' | 'value'> & Required<Pick<ComboboxProps<Multiple, FreeInput, T | TransformedOption | AdditionalOption>, 'loadingAriaLabel'>> & {
3
8
  /**
4
9
  * Displays error state
5
10
  */
@@ -16,8 +21,16 @@ export type ComboboxWithApiProps<Multiple extends boolean, FreeInput extends boo
16
21
  * Function called when user clicks try again in error state
17
22
  */
18
23
  onTryAgain: () => void;
24
+ /**
25
+ * Event handler called when selectable value(s) change.
26
+ */
27
+ onValueChange: (value: ComboboxValue<Multiple, FreeInput, T>) => void;
28
+ /**
29
+ * Controlled selectable value(s) of the combobox.
30
+ */
31
+ value: ComboboxValue<Multiple, FreeInput, T>;
19
32
  };
20
- export type ComboboxWithApiContainerProps<Multiple extends boolean, FreeInput extends boolean, T extends OptionValue> = Omit<ComboboxWithApiProps<Multiple, FreeInput, T>, 'options' | 'onTryAgain'> & {
33
+ export type ComboboxWithApiContainerProps<Multiple extends boolean, FreeInput extends boolean, T extends OptionValue, AdditionalOption extends NonSelectableAdditionalOption = never, TransformedOption extends OptionValue = T> = Omit<ComboboxWithApiProps<Multiple, FreeInput, T, AdditionalOption, TransformedOption>, 'options' | 'onTryAgain'> & {
21
34
  /**
22
35
  * Debounce time in ms for the fetcher call. Defaults to 250ms.
23
36
  */
@@ -34,6 +47,10 @@ export type ComboboxWithApiContainerProps<Multiple extends boolean, FreeInput ex
34
47
  * Default options to show when the input is empty
35
48
  */
36
49
  defaultOptions?: T[];
50
+ /**
51
+ * Function to transform options before they are rendered. Additional options must be disabled structural rows.
52
+ */
53
+ transformOptions?: (options: T[], inputValue: string) => Array<T | TransformedOption | AdditionalOption>;
37
54
  };
38
55
  export type ComboboxWithApiErrorProps = {
39
56
  /**
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "@box/combobox-with-api",
3
- "version": "2.2.22",
3
+ "version": "2.2.24",
4
4
  "peerDependencies": {
5
5
  "@ariakit/react": "0.4.21",
6
- "@box/blueprint-web": "^16.20.5",
7
- "@box/blueprint-web-assets": "^5.7.12",
6
+ "@box/blueprint-web": "^16.20.6",
7
+ "@box/blueprint-web-assets": "^5.7.13",
8
8
  "lodash": "^4.17.15",
9
9
  "react": "^17.0.0 || ^18.0.0",
10
10
  "react-dom": "^17.0.0 || ^18.0.0",
11
11
  "react-intl": "^6.4.2"
12
12
  },
13
13
  "dependencies": {
14
- "@box/tree": "^2.3.4",
14
+ "@box/tree": "^2.3.5",
15
15
  "react-accessible-treeview": "2.9.0"
16
16
  },
17
17
  "devDependencies": {
18
- "@box/blueprint-web": "^16.20.5",
19
- "@box/blueprint-web-assets": "^5.7.12",
20
- "@box/storybook-utils": "^1.2.21",
18
+ "@box/blueprint-web": "^16.20.6",
19
+ "@box/blueprint-web-assets": "^5.7.13",
20
+ "@box/storybook-utils": "^1.2.22",
21
21
  "react": "^18.3.0",
22
22
  "react-dom": "^18.3.0"
23
23
  },