@yamada-ui/autocomplete 1.6.0-dev-20241011153139 → 1.6.0-dev-20241011162716
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/autocomplete-context.d.mts +2 -4
- package/dist/autocomplete-context.d.ts +2 -4
- package/dist/autocomplete-context.js.map +1 -1
- package/dist/autocomplete-context.mjs +1 -1
- package/dist/autocomplete-create.js.map +1 -1
- package/dist/autocomplete-create.mjs +4 -4
- package/dist/autocomplete-empty.js.map +1 -1
- package/dist/autocomplete-empty.mjs +4 -4
- package/dist/autocomplete-icon.js.map +1 -1
- package/dist/autocomplete-icon.mjs +2 -2
- package/dist/autocomplete-list.js.map +1 -1
- package/dist/autocomplete-list.mjs +3 -3
- package/dist/autocomplete-option-group.js.map +1 -1
- package/dist/autocomplete-option-group.mjs +3 -3
- package/dist/autocomplete-option.js +11 -14
- package/dist/autocomplete-option.js.map +1 -1
- package/dist/autocomplete-option.mjs +4 -4
- package/dist/autocomplete.d.mts +0 -1
- package/dist/autocomplete.d.ts +0 -1
- package/dist/autocomplete.js +51 -88
- package/dist/autocomplete.js.map +1 -1
- package/dist/autocomplete.mjs +12 -12
- package/dist/{chunk-WERCEZNJ.mjs → chunk-352YZTOO.mjs} +39 -62
- package/dist/chunk-352YZTOO.mjs.map +1 -0
- package/dist/{chunk-V6YCG642.mjs → chunk-6OTUOEYS.mjs} +2 -2
- package/dist/{chunk-67X7JKWL.mjs → chunk-7QON6ZHO.mjs} +2 -2
- package/dist/{chunk-7LZUXESV.mjs → chunk-HUY7Q3WA.mjs} +14 -25
- package/dist/chunk-HUY7Q3WA.mjs.map +1 -0
- package/dist/{chunk-K5HCHHCJ.mjs → chunk-J6UKIKNL.mjs} +4 -4
- package/dist/{chunk-AIBJWPRA.mjs → chunk-KWIJUSLC.mjs} +4 -4
- package/dist/{chunk-Q33EMTO2.mjs → chunk-OY5JVCQQ.mjs} +14 -24
- package/dist/chunk-OY5JVCQQ.mjs.map +1 -0
- package/dist/{chunk-2HJUFFCU.mjs → chunk-PKRO525W.mjs} +4 -4
- package/dist/{chunk-VTBUO5SK.mjs → chunk-WJ4P4B2E.mjs} +3 -3
- package/dist/{chunk-4SBNPEK7.mjs → chunk-WNN655FX.mjs} +3 -3
- package/dist/{chunk-TR4X4EEG.mjs → chunk-X2PFHBZY.mjs} +2 -2
- package/dist/{chunk-WDQYM4FP.mjs → chunk-ZNO5RUVT.mjs} +1 -1
- package/dist/chunk-ZNO5RUVT.mjs.map +1 -0
- package/dist/{chunk-6OLHNK7O.mjs → chunk-ZXALWDKQ.mjs} +14 -18
- package/dist/chunk-ZXALWDKQ.mjs.map +1 -0
- package/dist/index.d.mts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +57 -105
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -13
- package/dist/multi-autocomplete.d.mts +0 -1
- package/dist/multi-autocomplete.d.ts +0 -1
- package/dist/multi-autocomplete.js +51 -89
- package/dist/multi-autocomplete.js.map +1 -1
- package/dist/multi-autocomplete.mjs +12 -12
- package/dist/use-autocomplete-list.js.map +1 -1
- package/dist/use-autocomplete-list.mjs +2 -2
- package/dist/use-autocomplete-option-group.js.map +1 -1
- package/dist/use-autocomplete-option-group.mjs +2 -2
- package/dist/use-autocomplete-option.js +11 -14
- package/dist/use-autocomplete-option.js.map +1 -1
- package/dist/use-autocomplete-option.mjs +2 -2
- package/dist/use-autocomplete.d.mts +11 -16
- package/dist/use-autocomplete.d.ts +11 -16
- package/dist/use-autocomplete.js +45 -72
- package/dist/use-autocomplete.js.map +1 -1
- package/dist/use-autocomplete.mjs +7 -7
- package/package.json +13 -13
- package/dist/chunk-6OLHNK7O.mjs.map +0 -1
- package/dist/chunk-7LZUXESV.mjs.map +0 -1
- package/dist/chunk-Q33EMTO2.mjs.map +0 -1
- package/dist/chunk-WDQYM4FP.mjs.map +0 -1
- package/dist/chunk-WERCEZNJ.mjs.map +0 -1
- /package/dist/{chunk-V6YCG642.mjs.map → chunk-6OTUOEYS.mjs.map} +0 -0
- /package/dist/{chunk-67X7JKWL.mjs.map → chunk-7QON6ZHO.mjs.map} +0 -0
- /package/dist/{chunk-K5HCHHCJ.mjs.map → chunk-J6UKIKNL.mjs.map} +0 -0
- /package/dist/{chunk-AIBJWPRA.mjs.map → chunk-KWIJUSLC.mjs.map} +0 -0
- /package/dist/{chunk-2HJUFFCU.mjs.map → chunk-PKRO525W.mjs.map} +0 -0
- /package/dist/{chunk-VTBUO5SK.mjs.map → chunk-WJ4P4B2E.mjs.map} +0 -0
- /package/dist/{chunk-4SBNPEK7.mjs.map → chunk-WNN655FX.mjs.map} +0 -0
- /package/dist/{chunk-TR4X4EEG.mjs.map → chunk-X2PFHBZY.mjs.map} +0 -0
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/use-autocomplete-list.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { PropGetter } from \"@yamada-ui/core\"\nimport { mergeRefs, useUpdateEffect } from \"@yamada-ui/utils\"\nimport { useCallback, useEffect, useId, useRef } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendantsContext,\n} from \"./autocomplete-context\"\n\nexport const useAutocompleteList = () => {\n const uuid = useId()\n const { focusedIndex, isOpen, listRef, rebirthOptions } =\n useAutocompleteContext()\n\n const descendants = useAutocompleteDescendantsContext()\n\n const beforeFocusedIndex = useRef<number>(-1)\n const selectedValue = descendants.value(focusedIndex)\n\n const onAnimationComplete = useCallback(() => {\n if (!isOpen) rebirthOptions(false)\n }, [isOpen, rebirthOptions])\n\n useEffect(() => {\n if (!listRef.current || !selectedValue) return\n\n if (beforeFocusedIndex.current === selectedValue.index) return\n\n const parent = listRef.current\n const child = selectedValue.node\n\n const parentHeight = parent.clientHeight\n const viewTop = parent.scrollTop\n const viewBottom = viewTop + parentHeight\n\n const childHeight = child.clientHeight\n const childTop = child.offsetTop\n const childBottom = childTop + childHeight\n\n const isInView = viewTop <= childTop && childBottom <= viewBottom\n\n const isScrollBottom = beforeFocusedIndex.current < selectedValue.index\n\n if (!isInView) {\n if (childBottom <= parentHeight) {\n listRef.current.scrollTo({ top: 0 })\n } else {\n if (!isScrollBottom) {\n listRef.current.scrollTo({ top: childTop + 1 })\n } else {\n listRef.current.scrollTo({ top: childBottom - parentHeight })\n }\n }\n }\n\n beforeFocusedIndex.current = selectedValue.index\n }, [listRef, selectedValue])\n\n useUpdateEffect(() => {\n if (!isOpen) beforeFocusedIndex.current = -1\n }, [isOpen])\n\n const getListProps: PropGetter<\"ul\"> = useCallback(\n (props = {}, ref = null) => ({\n id: props.id ?? uuid,\n ref: mergeRefs(listRef, ref),\n as: \"ul\",\n position: \"relative\",\n role: \"listbox\",\n tabIndex: -1,\n ...props,\n }),\n [listRef, uuid],\n )\n\n return {\n getListProps,\n onAnimationComplete,\n }\n}\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type {
|
1
|
+
{"version":3,"sources":["../src/use-autocomplete-list.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { PropGetter } from \"@yamada-ui/core\"\nimport { mergeRefs, useUpdateEffect } from \"@yamada-ui/utils\"\nimport { useCallback, useEffect, useId, useRef } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendantsContext,\n} from \"./autocomplete-context\"\n\nexport const useAutocompleteList = () => {\n const uuid = useId()\n const { focusedIndex, isOpen, listRef, rebirthOptions } =\n useAutocompleteContext()\n\n const descendants = useAutocompleteDescendantsContext()\n\n const beforeFocusedIndex = useRef<number>(-1)\n const selectedValue = descendants.value(focusedIndex)\n\n const onAnimationComplete = useCallback(() => {\n if (!isOpen) rebirthOptions(false)\n }, [isOpen, rebirthOptions])\n\n useEffect(() => {\n if (!listRef.current || !selectedValue) return\n\n if (beforeFocusedIndex.current === selectedValue.index) return\n\n const parent = listRef.current\n const child = selectedValue.node\n\n const parentHeight = parent.clientHeight\n const viewTop = parent.scrollTop\n const viewBottom = viewTop + parentHeight\n\n const childHeight = child.clientHeight\n const childTop = child.offsetTop\n const childBottom = childTop + childHeight\n\n const isInView = viewTop <= childTop && childBottom <= viewBottom\n\n const isScrollBottom = beforeFocusedIndex.current < selectedValue.index\n\n if (!isInView) {\n if (childBottom <= parentHeight) {\n listRef.current.scrollTo({ top: 0 })\n } else {\n if (!isScrollBottom) {\n listRef.current.scrollTo({ top: childTop + 1 })\n } else {\n listRef.current.scrollTo({ top: childBottom - parentHeight })\n }\n }\n }\n\n beforeFocusedIndex.current = selectedValue.index\n }, [listRef, selectedValue])\n\n useUpdateEffect(() => {\n if (!isOpen) beforeFocusedIndex.current = -1\n }, [isOpen])\n\n const getListProps: PropGetter<\"ul\"> = useCallback(\n (props = {}, ref = null) => ({\n id: props.id ?? uuid,\n ref: mergeRefs(listRef, ref),\n as: \"ul\",\n position: \"relative\",\n role: \"listbox\",\n tabIndex: -1,\n ...props,\n }),\n [listRef, uuid],\n )\n\n return {\n getListProps,\n onAnimationComplete,\n }\n}\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type { UseAutocompleteProps } from \"./use-autocomplete\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport { createContext } from \"@yamada-ui/utils\"\n\nexport const {\n DescendantsContextProvider: AutocompleteDescendantsContextProvider,\n useDescendant: useAutocompleteDescendant,\n useDescendants: useAutocompleteDescendants,\n useDescendantsContext: useAutocompleteDescendantsContext,\n} = createDescendant()\n\ninterface AutocompleteContext\n extends Omit<\n UseAutocompleteProps,\n \"defaultValue\" | \"onChange\" | \"onCreate\" | \"value\"\n > {\n focusedIndex: number\n inputRef: RefObject<HTMLInputElement>\n inputValue: string\n isAllSelected: boolean\n isEmpty: boolean\n isHit: boolean\n isOpen: boolean\n label: string | string[] | undefined\n listRef: RefObject<HTMLUListElement>\n pickOptions: (value: string) => void\n rebirthOptions: (runFocus?: boolean) => void\n setFocusedIndex: Dispatch<SetStateAction<number>>\n styles: { [key: string]: CSSUIObject | undefined }\n value: string | string[]\n formControlProps: { [key: string]: any }\n inputProps: DOMAttributes<HTMLInputElement>\n onChange: (newValue: string, runRebirth?: boolean) => void\n onClose: () => void\n onCreate: () => void\n onFocusFirst: () => void\n onFocusLast: () => void\n onFocusNext: (index?: number) => void\n onFocusPrev: (index?: number) => void\n onOpen: () => void\n}\n\nexport const [AutocompleteProvider, useAutocompleteContext] =\n createContext<AutocompleteContext>({\n name: \"AutocompleteContext\",\n errorMessage: `useAutocompleteContext returned is 'undefined'. Seems you forgot to wrap the components in \"<Autocomplete />\" or \"<MultiAutocomplete />\"`,\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,IAAAA,gBAA2C;AAC3C,mBAAsD;;;ACCtD,4BAAiC;AACjC,mBAA8B;AAEvB,IAAM;AAAA,EACX,4BAA4B;AAAA,EAC5B,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,uBAAuB;AACzB,QAAI,wCAAiB;AAiCd,IAAM,CAAC,sBAAsB,sBAAsB,QACxD,4BAAmC;AAAA,EACjC,MAAM;AAAA,EACN,cAAc;AAChB,CAAC;;;ADxCI,IAAM,sBAAsB,MAAM;AACvC,QAAM,WAAO,oBAAM;AACnB,QAAM,EAAE,cAAc,QAAQ,SAAS,eAAe,IACpD,uBAAuB;AAEzB,QAAM,cAAc,kCAAkC;AAEtD,QAAM,yBAAqB,qBAAe,EAAE;AAC5C,QAAM,gBAAgB,YAAY,MAAM,YAAY;AAEpD,QAAM,0BAAsB,0BAAY,MAAM;AAC5C,QAAI,CAAC,OAAQ,gBAAe,KAAK;AAAA,EACnC,GAAG,CAAC,QAAQ,cAAc,CAAC;AAE3B,8BAAU,MAAM;AACd,QAAI,CAAC,QAAQ,WAAW,CAAC,cAAe;AAExC,QAAI,mBAAmB,YAAY,cAAc,MAAO;AAExD,UAAM,SAAS,QAAQ;AACvB,UAAM,QAAQ,cAAc;AAE5B,UAAM,eAAe,OAAO;AAC5B,UAAM,UAAU,OAAO;AACvB,UAAM,aAAa,UAAU;AAE7B,UAAM,cAAc,MAAM;AAC1B,UAAM,WAAW,MAAM;AACvB,UAAM,cAAc,WAAW;AAE/B,UAAM,WAAW,WAAW,YAAY,eAAe;AAEvD,UAAM,iBAAiB,mBAAmB,UAAU,cAAc;AAElE,QAAI,CAAC,UAAU;AACb,UAAI,eAAe,cAAc;AAC/B,gBAAQ,QAAQ,SAAS,EAAE,KAAK,EAAE,CAAC;AAAA,MACrC,OAAO;AACL,YAAI,CAAC,gBAAgB;AACnB,kBAAQ,QAAQ,SAAS,EAAE,KAAK,WAAW,EAAE,CAAC;AAAA,QAChD,OAAO;AACL,kBAAQ,QAAQ,SAAS,EAAE,KAAK,cAAc,aAAa,CAAC;AAAA,QAC9D;AAAA,MACF;AAAA,IACF;AAEA,uBAAmB,UAAU,cAAc;AAAA,EAC7C,GAAG,CAAC,SAAS,aAAa,CAAC;AAE3B,qCAAgB,MAAM;AACpB,QAAI,CAAC,OAAQ,oBAAmB,UAAU;AAAA,EAC5C,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,mBAAiC;AAAA,IACrC,CAAC,QAAQ,CAAC,GAAG,MAAM,SAAM;AA9D7B;AA8DiC;AAAA,QAC3B,KAAI,WAAM,OAAN,YAAY;AAAA,QAChB,SAAK,yBAAU,SAAS,GAAG;AAAA,QAC3B,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,MAAM;AAAA,QACN,UAAU;AAAA,QACV,GAAG;AAAA,MACL;AAAA;AAAA,IACA,CAAC,SAAS,IAAI;AAAA,EAChB;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":["import_utils"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/use-autocomplete-option-group.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { HTMLUIProps, PropGetter } from \"@yamada-ui/core\"\nimport type { CSSProperties } from \"react\"\nimport { layoutStyleProperties } from \"@yamada-ui/core\"\nimport { isArray, splitObject } from \"@yamada-ui/utils\"\nimport { useCallback } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendantsContext,\n} from \"./autocomplete-context\"\n\nexport interface UseAutocompleteOptionGroupProps extends HTMLUIProps<\"ul\"> {\n /**\n * The label of the autocomplete option group.\n */\n label: string\n}\n\nexport const useAutocompleteOptionGroup = ({\n label,\n ...rest\n}: UseAutocompleteOptionGroupProps) => {\n const { omitSelectedValues, value } = useAutocompleteContext()\n\n const isMulti = isArray(value)\n\n const descendants = useAutocompleteDescendantsContext()\n\n const values = descendants.values()\n const selectedValues =\n isMulti && omitSelectedValues\n ? descendants.values(({ node }) =>\n value.includes(node.dataset.value ?? \"\"),\n )\n : []\n const selectedIndexes = selectedValues.map(({ index }) => index)\n const childValues = values.filter(\n ({ index, node }) =>\n node.parentElement?.dataset.label === label &&\n !selectedIndexes.includes(index) &&\n \"target\" in node.dataset,\n )\n\n const isEmpty = !childValues.length\n\n const [containerProps, groupProps] = splitObject(rest, layoutStyleProperties)\n\n const getContainerProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n ...containerProps,\n style: isEmpty ? style : undefined,\n \"data-label\": label,\n role: \"autocomplete-group-container\",\n }\n },\n [containerProps, isEmpty, label],\n )\n\n const getGroupProps: PropGetter<\"ul\"> = useCallback(\n (props = {}, ref = null) => ({\n ref,\n ...props,\n ...groupProps,\n \"data-label\": label,\n role: \"autocomplete-group\",\n }),\n [groupProps, label],\n )\n\n return {\n label,\n getContainerProps,\n getGroupProps,\n }\n}\n\nexport type UseAutocompleteOptionGroupReturn = ReturnType<\n typeof useAutocompleteOptionGroup\n>\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type {
|
1
|
+
{"version":3,"sources":["../src/use-autocomplete-option-group.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { HTMLUIProps, PropGetter } from \"@yamada-ui/core\"\nimport type { CSSProperties } from \"react\"\nimport { layoutStyleProperties } from \"@yamada-ui/core\"\nimport { isArray, splitObject } from \"@yamada-ui/utils\"\nimport { useCallback } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendantsContext,\n} from \"./autocomplete-context\"\n\nexport interface UseAutocompleteOptionGroupProps extends HTMLUIProps<\"ul\"> {\n /**\n * The label of the autocomplete option group.\n */\n label: string\n}\n\nexport const useAutocompleteOptionGroup = ({\n label,\n ...rest\n}: UseAutocompleteOptionGroupProps) => {\n const { omitSelectedValues, value } = useAutocompleteContext()\n\n const isMulti = isArray(value)\n\n const descendants = useAutocompleteDescendantsContext()\n\n const values = descendants.values()\n const selectedValues =\n isMulti && omitSelectedValues\n ? descendants.values(({ node }) =>\n value.includes(node.dataset.value ?? \"\"),\n )\n : []\n const selectedIndexes = selectedValues.map(({ index }) => index)\n const childValues = values.filter(\n ({ index, node }) =>\n node.parentElement?.dataset.label === label &&\n !selectedIndexes.includes(index) &&\n \"target\" in node.dataset,\n )\n\n const isEmpty = !childValues.length\n\n const [containerProps, groupProps] = splitObject(rest, layoutStyleProperties)\n\n const getContainerProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n ...containerProps,\n style: isEmpty ? style : undefined,\n \"data-label\": label,\n role: \"autocomplete-group-container\",\n }\n },\n [containerProps, isEmpty, label],\n )\n\n const getGroupProps: PropGetter<\"ul\"> = useCallback(\n (props = {}, ref = null) => ({\n ref,\n ...props,\n ...groupProps,\n \"data-label\": label,\n role: \"autocomplete-group\",\n }),\n [groupProps, label],\n )\n\n return {\n label,\n getContainerProps,\n getGroupProps,\n }\n}\n\nexport type UseAutocompleteOptionGroupReturn = ReturnType<\n typeof useAutocompleteOptionGroup\n>\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type { UseAutocompleteProps } from \"./use-autocomplete\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport { createContext } from \"@yamada-ui/utils\"\n\nexport const {\n DescendantsContextProvider: AutocompleteDescendantsContextProvider,\n useDescendant: useAutocompleteDescendant,\n useDescendants: useAutocompleteDescendants,\n useDescendantsContext: useAutocompleteDescendantsContext,\n} = createDescendant()\n\ninterface AutocompleteContext\n extends Omit<\n UseAutocompleteProps,\n \"defaultValue\" | \"onChange\" | \"onCreate\" | \"value\"\n > {\n focusedIndex: number\n inputRef: RefObject<HTMLInputElement>\n inputValue: string\n isAllSelected: boolean\n isEmpty: boolean\n isHit: boolean\n isOpen: boolean\n label: string | string[] | undefined\n listRef: RefObject<HTMLUListElement>\n pickOptions: (value: string) => void\n rebirthOptions: (runFocus?: boolean) => void\n setFocusedIndex: Dispatch<SetStateAction<number>>\n styles: { [key: string]: CSSUIObject | undefined }\n value: string | string[]\n formControlProps: { [key: string]: any }\n inputProps: DOMAttributes<HTMLInputElement>\n onChange: (newValue: string, runRebirth?: boolean) => void\n onClose: () => void\n onCreate: () => void\n onFocusFirst: () => void\n onFocusLast: () => void\n onFocusNext: (index?: number) => void\n onFocusPrev: (index?: number) => void\n onOpen: () => void\n}\n\nexport const [AutocompleteProvider, useAutocompleteContext] =\n createContext<AutocompleteContext>({\n name: \"AutocompleteContext\",\n errorMessage: `useAutocompleteContext returned is 'undefined'. Seems you forgot to wrap the components in \"<Autocomplete />\" or \"<MultiAutocomplete />\"`,\n })\n"],"mappings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AA,QACR,UAAU;AAAA,QACV,SAAS;AAAA,QACT,UAAU;AAAA,QACV,YAAY;AAAA,QACZ,OAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL;AAAA,QACA,GAAG;AAAA,QACH,GAAG;AAAA,QACH,OAAO,UAAU,QAAQ;AAAA,QACzB,cAAc;AAAA,QACd,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,CAAC,gBAAgB,SAAS,KAAK;AAAA,EACjC;AAEA,QAAM,oBAAkC;AAAA,IACtC,CAAC,QAAQ,CAAC,GAAG,MAAM,UAAU;AAAA,MAC3B;AAAA,MACA,GAAG;AAAA,MACH,GAAG;AAAA,MACH,cAAc;AAAA,MACd,MAAM;AAAA,IACR;AAAA,IACA,CAAC,YAAY,KAAK;AAAA,EACpB;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["import_utils"]}
|
@@ -51,7 +51,7 @@ var isTargetOption = (target) => {
|
|
51
51
|
var useAutocompleteOption = (props) => {
|
52
52
|
var _a, _b;
|
53
53
|
const {
|
54
|
-
closeOnSelect
|
54
|
+
closeOnSelect,
|
55
55
|
focusedIndex,
|
56
56
|
inputRef,
|
57
57
|
omitSelectedValues,
|
@@ -59,7 +59,6 @@ var useAutocompleteOption = (props) => {
|
|
59
59
|
value,
|
60
60
|
optionProps,
|
61
61
|
onChange,
|
62
|
-
onChangeLabel,
|
63
62
|
onClose,
|
64
63
|
onFocusNext
|
65
64
|
} = useAutocompleteContext();
|
@@ -90,28 +89,27 @@ var useAutocompleteOption = (props) => {
|
|
90
89
|
);
|
91
90
|
}
|
92
91
|
}
|
93
|
-
const isDuplicated =
|
92
|
+
const isDuplicated = frontValues.some(
|
93
|
+
({ node }) => node.dataset.value === (optionValue != null ? optionValue : "")
|
94
|
+
);
|
94
95
|
const isSelected = !isDuplicated && (!isMulti ? (optionValue != null ? optionValue : "") === value : value.includes(optionValue != null ? optionValue : ""));
|
95
96
|
const isTarget = "target" in ((_b = (_a = itemRef.current) == null ? void 0 : _a.dataset) != null ? _b : {});
|
96
97
|
const isFocused = index === focusedIndex;
|
97
98
|
const onClick = (0, import_react.useCallback)(
|
98
99
|
(ev) => {
|
99
100
|
ev.stopPropagation();
|
100
|
-
if (isDisabled) {
|
101
|
-
if (inputRef.current) inputRef.current.focus();
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
if (!isTargetOption(ev.currentTarget)) {
|
101
|
+
if (isDisabled || !isTargetOption(ev.currentTarget)) {
|
105
102
|
if (inputRef.current) inputRef.current.focus();
|
106
103
|
return;
|
107
104
|
}
|
108
|
-
setFocusedIndex(index);
|
105
|
+
if (!isDuplicated) setFocusedIndex(index);
|
109
106
|
onChange(optionValue != null ? optionValue : "");
|
110
107
|
if (inputRef.current) inputRef.current.focus();
|
111
|
-
if (customCloseOnSelect != null ? customCloseOnSelect :
|
108
|
+
if (customCloseOnSelect != null ? customCloseOnSelect : closeOnSelect) onClose();
|
112
109
|
if (omitSelectedValues) onFocusNext(index);
|
113
110
|
},
|
114
111
|
[
|
112
|
+
isDuplicated,
|
115
113
|
onFocusNext,
|
116
114
|
omitSelectedValues,
|
117
115
|
isDisabled,
|
@@ -120,14 +118,11 @@ var useAutocompleteOption = (props) => {
|
|
120
118
|
index,
|
121
119
|
onChange,
|
122
120
|
customCloseOnSelect,
|
123
|
-
|
121
|
+
closeOnSelect,
|
124
122
|
onClose,
|
125
123
|
inputRef
|
126
124
|
]
|
127
125
|
);
|
128
|
-
(0, import_utils2.useUpdateEffect)(() => {
|
129
|
-
if (isSelected) onChangeLabel(optionValue != null ? optionValue : "", { runOmit: false });
|
130
|
-
}, [optionValue]);
|
131
126
|
const getOptionProps = (0, import_react.useCallback)(
|
132
127
|
(props2 = {}, ref = null) => {
|
133
128
|
const style = {
|
@@ -151,6 +146,7 @@ var useAutocompleteOption = (props) => {
|
|
151
146
|
"aria-checked": isSelected,
|
152
147
|
"aria-disabled": (0, import_utils2.ariaAttr)(isDisabled),
|
153
148
|
"data-disabled": (0, import_utils2.dataAttr)(isDisabled),
|
149
|
+
"data-duplicated": (0, import_utils2.dataAttr)(isDuplicated),
|
154
150
|
"data-focus": (0, import_utils2.dataAttr)(isFocused),
|
155
151
|
"data-target": (0, import_utils2.dataAttr)(true),
|
156
152
|
"data-value": optionValue != null ? optionValue : "",
|
@@ -160,6 +156,7 @@ var useAutocompleteOption = (props) => {
|
|
160
156
|
},
|
161
157
|
[
|
162
158
|
id,
|
159
|
+
isDuplicated,
|
163
160
|
optionValue,
|
164
161
|
computedProps,
|
165
162
|
isDisabled,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/use-autocomplete-option.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { HTMLUIProps, PropGetter } from \"@yamada-ui/core\"\nimport type { CSSProperties, MouseEvent } from \"react\"\nimport {\n ariaAttr,\n dataAttr,\n handlerAll,\n isArray,\n isHTMLElement,\n isNumber,\n isString,\n isUndefined,\n mergeRefs,\n useUpdateEffect,\n} from \"@yamada-ui/utils\"\nimport { useCallback, useId, useRef } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendant,\n} from \"./autocomplete-context\"\n\nconst isTargetOption = (target: EventTarget | null): boolean =>\n isHTMLElement(target) && !!target.getAttribute(\"role\")?.startsWith(\"option\")\n\nexport interface UseAutocompleteOptionProps\n extends Omit<HTMLUIProps<\"li\">, \"children\" | \"value\"> {\n /**\n * The label of the autocomplete option.\n */\n children?: string\n /**\n * If `true`, the list element will be closed when selected.\n *\n * @default false\n */\n closeOnSelect?: boolean\n /**\n * If `true`, the autocomplete option will be disabled.\n *\n * @default false\n */\n isDisabled?: boolean\n /**\n * If `true`, the autocomplete option will be focusable.\n *\n * @default false\n */\n isFocusable?: boolean\n /**\n * The value of the autocomplete option.\n */\n value?: string\n}\n\nexport const useAutocompleteOption = (props: UseAutocompleteOptionProps) => {\n const {\n closeOnSelect: generalCloseOnSelect,\n focusedIndex,\n inputRef,\n omitSelectedValues,\n setFocusedIndex,\n value,\n optionProps,\n onChange,\n onChangeLabel,\n onClose,\n onFocusNext,\n } = useAutocompleteContext()\n const id = useId()\n\n let {\n children,\n closeOnSelect: customCloseOnSelect,\n icon: customIcon,\n isDisabled,\n isFocusable,\n value: optionValue,\n ...computedProps\n } = { ...optionProps, ...props }\n\n const trulyDisabled = !!isDisabled && !isFocusable\n\n const itemRef = useRef<HTMLLIElement>(null)\n\n const { descendants, index, register } = useAutocompleteDescendant({\n disabled: trulyDisabled,\n })\n\n const values = descendants.values()\n const frontValues = values.slice(0, index)\n const isMulti = isArray(value)\n\n if (isUndefined(optionValue)) {\n if (isString(children) || isNumber(children)) {\n optionValue = children.toString()\n } else {\n console.warn(\n `${\n !isMulti ? \"Autocomplete\" : \"MultiAutocomplete\"\n }: Cannot infer the option value of complex children. Pass a \\`value\\` prop or use a plain string as children to <Option>.`,\n )\n }\n }\n\n const isDuplicated = !isMulti\n ? frontValues.some(({ node }) => node.dataset.value === (optionValue ?? \"\"))\n : false\n const isSelected =\n !isDuplicated &&\n (!isMulti\n ? (optionValue ?? \"\") === value\n : value.includes(optionValue ?? \"\"))\n const isTarget = \"target\" in (itemRef.current?.dataset ?? {})\n const isFocused = index === focusedIndex\n\n const onClick = useCallback(\n (ev: MouseEvent<HTMLLIElement>) => {\n ev.stopPropagation()\n\n if (isDisabled) {\n if (inputRef.current) inputRef.current.focus()\n\n return\n }\n\n if (!isTargetOption(ev.currentTarget)) {\n if (inputRef.current) inputRef.current.focus()\n\n return\n }\n\n setFocusedIndex(index)\n\n onChange(optionValue ?? \"\")\n\n if (inputRef.current) inputRef.current.focus()\n\n if (customCloseOnSelect ?? generalCloseOnSelect) onClose()\n\n if (omitSelectedValues) onFocusNext(index)\n },\n [\n onFocusNext,\n omitSelectedValues,\n isDisabled,\n optionValue,\n setFocusedIndex,\n index,\n onChange,\n customCloseOnSelect,\n generalCloseOnSelect,\n onClose,\n inputRef,\n ],\n )\n\n useUpdateEffect(() => {\n if (isSelected) onChangeLabel(optionValue ?? \"\", { runOmit: false })\n }, [optionValue])\n\n const getOptionProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n id,\n ref: mergeRefs(itemRef, ref, register),\n role: \"option\",\n ...computedProps,\n ...props,\n style:\n !isTarget || (omitSelectedValues && isSelected) ? style : undefined,\n \"aria-checked\": isSelected,\n \"aria-disabled\": ariaAttr(isDisabled),\n \"data-disabled\": dataAttr(isDisabled),\n \"data-focus\": dataAttr(isFocused),\n \"data-target\": dataAttr(true),\n \"data-value\": optionValue ?? \"\",\n tabIndex: -1,\n onClick: handlerAll(computedProps.onClick, props.onClick, onClick),\n }\n },\n [\n id,\n optionValue,\n computedProps,\n isDisabled,\n isFocused,\n isSelected,\n isTarget,\n omitSelectedValues,\n onClick,\n register,\n ],\n )\n\n return {\n children,\n customIcon,\n isFocused,\n isSelected,\n getOptionProps,\n }\n}\n\nexport type UseAutocompleteOptionReturn = ReturnType<\n typeof useAutocompleteOption\n>\n\nexport const useAutocompleteCreate = () => {\n const { isHit, onCreate } = useAutocompleteContext()\n\n const getCreateProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n style: isHit ? style : undefined,\n \"data-focus\": dataAttr(!isHit),\n tabIndex: -1,\n onClick: handlerAll(props.onClick, onCreate),\n }\n },\n [isHit, onCreate],\n )\n\n return { getCreateProps }\n}\n\nexport type UseAutocompleteCreateReturn = ReturnType<\n typeof useAutocompleteCreate\n>\n\nexport const useAutocompleteEmpty = () => {\n const { isEmpty, isHit } = useAutocompleteContext()\n\n const getEmptyProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n style: isHit && !isEmpty ? style : undefined,\n tabIndex: -1,\n }\n },\n [isHit, isEmpty],\n )\n\n return { getEmptyProps }\n}\n\nexport type UseAutocompleteEmptyReturn = ReturnType<typeof useAutocompleteEmpty>\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type { ChangeOptions, UseAutocompleteProps } from \"./use-autocomplete\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport { createContext } from \"@yamada-ui/utils\"\n\nexport const {\n DescendantsContextProvider: AutocompleteDescendantsContextProvider,\n useDescendant: useAutocompleteDescendant,\n useDescendants: useAutocompleteDescendants,\n useDescendantsContext: useAutocompleteDescendantsContext,\n} = createDescendant()\n\ninterface AutocompleteContext\n extends Omit<\n UseAutocompleteProps,\n \"defaultValue\" | \"onChange\" | \"onCreate\" | \"value\"\n > {\n focusedIndex: number\n inputRef: RefObject<HTMLInputElement>\n inputValue: string\n isAllSelected: boolean\n isEmpty: boolean\n isHit: boolean\n isOpen: boolean\n label: string | string[] | undefined\n listRef: RefObject<HTMLUListElement>\n pickOptions: (value: string) => void\n rebirthOptions: (runFocus?: boolean) => void\n setFocusedIndex: Dispatch<SetStateAction<number>>\n styles: { [key: string]: CSSUIObject | undefined }\n value: string | string[]\n formControlProps: { [key: string]: any }\n inputProps: DOMAttributes<HTMLInputElement>\n onChange: (newValue: string, options?: ChangeOptions) => void\n onChangeLabel: (newValue: string, options?: ChangeOptions) => void\n onClose: () => void\n onCreate: () => void\n onFocusFirst: () => void\n onFocusLast: () => void\n onFocusNext: (index?: number) => void\n onFocusPrev: (index?: number) => void\n onOpen: () => void\n}\n\nexport const [AutocompleteProvider, useAutocompleteContext] =\n createContext<AutocompleteContext>({\n name: \"AutocompleteContext\",\n errorMessage: `useAutocompleteContext returned is 'undefined'. Seems you forgot to wrap the components in \"<Autocomplete />\" or \"<MultiAutocomplete />\"`,\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAAA,gBAWO;AACP,mBAA2C;;;ACX3C,4BAAiC;AACjC,mBAA8B;AAEvB,IAAM;AAAA,EACX,4BAA4B;AAAA,EAC5B,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,uBAAuB;AACzB,QAAI,wCAAiB;AAkCd,IAAM,CAAC,sBAAsB,sBAAsB,QACxD,4BAAmC;AAAA,EACjC,MAAM;AAAA,EACN,cAAc;AAChB,CAAC;;;AD7BH,IAAM,iBAAiB,CAAC,WAAqC;AApB7D;AAqBE,0CAAc,MAAM,KAAK,CAAC,GAAC,YAAO,aAAa,MAAM,MAA1B,mBAA6B,WAAW;AAAA;AAgC9D,IAAM,wBAAwB,CAAC,UAAsC;AArD5E;AAsDE,QAAM;AAAA,IACJ,eAAe;AAAA,IACf;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,uBAAuB;AAC3B,QAAM,SAAK,oBAAM;AAEjB,MAAI;AAAA,IACF;AAAA,IACA,eAAe;AAAA,IACf,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,GAAG;AAAA,EACL,IAAI,EAAE,GAAG,aAAa,GAAG,MAAM;AAE/B,QAAM,gBAAgB,CAAC,CAAC,cAAc,CAAC;AAEvC,QAAM,cAAU,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,SAAS;AAC1B,YAAM,QAAuB;AAAA,QAC3B,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,UAAU;AAAA,QACV,SAAS;AAAA,QACT,UAAU;AAAA,QACV,YAAY;AAAA,QACZ,OAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL;AAAA,QACA,GAAG;AAAA,QACH,OAAO,QAAQ,QAAQ;AAAA,QACvB,kBAAc,wBAAS,CAAC,KAAK;AAAA,QAC7B,UAAU;AAAA,QACV,aAAS,0BAAW,MAAM,SAAS,QAAQ;AAAA,MAC7C;AAAA,IACF;AAAA,IACA,CAAC,OAAO,QAAQ;AAAA,EAClB;AAEA,SAAO,EAAE,eAAe;AAC1B;AAMO,IAAM,uBAAuB,MAAM;AACxC,QAAM,EAAE,SAAS,MAAM,IAAI,uBAAuB;AAElD,QAAM,oBAAkC;AAAA,IACtC,CAAC,QAAQ,CAAC,GAAG,MAAM,SAAS;AAC1B,YAAM,QAAuB;AAAA,QAC3B,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,UAAU;AAAA,QACV,SAAS;AAAA,QACT,UAAU;AAAA,QACV,YAAY;AAAA,QACZ,OAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL;AAAA,QACA,GAAG;AAAA,QACH,OAAO,SAAS,CAAC,UAAU,QAAQ;AAAA,QACnC,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,IACA,CAAC,OAAO,OAAO;AAAA,EACjB;AAEA,SAAO,EAAE,cAAc;AACzB;","names":["import_utils","props"]}
|
1
|
+
{"version":3,"sources":["../src/use-autocomplete-option.ts","../src/autocomplete-context.ts"],"sourcesContent":["import type { HTMLUIProps, PropGetter } from \"@yamada-ui/core\"\nimport type { CSSProperties, MouseEvent } from \"react\"\nimport {\n ariaAttr,\n dataAttr,\n handlerAll,\n isArray,\n isHTMLElement,\n isNumber,\n isString,\n isUndefined,\n mergeRefs,\n} from \"@yamada-ui/utils\"\nimport { useCallback, useId, useRef } from \"react\"\nimport {\n useAutocompleteContext,\n useAutocompleteDescendant,\n} from \"./autocomplete-context\"\n\nconst isTargetOption = (target: EventTarget | null): boolean =>\n isHTMLElement(target) && !!target.getAttribute(\"role\")?.startsWith(\"option\")\n\nexport interface UseAutocompleteOptionProps\n extends Omit<HTMLUIProps<\"li\">, \"children\" | \"value\"> {\n /**\n * The label of the autocomplete option.\n */\n children?: string\n /**\n * If `true`, the list element will be closed when selected.\n *\n * @default false\n */\n closeOnSelect?: boolean\n /**\n * If `true`, the autocomplete option will be disabled.\n *\n * @default false\n */\n isDisabled?: boolean\n /**\n * If `true`, the autocomplete option will be focusable.\n *\n * @default false\n */\n isFocusable?: boolean\n /**\n * The value of the autocomplete option.\n */\n value?: string\n}\n\nexport const useAutocompleteOption = (props: UseAutocompleteOptionProps) => {\n const {\n closeOnSelect,\n focusedIndex,\n inputRef,\n omitSelectedValues,\n setFocusedIndex,\n value,\n optionProps,\n onChange,\n onClose,\n onFocusNext,\n } = useAutocompleteContext()\n const id = useId()\n\n let {\n children,\n closeOnSelect: customCloseOnSelect,\n icon: customIcon,\n isDisabled,\n isFocusable,\n value: optionValue,\n ...computedProps\n } = { ...optionProps, ...props }\n\n const trulyDisabled = !!isDisabled && !isFocusable\n\n const itemRef = useRef<HTMLLIElement>(null)\n\n const { descendants, index, register } = useAutocompleteDescendant({\n disabled: trulyDisabled,\n })\n\n const values = descendants.values()\n const frontValues = values.slice(0, index)\n const isMulti = isArray(value)\n\n if (isUndefined(optionValue)) {\n if (isString(children) || isNumber(children)) {\n optionValue = children.toString()\n } else {\n console.warn(\n `${\n !isMulti ? \"Autocomplete\" : \"MultiAutocomplete\"\n }: Cannot infer the option value of complex children. Pass a \\`value\\` prop or use a plain string as children to <Option>.`,\n )\n }\n }\n\n const isDuplicated = frontValues.some(\n ({ node }) => node.dataset.value === (optionValue ?? \"\"),\n )\n const isSelected =\n !isDuplicated &&\n (!isMulti\n ? (optionValue ?? \"\") === value\n : value.includes(optionValue ?? \"\"))\n const isTarget = \"target\" in (itemRef.current?.dataset ?? {})\n const isFocused = index === focusedIndex\n\n const onClick = useCallback(\n (ev: MouseEvent<HTMLLIElement>) => {\n ev.stopPropagation()\n\n if (isDisabled || !isTargetOption(ev.currentTarget)) {\n if (inputRef.current) inputRef.current.focus()\n\n return\n }\n\n if (!isDuplicated) setFocusedIndex(index)\n\n onChange(optionValue ?? \"\")\n\n if (inputRef.current) inputRef.current.focus()\n\n if (customCloseOnSelect ?? closeOnSelect) onClose()\n\n if (omitSelectedValues) onFocusNext(index)\n },\n [\n isDuplicated,\n onFocusNext,\n omitSelectedValues,\n isDisabled,\n optionValue,\n setFocusedIndex,\n index,\n onChange,\n customCloseOnSelect,\n closeOnSelect,\n onClose,\n inputRef,\n ],\n )\n\n const getOptionProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n id,\n ref: mergeRefs(itemRef, ref, register),\n role: \"option\",\n ...computedProps,\n ...props,\n style:\n !isTarget || (omitSelectedValues && isSelected) ? style : undefined,\n \"aria-checked\": isSelected,\n \"aria-disabled\": ariaAttr(isDisabled),\n \"data-disabled\": dataAttr(isDisabled),\n \"data-duplicated\": dataAttr(isDuplicated),\n \"data-focus\": dataAttr(isFocused),\n \"data-target\": dataAttr(true),\n \"data-value\": optionValue ?? \"\",\n tabIndex: -1,\n onClick: handlerAll(computedProps.onClick, props.onClick, onClick),\n }\n },\n [\n id,\n isDuplicated,\n optionValue,\n computedProps,\n isDisabled,\n isFocused,\n isSelected,\n isTarget,\n omitSelectedValues,\n onClick,\n register,\n ],\n )\n\n return {\n children,\n customIcon,\n isFocused,\n isSelected,\n getOptionProps,\n }\n}\n\nexport type UseAutocompleteOptionReturn = ReturnType<\n typeof useAutocompleteOption\n>\n\nexport const useAutocompleteCreate = () => {\n const { isHit, onCreate } = useAutocompleteContext()\n\n const getCreateProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n style: isHit ? style : undefined,\n \"data-focus\": dataAttr(!isHit),\n tabIndex: -1,\n onClick: handlerAll(props.onClick, onCreate),\n }\n },\n [isHit, onCreate],\n )\n\n return { getCreateProps }\n}\n\nexport type UseAutocompleteCreateReturn = ReturnType<\n typeof useAutocompleteCreate\n>\n\nexport const useAutocompleteEmpty = () => {\n const { isEmpty, isHit } = useAutocompleteContext()\n\n const getEmptyProps: PropGetter<\"li\"> = useCallback(\n (props = {}, ref = null) => {\n const style: CSSProperties = {\n border: \"0px\",\n clip: \"rect(0px, 0px, 0px, 0px)\",\n height: \"1px\",\n margin: \"-1px\",\n overflow: \"hidden\",\n padding: \"0px\",\n position: \"absolute\",\n whiteSpace: \"nowrap\",\n width: \"1px\",\n }\n\n return {\n ref,\n ...props,\n style: isHit && !isEmpty ? style : undefined,\n tabIndex: -1,\n }\n },\n [isHit, isEmpty],\n )\n\n return { getEmptyProps }\n}\n\nexport type UseAutocompleteEmptyReturn = ReturnType<typeof useAutocompleteEmpty>\n","import type { CSSUIObject } from \"@yamada-ui/core\"\nimport type { Dispatch, DOMAttributes, RefObject, SetStateAction } from \"react\"\nimport type { UseAutocompleteProps } from \"./use-autocomplete\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport { createContext } from \"@yamada-ui/utils\"\n\nexport const {\n DescendantsContextProvider: AutocompleteDescendantsContextProvider,\n useDescendant: useAutocompleteDescendant,\n useDescendants: useAutocompleteDescendants,\n useDescendantsContext: useAutocompleteDescendantsContext,\n} = createDescendant()\n\ninterface AutocompleteContext\n extends Omit<\n UseAutocompleteProps,\n \"defaultValue\" | \"onChange\" | \"onCreate\" | \"value\"\n > {\n focusedIndex: number\n inputRef: RefObject<HTMLInputElement>\n inputValue: string\n isAllSelected: boolean\n isEmpty: boolean\n isHit: boolean\n isOpen: boolean\n label: string | string[] | undefined\n listRef: RefObject<HTMLUListElement>\n pickOptions: (value: string) => void\n rebirthOptions: (runFocus?: boolean) => void\n setFocusedIndex: Dispatch<SetStateAction<number>>\n styles: { [key: string]: CSSUIObject | undefined }\n value: string | string[]\n formControlProps: { [key: string]: any }\n inputProps: DOMAttributes<HTMLInputElement>\n onChange: (newValue: string, runRebirth?: boolean) => void\n onClose: () => void\n onCreate: () => void\n onFocusFirst: () => void\n onFocusLast: () => void\n onFocusNext: (index?: number) => void\n onFocusPrev: (index?: number) => void\n onOpen: () => void\n}\n\nexport const [AutocompleteProvider, useAutocompleteContext] =\n createContext<AutocompleteContext>({\n name: \"AutocompleteContext\",\n errorMessage: `useAutocompleteContext returned is 'undefined'. Seems you forgot to wrap the components in \"<Autocomplete />\" or \"<MultiAutocomplete />\"`,\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAAA,gBAUO;AACP,mBAA2C;;;ACV3C,4BAAiC;AACjC,mBAA8B;AAEvB,IAAM;AAAA,EACX,4BAA4B;AAAA,EAC5B,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,uBAAuB;AACzB,QAAI,wCAAiB;AAiCd,IAAM,CAAC,sBAAsB,sBAAsB,QACxD,4BAAmC;AAAA,EACjC,MAAM;AAAA,EACN,cAAc;AAChB,CAAC;;;AD7BH,IAAM,iBAAiB,CAAC,WAAqC;AAnB7D;AAoBE,0CAAc,MAAM,KAAK,CAAC,GAAC,YAAO,aAAa,MAAM,MAA1B,m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AAAA,MACZ;AAAA,IACF;AAAA,IACA,CAAC,OAAO,OAAO;AAAA,EACjB;AAEA,SAAO,EAAE,cAAc;AACzB;","names":["import_utils","props"]}
|
@@ -3,8 +3,8 @@ import {
|
|
3
3
|
useAutocompleteCreate,
|
4
4
|
useAutocompleteEmpty,
|
5
5
|
useAutocompleteOption
|
6
|
-
} from "./chunk-
|
7
|
-
import "./chunk-
|
6
|
+
} from "./chunk-ZXALWDKQ.mjs";
|
7
|
+
import "./chunk-ZNO5RUVT.mjs";
|
8
8
|
export {
|
9
9
|
useAutocompleteCreate,
|
10
10
|
useAutocompleteEmpty,
|
@@ -1,9 +1,8 @@
|
|
1
1
|
import * as _yamada_ui_core from '@yamada-ui/core';
|
2
2
|
import { HTMLUIProps, PropGetter } from '@yamada-ui/core';
|
3
|
-
import * as react from 'react';
|
4
|
-
import { FocusEvent, DOMAttributes, MouseEvent, ChangeEvent } from 'react';
|
5
3
|
import * as _yamada_ui_use_descendant from '@yamada-ui/use-descendant';
|
6
|
-
import * as
|
4
|
+
import * as react from 'react';
|
5
|
+
import { ReactElement, FocusEvent, DOMAttributes, MouseEvent, ChangeEvent } from 'react';
|
7
6
|
import { FormControlOptions } from '@yamada-ui/form-control';
|
8
7
|
import { PopoverProps, ComboBoxProps } from '@yamada-ui/popover';
|
9
8
|
import { Union } from '@yamada-ui/utils';
|
@@ -12,11 +11,6 @@ import { AutocompleteOptionGroupProps } from './autocomplete-option-group.mjs';
|
|
12
11
|
import './use-autocomplete-option.mjs';
|
13
12
|
import './use-autocomplete-option-group.mjs';
|
14
13
|
|
15
|
-
interface ChangeOptions {
|
16
|
-
forceUpdate?: boolean;
|
17
|
-
runOmit?: boolean;
|
18
|
-
runRebirth?: boolean;
|
19
|
-
}
|
20
14
|
interface AutocompleteItemWithValue extends AutocompleteOptionProps {
|
21
15
|
label?: string;
|
22
16
|
value?: string;
|
@@ -25,7 +19,8 @@ interface AutocompleteItemWithItems extends AutocompleteOptionGroupProps, Pick<A
|
|
25
19
|
items?: AutocompleteItemWithValue[];
|
26
20
|
}
|
27
21
|
type AutocompleteItem = AutocompleteItemWithItems | AutocompleteItemWithValue;
|
28
|
-
type
|
22
|
+
type MaybeValue = string | string[];
|
23
|
+
interface UseAutocompleteBaseProps<T extends MaybeValue = string> extends ComboBoxProps, FormControlOptions {
|
29
24
|
/**
|
30
25
|
* If `true`, enables the creation of autocomplete option.
|
31
26
|
*
|
@@ -96,14 +91,15 @@ type UseAutocompleteBaseProps<T extends string | string[] = string> = {
|
|
96
91
|
* The callback invoked when search input.
|
97
92
|
*/
|
98
93
|
onSearch?: (ev: ChangeEvent<HTMLInputElement>) => void;
|
99
|
-
}
|
100
|
-
|
101
|
-
|
94
|
+
}
|
95
|
+
interface UseAutocompleteProps<T extends MaybeValue = string> extends Omit<HTMLUIProps<"input">, "disabled" | "list" | "readOnly" | "required" | "size" | keyof UseAutocompleteBaseProps>, UseAutocompleteBaseProps<T> {
|
96
|
+
}
|
97
|
+
declare const useAutocomplete: <T extends MaybeValue = string>(props: UseAutocompleteProps<T>) => {
|
102
98
|
id: string | undefined;
|
103
99
|
allowCreate: boolean;
|
104
100
|
allowFree: boolean;
|
101
|
+
children: string | number | boolean | ReactElement<any, string | react.JSXElementConstructor<any>> | Iterable<react.ReactNode> | undefined;
|
105
102
|
closeOnSelect: boolean;
|
106
|
-
computedChildren: (react_jsx_runtime.JSX.Element | undefined)[] | undefined;
|
107
103
|
descendants: {
|
108
104
|
count: (filter?: _yamada_ui_use_descendant.FilterDescendant<HTMLElement, {}> | undefined) => number;
|
109
105
|
destroy: () => void;
|
@@ -164,8 +160,7 @@ declare const useAutocomplete: <T extends string | string[] = string>(props: Use
|
|
164
160
|
getPopoverProps: (props?: PopoverProps) => PopoverProps;
|
165
161
|
inputProps: DOMAttributes<HTMLInputElement>;
|
166
162
|
optionProps: Omit<AutocompleteOptionProps, "value" | "children"> | undefined;
|
167
|
-
onChange: (newValue: string,
|
168
|
-
onChangeLabel: (newValue: string, { forceUpdate, runOmit }?: ChangeOptions) => void;
|
163
|
+
onChange: (newValue: string, runRebirth?: boolean) => void;
|
169
164
|
onClear: (ev: MouseEvent<HTMLDivElement>) => void;
|
170
165
|
onClose: () => void;
|
171
166
|
onCompositionEnd: () => void;
|
@@ -185,4 +180,4 @@ declare const useAutocompleteInput: () => {
|
|
185
180
|
};
|
186
181
|
type UseAutocompleteInputReturn = ReturnType<typeof useAutocompleteInput>;
|
187
182
|
|
188
|
-
export { type AutocompleteItem, type
|
183
|
+
export { type AutocompleteItem, type UseAutocompleteInputReturn, type UseAutocompleteProps, type UseAutocompleteReturn, useAutocomplete, useAutocompleteInput };
|
@@ -1,9 +1,8 @@
|
|
1
1
|
import * as _yamada_ui_core from '@yamada-ui/core';
|
2
2
|
import { HTMLUIProps, PropGetter } from '@yamada-ui/core';
|
3
|
-
import * as react from 'react';
|
4
|
-
import { FocusEvent, DOMAttributes, MouseEvent, ChangeEvent } from 'react';
|
5
3
|
import * as _yamada_ui_use_descendant from '@yamada-ui/use-descendant';
|
6
|
-
import * as
|
4
|
+
import * as react from 'react';
|
5
|
+
import { ReactElement, FocusEvent, DOMAttributes, MouseEvent, ChangeEvent } from 'react';
|
7
6
|
import { FormControlOptions } from '@yamada-ui/form-control';
|
8
7
|
import { PopoverProps, ComboBoxProps } from '@yamada-ui/popover';
|
9
8
|
import { Union } from '@yamada-ui/utils';
|
@@ -12,11 +11,6 @@ import { AutocompleteOptionGroupProps } from './autocomplete-option-group.js';
|
|
12
11
|
import './use-autocomplete-option.js';
|
13
12
|
import './use-autocomplete-option-group.js';
|
14
13
|
|
15
|
-
interface ChangeOptions {
|
16
|
-
forceUpdate?: boolean;
|
17
|
-
runOmit?: boolean;
|
18
|
-
runRebirth?: boolean;
|
19
|
-
}
|
20
14
|
interface AutocompleteItemWithValue extends AutocompleteOptionProps {
|
21
15
|
label?: string;
|
22
16
|
value?: string;
|
@@ -25,7 +19,8 @@ interface AutocompleteItemWithItems extends AutocompleteOptionGroupProps, Pick<A
|
|
25
19
|
items?: AutocompleteItemWithValue[];
|
26
20
|
}
|
27
21
|
type AutocompleteItem = AutocompleteItemWithItems | AutocompleteItemWithValue;
|
28
|
-
type
|
22
|
+
type MaybeValue = string | string[];
|
23
|
+
interface UseAutocompleteBaseProps<T extends MaybeValue = string> extends ComboBoxProps, FormControlOptions {
|
29
24
|
/**
|
30
25
|
* If `true`, enables the creation of autocomplete option.
|
31
26
|
*
|
@@ -96,14 +91,15 @@ type UseAutocompleteBaseProps<T extends string | string[] = string> = {
|
|
96
91
|
* The callback invoked when search input.
|
97
92
|
*/
|
98
93
|
onSearch?: (ev: ChangeEvent<HTMLInputElement>) => void;
|
99
|
-
}
|
100
|
-
|
101
|
-
|
94
|
+
}
|
95
|
+
interface UseAutocompleteProps<T extends MaybeValue = string> extends Omit<HTMLUIProps<"input">, "disabled" | "list" | "readOnly" | "required" | "size" | keyof UseAutocompleteBaseProps>, UseAutocompleteBaseProps<T> {
|
96
|
+
}
|
97
|
+
declare const useAutocomplete: <T extends MaybeValue = string>(props: UseAutocompleteProps<T>) => {
|
102
98
|
id: string | undefined;
|
103
99
|
allowCreate: boolean;
|
104
100
|
allowFree: boolean;
|
101
|
+
children: string | number | boolean | ReactElement<any, string | react.JSXElementConstructor<any>> | Iterable<react.ReactNode> | undefined;
|
105
102
|
closeOnSelect: boolean;
|
106
|
-
computedChildren: (react_jsx_runtime.JSX.Element | undefined)[] | undefined;
|
107
103
|
descendants: {
|
108
104
|
count: (filter?: _yamada_ui_use_descendant.FilterDescendant<HTMLElement, {}> | undefined) => number;
|
109
105
|
destroy: () => void;
|
@@ -164,8 +160,7 @@ declare const useAutocomplete: <T extends string | string[] = string>(props: Use
|
|
164
160
|
getPopoverProps: (props?: PopoverProps) => PopoverProps;
|
165
161
|
inputProps: DOMAttributes<HTMLInputElement>;
|
166
162
|
optionProps: Omit<AutocompleteOptionProps, "value" | "children"> | undefined;
|
167
|
-
onChange: (newValue: string,
|
168
|
-
onChangeLabel: (newValue: string, { forceUpdate, runOmit }?: ChangeOptions) => void;
|
163
|
+
onChange: (newValue: string, runRebirth?: boolean) => void;
|
169
164
|
onClear: (ev: MouseEvent<HTMLDivElement>) => void;
|
170
165
|
onClose: () => void;
|
171
166
|
onCompositionEnd: () => void;
|
@@ -185,4 +180,4 @@ declare const useAutocompleteInput: () => {
|
|
185
180
|
};
|
186
181
|
type UseAutocompleteInputReturn = ReturnType<typeof useAutocompleteInput>;
|
187
182
|
|
188
|
-
export { type AutocompleteItem, type
|
183
|
+
export { type AutocompleteItem, type UseAutocompleteInputReturn, type UseAutocompleteProps, type UseAutocompleteReturn, useAutocomplete, useAutocompleteInput };
|
package/dist/use-autocomplete.js
CHANGED
@@ -158,7 +158,7 @@ var isTargetOption = (target) => {
|
|
158
158
|
var useAutocompleteOption = (props) => {
|
159
159
|
var _a, _b;
|
160
160
|
const {
|
161
|
-
closeOnSelect
|
161
|
+
closeOnSelect,
|
162
162
|
focusedIndex,
|
163
163
|
inputRef,
|
164
164
|
omitSelectedValues,
|
@@ -166,7 +166,6 @@ var useAutocompleteOption = (props) => {
|
|
166
166
|
value,
|
167
167
|
optionProps,
|
168
168
|
onChange,
|
169
|
-
onChangeLabel,
|
170
169
|
onClose,
|
171
170
|
onFocusNext
|
172
171
|
} = useAutocompleteContext();
|
@@ -197,28 +196,27 @@ var useAutocompleteOption = (props) => {
|
|
197
196
|
);
|
198
197
|
}
|
199
198
|
}
|
200
|
-
const isDuplicated =
|
199
|
+
const isDuplicated = frontValues.some(
|
200
|
+
({ node }) => node.dataset.value === (optionValue != null ? optionValue : "")
|
201
|
+
);
|
201
202
|
const isSelected = !isDuplicated && (!isMulti ? (optionValue != null ? optionValue : "") === value : value.includes(optionValue != null ? optionValue : ""));
|
202
203
|
const isTarget = "target" in ((_b = (_a = itemRef.current) == null ? void 0 : _a.dataset) != null ? _b : {});
|
203
204
|
const isFocused = index === focusedIndex;
|
204
205
|
const onClick = (0, import_react2.useCallback)(
|
205
206
|
(ev) => {
|
206
207
|
ev.stopPropagation();
|
207
|
-
if (isDisabled) {
|
208
|
-
if (inputRef.current) inputRef.current.focus();
|
209
|
-
return;
|
210
|
-
}
|
211
|
-
if (!isTargetOption(ev.currentTarget)) {
|
208
|
+
if (isDisabled || !isTargetOption(ev.currentTarget)) {
|
212
209
|
if (inputRef.current) inputRef.current.focus();
|
213
210
|
return;
|
214
211
|
}
|
215
|
-
setFocusedIndex(index);
|
212
|
+
if (!isDuplicated) setFocusedIndex(index);
|
216
213
|
onChange(optionValue != null ? optionValue : "");
|
217
214
|
if (inputRef.current) inputRef.current.focus();
|
218
|
-
if (customCloseOnSelect != null ? customCloseOnSelect :
|
215
|
+
if (customCloseOnSelect != null ? customCloseOnSelect : closeOnSelect) onClose();
|
219
216
|
if (omitSelectedValues) onFocusNext(index);
|
220
217
|
},
|
221
218
|
[
|
219
|
+
isDuplicated,
|
222
220
|
onFocusNext,
|
223
221
|
omitSelectedValues,
|
224
222
|
isDisabled,
|
@@ -227,14 +225,11 @@ var useAutocompleteOption = (props) => {
|
|
227
225
|
index,
|
228
226
|
onChange,
|
229
227
|
customCloseOnSelect,
|
230
|
-
|
228
|
+
closeOnSelect,
|
231
229
|
onClose,
|
232
230
|
inputRef
|
233
231
|
]
|
234
232
|
);
|
235
|
-
(0, import_utils3.useUpdateEffect)(() => {
|
236
|
-
if (isSelected) onChangeLabel(optionValue != null ? optionValue : "", { runOmit: false });
|
237
|
-
}, [optionValue]);
|
238
233
|
const getOptionProps = (0, import_react2.useCallback)(
|
239
234
|
(props2 = {}, ref = null) => {
|
240
235
|
const style = {
|
@@ -258,6 +253,7 @@ var useAutocompleteOption = (props) => {
|
|
258
253
|
"aria-checked": isSelected,
|
259
254
|
"aria-disabled": (0, import_utils3.ariaAttr)(isDisabled),
|
260
255
|
"data-disabled": (0, import_utils3.dataAttr)(isDisabled),
|
256
|
+
"data-duplicated": (0, import_utils3.dataAttr)(isDuplicated),
|
261
257
|
"data-focus": (0, import_utils3.dataAttr)(isFocused),
|
262
258
|
"data-target": (0, import_utils3.dataAttr)(true),
|
263
259
|
"data-value": optionValue != null ? optionValue : "",
|
@@ -267,6 +263,7 @@ var useAutocompleteOption = (props) => {
|
|
267
263
|
},
|
268
264
|
[
|
269
265
|
id,
|
266
|
+
isDuplicated,
|
270
267
|
optionValue,
|
271
268
|
computedProps,
|
272
269
|
isDisabled,
|
@@ -685,15 +682,23 @@ var useAutocomplete = (props) => {
|
|
685
682
|
);
|
686
683
|
const validChildren = (0, import_utils7.getValidChildren)(children);
|
687
684
|
const computedChildren = (0, import_react4.useMemo)(
|
688
|
-
() => resolvedItems == null ? void 0 : resolvedItems.map((item,
|
685
|
+
() => resolvedItems == null ? void 0 : resolvedItems.map((item, index) => {
|
689
686
|
if ("value" in item) {
|
690
687
|
const { label: label2, value: value2, ...props2 } = item;
|
691
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteOption, { value: value2, ...props2, children: label2 },
|
688
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteOption, { value: value2, ...props2, children: label2 }, index);
|
692
689
|
} else if ("items" in item) {
|
693
690
|
const { items: items2 = [], label: label2, ...props2 } = item;
|
694
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
691
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
692
|
+
AutocompleteOptionGroup,
|
693
|
+
{
|
694
|
+
label: label2,
|
695
|
+
...props2,
|
696
|
+
children: items2.map(({ label: label3, value: value2, ...props3 }, index2) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteOption, { value: value2, ...props3, children: label3 }, index2))
|
697
|
+
},
|
698
|
+
index
|
699
|
+
);
|
695
700
|
}
|
696
|
-
}),
|
701
|
+
}).filter(Boolean),
|
697
702
|
[resolvedItems]
|
698
703
|
);
|
699
704
|
const isEmpty = !validChildren.length && !(computedChildren == null ? void 0 : computedChildren.length);
|
@@ -872,50 +877,29 @@ var useAutocomplete = (props) => {
|
|
872
877
|
},
|
873
878
|
[descendants, onFocusFirst]
|
874
879
|
);
|
875
|
-
const
|
876
|
-
(
|
877
|
-
const
|
878
|
-
|
879
|
-
const
|
880
|
+
const onChangeLabel = (0, import_react4.useCallback)(
|
881
|
+
(newValue) => {
|
882
|
+
const values = descendants.values();
|
883
|
+
if (!values.length) return;
|
884
|
+
const resolvedValues = (0, import_utils7.isArray)(newValue) ? newValue : [newValue];
|
885
|
+
const selectedLabel = resolvedValues.map((value2) => {
|
880
886
|
var _a, _b;
|
881
|
-
const { node } = (_a =
|
887
|
+
const { node } = (_a = values.find(({ node: node2 }) => node2.dataset.value === value2)) != null ? _a : {};
|
882
888
|
if (node) {
|
883
|
-
const
|
889
|
+
const { textContent } = (_b = Array.from(node.children).find(
|
884
890
|
(child) => child.getAttribute("data-label") !== null
|
885
|
-
);
|
886
|
-
return
|
891
|
+
)) != null ? _b : {};
|
892
|
+
return textContent;
|
887
893
|
} else {
|
888
894
|
return allowFree ? value2 : void 0;
|
889
895
|
}
|
890
|
-
}).filter(
|
891
|
-
|
896
|
+
}).filter((label2) => !(0, import_utils7.isUndefined)(label2));
|
897
|
+
setLabel(!isMulti ? selectedLabel[0] : selectedLabel);
|
892
898
|
},
|
893
|
-
[allowFree, descendants]
|
894
|
-
);
|
895
|
-
const onChangeLabel = (0, import_react4.useCallback)(
|
896
|
-
(newValue, { forceUpdate, runOmit = true } = {}) => {
|
897
|
-
const selectedValues2 = getSelectedValues(newValue);
|
898
|
-
if (!forceUpdate && !selectedValues2.length) return;
|
899
|
-
setLabel((prev) => {
|
900
|
-
if (!isMulti) {
|
901
|
-
return selectedValues2[0];
|
902
|
-
} else {
|
903
|
-
selectedValues2.forEach((selectedValue) => {
|
904
|
-
const isSelected = (0, import_utils7.isArray)(prev) && prev.includes(selectedValue);
|
905
|
-
if (!isSelected) {
|
906
|
-
prev = [...(0, import_utils7.isArray)(prev) ? prev : [], selectedValue];
|
907
|
-
} else if (runOmit) {
|
908
|
-
prev = (0, import_utils7.isArray)(prev) ? prev.filter((value2) => value2 !== selectedValue) : void 0;
|
909
|
-
}
|
910
|
-
});
|
911
|
-
return prev;
|
912
|
-
}
|
913
|
-
});
|
914
|
-
},
|
915
|
-
[getSelectedValues, isMulti]
|
899
|
+
[allowFree, descendants, isMulti]
|
916
900
|
);
|
917
901
|
const onChange = (0, import_react4.useCallback)(
|
918
|
-
(newValue,
|
902
|
+
(newValue, runRebirth = true) => {
|
919
903
|
setValue((prev) => {
|
920
904
|
let next;
|
921
905
|
if (!(0, import_utils7.isArray)(prev)) {
|
@@ -937,14 +921,13 @@ var useAutocomplete = (props) => {
|
|
937
921
|
return format((_a = node.textContent) != null ? _a : "").includes(newValue);
|
938
922
|
}
|
939
923
|
).length > 0;
|
940
|
-
|
941
|
-
if (allowFree || isHit2) setInputValue("");
|
924
|
+
if (allowCreate || allowFree || isHit2) setInputValue("");
|
942
925
|
if (isMulti && runRebirth) rebirthOptions(false);
|
943
926
|
},
|
944
927
|
[
|
945
928
|
allowFree,
|
929
|
+
allowCreate,
|
946
930
|
isMulti,
|
947
|
-
onChangeLabel,
|
948
931
|
rebirthOptions,
|
949
932
|
setValue,
|
950
933
|
descendants,
|
@@ -1055,7 +1038,7 @@ var useAutocomplete = (props) => {
|
|
1055
1038
|
const relatedTarget = (0, import_utils7.getEventRelatedTarget)(ev);
|
1056
1039
|
if ((0, import_utils7.isContains)(containerRef.current, relatedTarget)) return;
|
1057
1040
|
if (!closeOnBlur && isHit) return;
|
1058
|
-
if (allowFree && !!inputValue) onChange(inputValue,
|
1041
|
+
if (allowFree && !!inputValue) onChange(inputValue, false);
|
1059
1042
|
setInputValue("");
|
1060
1043
|
if (isOpen) onClose();
|
1061
1044
|
},
|
@@ -1063,7 +1046,7 @@ var useAutocomplete = (props) => {
|
|
1063
1046
|
);
|
1064
1047
|
const onDelete = (0, import_react4.useCallback)(() => {
|
1065
1048
|
if (!isMulti) {
|
1066
|
-
onChange(""
|
1049
|
+
onChange("");
|
1067
1050
|
} else {
|
1068
1051
|
onChange(value[value.length - 1]);
|
1069
1052
|
}
|
@@ -1150,18 +1133,9 @@ var useAutocomplete = (props) => {
|
|
1150
1133
|
onClose,
|
1151
1134
|
maxSelectValues
|
1152
1135
|
]);
|
1153
|
-
(0,
|
1154
|
-
|
1155
|
-
|
1156
|
-
if (JSON.stringify((_a = prevValue.current) != null ? _a : []) === JSON.stringify(value))
|
1157
|
-
return;
|
1158
|
-
const label2 = getSelectedValues(value);
|
1159
|
-
setLabel(label2);
|
1160
|
-
} else {
|
1161
|
-
if (prevValue.current === value) return;
|
1162
|
-
onChangeLabel(value, { runOmit: false });
|
1163
|
-
}
|
1164
|
-
}, [isMulti, value, onChangeLabel, getSelectedValues]);
|
1136
|
+
(0, import_utils7.useSafeLayoutEffect)(() => {
|
1137
|
+
onChangeLabel(value);
|
1138
|
+
}, [value]);
|
1165
1139
|
(0, import_utils7.useUpdateEffect)(() => {
|
1166
1140
|
if (isOpen || allowFree) return;
|
1167
1141
|
setFocusedIndex(-1);
|
@@ -1268,8 +1242,8 @@ var useAutocomplete = (props) => {
|
|
1268
1242
|
id,
|
1269
1243
|
allowCreate,
|
1270
1244
|
allowFree,
|
1245
|
+
children: children != null ? children : computedChildren,
|
1271
1246
|
closeOnSelect,
|
1272
|
-
computedChildren,
|
1273
1247
|
descendants,
|
1274
1248
|
emptyMessage,
|
1275
1249
|
focusedIndex,
|
@@ -1293,7 +1267,6 @@ var useAutocomplete = (props) => {
|
|
1293
1267
|
inputProps,
|
1294
1268
|
optionProps,
|
1295
1269
|
onChange,
|
1296
|
-
onChangeLabel,
|
1297
1270
|
onClear,
|
1298
1271
|
onClose,
|
1299
1272
|
onCompositionEnd,
|