@neovici/cosmoz-autocomplete 7.1.0 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,8 +5,8 @@ type Source<I> = (opts: {
|
|
|
5
5
|
interface Base<I> {
|
|
6
6
|
value: I | I[];
|
|
7
7
|
limit?: number;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
autoClose?: boolean;
|
|
9
|
+
autoClear?: boolean;
|
|
10
10
|
onText: (text: string) => void;
|
|
11
11
|
onChange: (value: I[], done?: () => void) => void;
|
|
12
12
|
onSelect: (value: I, meta: Meta<I>) => void;
|
|
@@ -26,7 +26,7 @@ export interface Props<I> extends Base<I> {
|
|
|
26
26
|
disabled?: boolean;
|
|
27
27
|
onFocus?: (focused?: boolean) => void;
|
|
28
28
|
}
|
|
29
|
-
export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty,
|
|
29
|
+
export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, autoClose, autoClear, ...thru }: Props<I>) => {
|
|
30
30
|
active: boolean | undefined;
|
|
31
31
|
query: string;
|
|
32
32
|
textual: (i: I) => string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useMemo } from 'haunted';
|
|
1
|
+
import { useCallback, useMemo, useEffect } from 'haunted';
|
|
2
2
|
import { without, array } from '@neovici/cosmoz-utils/array';
|
|
3
3
|
import { prop, strProp } from '@neovici/cosmoz-utils/object';
|
|
4
4
|
import { useHost } from '@neovici/cosmoz-utils/hooks/use-host';
|
|
@@ -6,8 +6,8 @@ import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
|
|
|
6
6
|
import { useFocus } from '@neovici/cosmoz-dropdown/use-focus';
|
|
7
7
|
import { useKeys } from './use-keys';
|
|
8
8
|
import { search, notify, useNotify, EMPTY$ } from './util';
|
|
9
|
-
export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty,
|
|
10
|
-
const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), { active, onFocus, setClosed } = useFocus(thru), empty = !text, query = useMemo(() => text?.trim().toLowerCase(), [text]), host = useHost(), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
|
|
9
|
+
export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, autoClose, autoClear, ...thru }) => {
|
|
10
|
+
const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), { active, focused, onFocus, setClosed } = useFocus(thru), empty = !text, query = useMemo(() => text?.trim().toLowerCase(), [text]), host = useHost(), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
|
|
11
11
|
_onChange?.(val, () => setClosed(true));
|
|
12
12
|
notify(host, 'value', val);
|
|
13
13
|
}, [_onChange]), source$ = useMemo(() => Promise.resolve(typeof source === 'function' ? source({ query, active }) : source), [source, active, query]), value = useMemo(() => array(_value), [_value]), values$ = useMemo(() => source$.then((source) => [
|
|
@@ -15,7 +15,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
15
15
|
...without(value, prop(valueProperty))(source),
|
|
16
16
|
]), [source$, value, valueProperty]);
|
|
17
17
|
useKeys({
|
|
18
|
-
|
|
18
|
+
focused,
|
|
19
19
|
empty,
|
|
20
20
|
limit,
|
|
21
21
|
value,
|
|
@@ -23,13 +23,17 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
23
23
|
onChange,
|
|
24
24
|
onText,
|
|
25
25
|
});
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!focused)
|
|
28
|
+
onText('');
|
|
29
|
+
}, [focused]);
|
|
26
30
|
const meta = useMeta({
|
|
27
31
|
onText,
|
|
28
32
|
onChange,
|
|
29
33
|
value,
|
|
30
34
|
limit,
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
autoClose,
|
|
36
|
+
autoClear,
|
|
33
37
|
setClosed,
|
|
34
38
|
onSelect,
|
|
35
39
|
});
|
|
@@ -54,17 +58,12 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
54
58
|
setClosed(false);
|
|
55
59
|
}, [onText, text, setClosed]),
|
|
56
60
|
onSelect: useCallback((newVal) => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
const { onChange, onText, limit, value: val, keepQuery, keepOpened, setClosed, } = meta;
|
|
62
|
-
if (!keepQuery) {
|
|
61
|
+
meta.onSelect?.(newVal, meta);
|
|
62
|
+
const { onChange, onText, limit, value: val, autoClose, autoClear, setClosed, } = meta;
|
|
63
|
+
if (autoClear)
|
|
63
64
|
onText('');
|
|
64
|
-
|
|
65
|
-
if (!keepOpened) {
|
|
65
|
+
if (autoClose)
|
|
66
66
|
setClosed(true);
|
|
67
|
-
}
|
|
68
67
|
const value = array(val);
|
|
69
68
|
onChange((value.includes(newVal)
|
|
70
69
|
? without(newVal)(value)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface Props<I> {
|
|
2
|
-
|
|
2
|
+
focused?: boolean;
|
|
3
3
|
empty?: boolean;
|
|
4
4
|
hideEmpty?: boolean;
|
|
5
5
|
value: I | I[];
|
|
@@ -7,4 +7,4 @@ export interface Props<I> {
|
|
|
7
7
|
onChange: (v: I[]) => void;
|
|
8
8
|
onText: (s: string) => void;
|
|
9
9
|
}
|
|
10
|
-
export declare const useKeys: <I>({
|
|
10
|
+
export declare const useKeys: <I>({ focused, empty, ...info }: Props<I>) => void;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useEffect } from 'haunted';
|
|
2
2
|
import { array } from '@neovici/cosmoz-utils/array';
|
|
3
3
|
import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
|
|
4
|
-
export const useKeys = ({
|
|
5
|
-
const enabled =
|
|
4
|
+
export const useKeys = ({ focused, empty, ...info }) => {
|
|
5
|
+
const enabled = focused && empty, meta = useMeta(info);
|
|
6
6
|
useEffect(() => {
|
|
7
7
|
if (!enabled) {
|
|
8
8
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-autocomplete",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.2.0",
|
|
4
4
|
"description": "A autocomplete input web component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lit-html",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@lit-labs/virtualizer": "^2.0.0",
|
|
71
|
-
"@neovici/cosmoz-dropdown": "^3.
|
|
72
|
-
"@neovici/cosmoz-input": "^3.
|
|
71
|
+
"@neovici/cosmoz-dropdown": "^3.4.0",
|
|
72
|
+
"@neovici/cosmoz-input": "^3.11.0",
|
|
73
73
|
"@neovici/cosmoz-utils": "^5.0.0",
|
|
74
74
|
"haunted": "^5.0.0",
|
|
75
75
|
"lit-html": "^2.0.0"
|
|
@@ -77,12 +77,12 @@
|
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@commitlint/cli": "^17.0.0",
|
|
79
79
|
"@commitlint/config-conventional": "^17.0.0",
|
|
80
|
-
"@neovici/cfg": "^1.
|
|
80
|
+
"@neovici/cfg": "^1.34.0",
|
|
81
81
|
"@open-wc/testing": "^3.0.0",
|
|
82
82
|
"@semantic-release/changelog": "^6.0.0",
|
|
83
83
|
"@semantic-release/git": "^10.0.0",
|
|
84
84
|
"@storybook/storybook-deployer": "^2.8.5",
|
|
85
|
-
"@web/dev-server-storybook": "^0.
|
|
85
|
+
"@web/dev-server-storybook": "^0.7.0",
|
|
86
86
|
"eslint-import-resolver-exports": "^1.0.0-beta.5",
|
|
87
87
|
"husky": "^8.0.0",
|
|
88
88
|
"semantic-release": "^21.0.0",
|