@neovici/cosmoz-autocomplete 10.4.0 → 10.6.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.
- 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 +4 -2
- package/dist/listbox/item-renderer.d.ts +1 -0
- package/dist/listbox/item-renderer.js +13 -12
- package/dist/listbox/style.css.d.ts +1 -1
- package/dist/listbox/style.css.js +31 -3
- 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);
|
|
@@ -19,7 +21,7 @@ const Listbox = (host) => {
|
|
|
19
21
|
vl.layoutComplete.then(() => {
|
|
20
22
|
host.dispatchEvent(new CustomEvent('layout-complete'));
|
|
21
23
|
return setItemHeight(vl['_layout']._metricsCache.averageChildSize);
|
|
22
|
-
});
|
|
24
|
+
}, ignore);
|
|
23
25
|
}, [items]);
|
|
24
26
|
useEffect(() => {
|
|
25
27
|
if (!position.scroll)
|
|
@@ -28,7 +30,7 @@ const Listbox = (host) => {
|
|
|
28
30
|
if (!vl)
|
|
29
31
|
return;
|
|
30
32
|
if (!vl?.['_layout']) {
|
|
31
|
-
vl.layoutComplete.then(() => vl.element(position.index)?.scrollIntoView({ block: 'nearest' }));
|
|
33
|
+
vl.layoutComplete.then(() => vl.element(position.index)?.scrollIntoView({ block: 'nearest' }), ignore);
|
|
32
34
|
return;
|
|
33
35
|
}
|
|
34
36
|
vl.element(position.index)?.scrollIntoView({ block: 'nearest' });
|
|
@@ -4,17 +4,18 @@ import { mark } from './util';
|
|
|
4
4
|
export const itemRenderer = (render = identity) => (item, i, { highlight, select, textual = identity, query, isSelected, }) => {
|
|
5
5
|
const text = textual(item), content = mark(text, query), rendered = render(content, item, i);
|
|
6
6
|
return html `<div
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
class="item"
|
|
8
|
+
role="option"
|
|
9
|
+
part="option"
|
|
10
|
+
?aria-selected=${isSelected(item)}
|
|
11
|
+
data-index=${i}
|
|
12
|
+
@mouseenter=${() => highlight(i)}
|
|
13
|
+
@click=${() => select(item)}
|
|
14
|
+
@mousedown=${(e) => e.preventDefault()}
|
|
15
|
+
title=${text}
|
|
16
|
+
>
|
|
17
|
+
${rendered}
|
|
18
|
+
</div>
|
|
19
|
+
<div class="sizer" virtualizer-sizer>${rendered}</div>`;
|
|
19
20
|
};
|
|
20
21
|
export default itemRenderer();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { css } from '@pionjs/pion';
|
|
2
2
|
import { when } from 'lit-html/directives/when.js';
|
|
3
3
|
const svg =
|
|
4
4
|
/* eslint-disable quotes */
|
|
@@ -20,12 +20,23 @@ const style = css `
|
|
|
20
20
|
0 1.5px 6px 0 rgba(0, 0, 0, 0.1);
|
|
21
21
|
text-transform: var(--cosmoz-autocomplete-listbox-text-transform, initial);
|
|
22
22
|
overflow: hidden;
|
|
23
|
+
transition:
|
|
24
|
+
opacity 150ms ease-in-out,
|
|
25
|
+
transform 100ms ease-in-out;
|
|
23
26
|
}
|
|
24
27
|
:host(:popover-open) {
|
|
25
28
|
box-sizing: border-box;
|
|
26
29
|
display: block;
|
|
27
30
|
margin: 0;
|
|
28
31
|
border: 1px solid rgba(200, 200, 200, 0.25);
|
|
32
|
+
|
|
33
|
+
opacity: 1;
|
|
34
|
+
transform: translateY(0);
|
|
35
|
+
|
|
36
|
+
@starting-style {
|
|
37
|
+
opacity: 0;
|
|
38
|
+
transform: translateY(-50px);
|
|
39
|
+
}
|
|
29
40
|
}
|
|
30
41
|
:host([popover]) {
|
|
31
42
|
padding: 0;
|
|
@@ -49,6 +60,20 @@ const style = css `
|
|
|
49
60
|
overflow: hidden;
|
|
50
61
|
}
|
|
51
62
|
|
|
63
|
+
.sizer {
|
|
64
|
+
position: relative;
|
|
65
|
+
visibility: hidden;
|
|
66
|
+
opacity: 0;
|
|
67
|
+
pointer-events: none;
|
|
68
|
+
z-index: -1;
|
|
69
|
+
height: 0;
|
|
70
|
+
width: auto;
|
|
71
|
+
padding: 0 20px;
|
|
72
|
+
overflow: hidden;
|
|
73
|
+
max-width: inherit;
|
|
74
|
+
font-size: 14px;
|
|
75
|
+
}
|
|
76
|
+
|
|
52
77
|
:host(:not([multi])) .item[aria-selected] {
|
|
53
78
|
background: var(--cosmoz-listbox-single-selection-color, #dadada);
|
|
54
79
|
}
|
|
@@ -71,6 +96,9 @@ const style = css `
|
|
|
71
96
|
/* prettier-ignore */
|
|
72
97
|
background: url("${svg}") #5881f6 no-repeat 50%;
|
|
73
98
|
}
|
|
99
|
+
:host([multi]) .sizer {
|
|
100
|
+
padding-left: 33px;
|
|
101
|
+
}
|
|
74
102
|
.swatch {
|
|
75
103
|
width: 18px;
|
|
76
104
|
height: 18px;
|
|
@@ -79,12 +107,12 @@ const style = css `
|
|
|
79
107
|
vertical-align: middle;
|
|
80
108
|
border-radius: 50%;
|
|
81
109
|
}
|
|
82
|
-
[virtualizer-sizer] {
|
|
110
|
+
[virtualizer-sizer]:not(.sizer) {
|
|
83
111
|
line-height: 1;
|
|
84
112
|
}
|
|
85
113
|
`;
|
|
86
114
|
export default style;
|
|
87
|
-
export const styles = ({ index, itemHeight, auto }) => css `
|
|
115
|
+
export const styles = ({ index, itemHeight, auto, }) => css `
|
|
88
116
|
${when(!auto, () => css `
|
|
89
117
|
.item {
|
|
90
118
|
line-height: ${itemHeight}px;
|