@neovici/cosmoz-autocomplete 10.5.0 → 10.6.1
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.
- package/dist/autocomplete/autocomplete.js +27 -7
- package/dist/autocomplete/use-autocomplete.d.ts +1 -0
- package/dist/autocomplete/use-autocomplete.js +3 -0
- package/dist/listbox/index.js +6 -3
- package/dist/listbox/item-renderer.d.ts +1 -0
- package/dist/listbox/use-listbox.js +1 -0
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import '@neovici/cosmoz-input';
|
|
|
2
2
|
import './skeleton-span';
|
|
3
3
|
import { useHost } from '@neovici/cosmoz-utils/hooks/use-host';
|
|
4
4
|
import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api';
|
|
5
|
-
import { html
|
|
5
|
+
import { html } from 'lit-html';
|
|
6
6
|
import { live } from 'lit-html/directives/live.js';
|
|
7
7
|
import { until } from 'lit-html/directives/until.js';
|
|
8
8
|
import { when } from 'lit-html/directives/when.js';
|
|
@@ -13,10 +13,10 @@ import { useAutocomplete } from './use-autocomplete';
|
|
|
13
13
|
import { useOverflow } from './use-overflow';
|
|
14
14
|
import { ref } from 'lit-html/directives/ref.js';
|
|
15
15
|
import { useFloating, defaultMiddleware, size, } from '@neovici/cosmoz-dropdown/use-floating';
|
|
16
|
+
import { useEffect } from '@pionjs/pion';
|
|
16
17
|
const inputParts = ['input', 'control', 'label', 'line', 'error', 'wrap']
|
|
17
18
|
.map((part) => `${part}: input-${part}`)
|
|
18
19
|
.join();
|
|
19
|
-
const blank = () => nothing;
|
|
20
20
|
const middleware = [
|
|
21
21
|
size({
|
|
22
22
|
apply({ rects, elements }) {
|
|
@@ -27,13 +27,28 @@ const middleware = [
|
|
|
27
27
|
}),
|
|
28
28
|
...defaultMiddleware,
|
|
29
29
|
];
|
|
30
|
+
const shouldShowDropdown = ({ active, loading, items, text, isSingle, showSingle, }) => {
|
|
31
|
+
if (!active)
|
|
32
|
+
return false;
|
|
33
|
+
const hasResultsOrQuery = loading || items.length > 0 || text.length > 0;
|
|
34
|
+
const disallowedSingle = isSingle && !showSingle;
|
|
35
|
+
return hasResultsOrQuery && !disallowedSingle;
|
|
36
|
+
};
|
|
30
37
|
const autocomplete = (props) => {
|
|
31
|
-
const { active, invalid, errorMessage, label, placeholder, disabled, noLabelFloat, alwaysFloatLabel, textual, text, onText, onFocus, onClick, onDeselect, value, limit, min, showSingle, items, source$, placement, } = props, host = useHost(), isOne = limit == 1, // eslint-disable-line eqeqeq
|
|
38
|
+
const { active, invalid, errorMessage, label, placeholder, disabled, noLabelFloat, alwaysFloatLabel, textual, text, onText, onFocus, onClick, onDeselect, value, limit, min, showSingle, items, source$, placement, loading, } = props, host = useHost(), isOne = limit == 1, // eslint-disable-line eqeqeq
|
|
32
39
|
isSingle = isOne && value?.[0] != null;
|
|
33
40
|
const { styles, setReference, setFloating } = useFloating({
|
|
34
41
|
placement,
|
|
35
42
|
middleware,
|
|
36
43
|
});
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
host.addEventListener('focusin', onFocus);
|
|
46
|
+
host.addEventListener('focusout', onFocus);
|
|
47
|
+
return () => {
|
|
48
|
+
host.removeEventListener('focusin', onFocus);
|
|
49
|
+
host.removeEventListener('focusout', onFocus);
|
|
50
|
+
};
|
|
51
|
+
}, [onFocus]);
|
|
37
52
|
useImperativeApi({
|
|
38
53
|
focus: () => host.shadowRoot?.querySelector('#input')?.focus(),
|
|
39
54
|
}, []);
|
|
@@ -51,8 +66,6 @@ const autocomplete = (props) => {
|
|
|
51
66
|
.errorMessage=${until(source$.then(() => errorMessage, (e) => e.message), errorMessage)}
|
|
52
67
|
.value=${live(text)}
|
|
53
68
|
@value-changed=${onText}
|
|
54
|
-
@focusin=${onFocus}
|
|
55
|
-
@focusout=${onFocus}
|
|
56
69
|
@click=${onClick}
|
|
57
70
|
autocomplete="off"
|
|
58
71
|
exportparts=${inputParts}
|
|
@@ -71,13 +84,20 @@ const autocomplete = (props) => {
|
|
|
71
84
|
})}
|
|
72
85
|
</cosmoz-input>
|
|
73
86
|
|
|
74
|
-
${when(
|
|
87
|
+
${when(shouldShowDropdown({
|
|
88
|
+
active,
|
|
89
|
+
loading,
|
|
90
|
+
items,
|
|
91
|
+
text,
|
|
92
|
+
isSingle,
|
|
93
|
+
showSingle,
|
|
94
|
+
}), () => listbox({
|
|
75
95
|
...props,
|
|
76
96
|
items,
|
|
77
97
|
multi: !isOne,
|
|
78
98
|
setFloating,
|
|
79
99
|
styles,
|
|
80
|
-
}, when(
|
|
100
|
+
}, when(loading, () => html `<cosmoz-autocomplete-skeleton-span></cosmoz-autocomplete-skeleton-span>`, () => when(text.length > 0 && items.length === 0, () => html `<slot name="no-result"></slot>`))))}`;
|
|
81
101
|
}, Autocomplete = (props) => {
|
|
82
102
|
const thru = {
|
|
83
103
|
...props,
|
|
@@ -6,6 +6,7 @@ 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, normalize, notify, useNotify, EMPTY } from './util';
|
|
9
|
+
import { usePromise } from '@neovici/cosmoz-utils/hooks/use-promise';
|
|
9
10
|
export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, externalSearch, ...thru }) => {
|
|
10
11
|
const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), { active, focused, onFocus, setClosed } = useFocus(thru), empty = !text, query = useMemo(() => text?.trim(), [text]), host = useHost(), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
|
|
11
12
|
_onChange?.(val, () => setClosed(true));
|
|
@@ -35,12 +36,14 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
35
36
|
setClosed,
|
|
36
37
|
onSelect,
|
|
37
38
|
});
|
|
39
|
+
const [, , state] = usePromise(source$);
|
|
38
40
|
return {
|
|
39
41
|
active,
|
|
40
42
|
query,
|
|
41
43
|
textual,
|
|
42
44
|
value,
|
|
43
45
|
source$,
|
|
46
|
+
loading: state === 'pending',
|
|
44
47
|
items: useMemo(() => {
|
|
45
48
|
if (!active)
|
|
46
49
|
return EMPTY;
|
package/dist/listbox/index.js
CHANGED
|
@@ -9,6 +9,8 @@ import { ref } from 'lit-html/directives/ref.js';
|
|
|
9
9
|
import { styleMap } from 'lit-html/directives/style-map.js';
|
|
10
10
|
import style, { styles } from './style.css';
|
|
11
11
|
import { properties, useListbox } from './use-listbox';
|
|
12
|
+
import { noop } from '@neovici/cosmoz-utils/function';
|
|
13
|
+
const ignore = noop;
|
|
12
14
|
const Listbox = (host) => {
|
|
13
15
|
const listRef = useRef(undefined);
|
|
14
16
|
const { position, items, renderItem, height, itemHeight, setItemHeight } = useListbox(host);
|
|
@@ -18,8 +20,9 @@ const Listbox = (host) => {
|
|
|
18
20
|
return;
|
|
19
21
|
vl.layoutComplete.then(() => {
|
|
20
22
|
host.dispatchEvent(new CustomEvent('layout-complete'));
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const { averageChildSize, averageMarginSize } = vl['_layout']._metricsCache;
|
|
24
|
+
return setItemHeight(averageChildSize + averageMarginSize * 2);
|
|
25
|
+
}, ignore);
|
|
23
26
|
}, [items]);
|
|
24
27
|
useEffect(() => {
|
|
25
28
|
if (!position.scroll)
|
|
@@ -28,7 +31,7 @@ const Listbox = (host) => {
|
|
|
28
31
|
if (!vl)
|
|
29
32
|
return;
|
|
30
33
|
if (!vl?.['_layout']) {
|
|
31
|
-
vl.layoutComplete.then(() => vl.element(position.index)?.scrollIntoView({ block: 'nearest' }));
|
|
34
|
+
vl.layoutComplete.then(() => vl.element(position.index)?.scrollIntoView({ block: 'nearest' }), ignore);
|
|
32
35
|
return;
|
|
33
36
|
}
|
|
34
37
|
vl.element(position.index)?.scrollIntoView({ block: 'nearest' });
|