@neovici/cosmoz-autocomplete 10.3.0 → 10.5.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.d.ts +3 -2
- package/dist/autocomplete/autocomplete.js +2 -1
- package/dist/autocomplete/use-autocomplete.d.ts +2 -1
- package/dist/autocomplete/use-autocomplete.js +3 -2
- package/dist/listbox/index.d.ts +1 -1
- package/dist/listbox/index.js +20 -11
- package/dist/listbox/item-renderer.d.ts +6 -16
- package/dist/listbox/item-renderer.js +1 -1
- package/dist/listbox/style.css.d.ts +2 -2
- package/dist/listbox/style.css.js +21 -15
- package/dist/listbox/use-item-height.d.ts +1 -0
- package/dist/listbox/use-item-height.js +8 -0
- package/dist/listbox/use-listbox.d.ts +3 -2
- package/dist/listbox/use-listbox.js +5 -2
- package/dist/listbox/use-render-item.d.ts +2 -2
- package/package.json +1 -1
|
@@ -11,14 +11,15 @@ export interface Props<I> extends Base<I> {
|
|
|
11
11
|
noLabelFloat?: boolean;
|
|
12
12
|
alwaysFloatLabel?: boolean;
|
|
13
13
|
showSingle?: boolean;
|
|
14
|
-
itemHeight?: number;
|
|
14
|
+
itemHeight?: number | 'auto';
|
|
15
15
|
itemLimit?: number;
|
|
16
16
|
wrap?: boolean;
|
|
17
17
|
defaultIndex?: number;
|
|
18
|
+
externalSearch?: boolean;
|
|
18
19
|
placement?: Placement;
|
|
19
20
|
}
|
|
20
21
|
type AProps<I> = Omit<Props<I>, keyof RProps<I>> & RProps<I> & {
|
|
21
22
|
onInputRef?: (el?: Element) => void;
|
|
22
23
|
};
|
|
23
|
-
declare const autocomplete: <I>(props: AProps<I>) => import("lit-html").TemplateResult<1>, Autocomplete: <I>(props: Props<I>) => import("lit-html").TemplateResult<1>, observedAttributes: readonly ["disabled", "invalid", "no-label-float", "always-float-label", "text-property", "value-property", "limit", "min", "show-single", "preserve-order", "keep-opened", "keep-query", "default-index", "item-height", "item-limit", "wrap"];
|
|
24
|
+
declare const autocomplete: <I>(props: AProps<I>) => import("lit-html").TemplateResult<1>, Autocomplete: <I>(props: Props<I>) => import("lit-html").TemplateResult<1>, observedAttributes: readonly ["disabled", "invalid", "no-label-float", "always-float-label", "text-property", "value-property", "limit", "min", "show-single", "preserve-order", "keep-opened", "keep-query", "default-index", "external-search", "item-height", "item-limit", "wrap"];
|
|
24
25
|
export { Autocomplete, autocomplete, observedAttributes, style };
|
|
@@ -71,7 +71,7 @@ const autocomplete = (props) => {
|
|
|
71
71
|
})}
|
|
72
72
|
</cosmoz-input>
|
|
73
73
|
|
|
74
|
-
${when(active && !(isSingle && !showSingle), () => listbox({
|
|
74
|
+
${when(active && !(isSingle && !showSingle) && items.length > 0, () => listbox({
|
|
75
75
|
...props,
|
|
76
76
|
items,
|
|
77
77
|
multi: !isOne,
|
|
@@ -99,6 +99,7 @@ const autocomplete = (props) => {
|
|
|
99
99
|
'keep-opened',
|
|
100
100
|
'keep-query',
|
|
101
101
|
'default-index',
|
|
102
|
+
'external-search',
|
|
102
103
|
'item-height',
|
|
103
104
|
'item-limit',
|
|
104
105
|
'wrap',
|
|
@@ -26,8 +26,9 @@ export interface Props<I> extends Base<I> {
|
|
|
26
26
|
onFocus?: (focused?: boolean) => void;
|
|
27
27
|
preserveOrder?: boolean;
|
|
28
28
|
defaultIndex?: number;
|
|
29
|
+
externalSearch?: boolean;
|
|
29
30
|
}
|
|
30
|
-
export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, ...thru }: Props<I>) => {
|
|
31
|
+
export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, externalSearch, ...thru }: Props<I>) => {
|
|
31
32
|
active: boolean | undefined;
|
|
32
33
|
query: string;
|
|
33
34
|
textual: (i: I) => string;
|
|
@@ -6,7 +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
|
-
export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, ...thru }) => {
|
|
9
|
+
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
10
|
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
11
|
_onChange?.(val, () => setClosed(true));
|
|
12
12
|
notify(host, 'value', val);
|
|
@@ -47,7 +47,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
47
47
|
const items = preserveOrder
|
|
48
48
|
? options
|
|
49
49
|
: [...value, ...without(value, prop(valueProperty))(options)];
|
|
50
|
-
return search(items, query, textual);
|
|
50
|
+
return externalSearch ? items : search(items, query, textual);
|
|
51
51
|
}, [
|
|
52
52
|
options,
|
|
53
53
|
active,
|
|
@@ -57,6 +57,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
|
|
|
57
57
|
value,
|
|
58
58
|
preserveOrder,
|
|
59
59
|
valueProperty,
|
|
60
|
+
externalSearch,
|
|
60
61
|
]),
|
|
61
62
|
onClick: useCallback(() => setClosed(false), []),
|
|
62
63
|
onFocus,
|
package/dist/listbox/index.d.ts
CHANGED
package/dist/listbox/index.js
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
import { virtualize, virtualizerRef, } from '@lit-labs/virtualizer/virtualize.js';
|
|
2
|
+
import { connectable } from '@neovici/cosmoz-dropdown/connectable';
|
|
2
3
|
import { sheet } from '@neovici/cosmoz-utils';
|
|
3
4
|
import { spreadProps } from '@neovici/cosmoz-utils/directives/spread-props';
|
|
4
5
|
import { useStyleSheet } from '@neovici/cosmoz-utils/hooks/use-stylesheet';
|
|
5
6
|
import { props } from '@neovici/cosmoz-utils/object';
|
|
6
|
-
import { component, html, useEffect,
|
|
7
|
+
import { component, html, useEffect, useRef } from '@pionjs/pion';
|
|
7
8
|
import { ref } from 'lit-html/directives/ref.js';
|
|
9
|
+
import { styleMap } from 'lit-html/directives/style-map.js';
|
|
8
10
|
import style, { styles } from './style.css';
|
|
9
11
|
import { properties, useListbox } from './use-listbox';
|
|
10
|
-
|
|
11
|
-
import { styleMap } from 'lit-html/directives/style-map.js';
|
|
12
|
-
const Listbox = (props) => {
|
|
12
|
+
const Listbox = (host) => {
|
|
13
13
|
const listRef = useRef(undefined);
|
|
14
|
-
const { position, items, renderItem, height, itemHeight } = useListbox(
|
|
14
|
+
const { position, items, renderItem, height, itemHeight, setItemHeight } = useListbox(host);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const vl = listRef.current?.[virtualizerRef];
|
|
17
|
+
if (!vl)
|
|
18
|
+
return;
|
|
19
|
+
vl.layoutComplete.then(() => {
|
|
20
|
+
host.dispatchEvent(new CustomEvent('layout-complete'));
|
|
21
|
+
return setItemHeight(vl['_layout']._metricsCache.averageChildSize);
|
|
22
|
+
});
|
|
23
|
+
}, [items]);
|
|
15
24
|
useEffect(() => {
|
|
16
25
|
if (!position.scroll)
|
|
17
26
|
return;
|
|
18
27
|
const vl = listRef.current?.[virtualizerRef];
|
|
19
|
-
if (!vl
|
|
28
|
+
if (!vl)
|
|
29
|
+
return;
|
|
30
|
+
if (!vl?.['_layout']) {
|
|
31
|
+
vl.layoutComplete.then(() => vl.element(position.index)?.scrollIntoView({ block: 'nearest' }));
|
|
20
32
|
return;
|
|
33
|
+
}
|
|
21
34
|
vl.element(position.index)?.scrollIntoView({ block: 'nearest' });
|
|
22
35
|
}, [position]);
|
|
23
|
-
useStyleSheet(styles({ ...position,
|
|
24
|
-
const layout = useMemo(() => ({
|
|
25
|
-
_itemSize: { height: itemHeight - 0.00001 },
|
|
26
|
-
}), [itemHeight]);
|
|
36
|
+
useStyleSheet(styles({ ...position, itemHeight, auto: host.itemHeight === 'auto' }));
|
|
27
37
|
return html `<div
|
|
28
38
|
class="items"
|
|
29
39
|
style="min-height: ${height}px"
|
|
@@ -34,7 +44,6 @@ const Listbox = (props) => {
|
|
|
34
44
|
items,
|
|
35
45
|
renderItem,
|
|
36
46
|
scroller: true,
|
|
37
|
-
layout,
|
|
38
47
|
})}
|
|
39
48
|
</div>
|
|
40
49
|
<slot></slot>`;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { TemplateResult } from 'lit-html';
|
|
2
|
-
|
|
2
|
+
import { Position } from './use-items';
|
|
3
|
+
export interface ItemRendererOpts<I> {
|
|
4
|
+
position: Position;
|
|
3
5
|
highlight: (i: number) => void;
|
|
4
6
|
select: (item: I) => void;
|
|
5
7
|
isSelected: (item: I) => void;
|
|
@@ -7,19 +9,7 @@ export interface Opts<I> {
|
|
|
7
9
|
textual: (i: I) => string;
|
|
8
10
|
}
|
|
9
11
|
export type Render<I> = (content: unknown, item: I, i: number) => unknown;
|
|
10
|
-
export type ItemRenderer<I> = (item: I, i: number, opts:
|
|
11
|
-
export declare const itemRenderer: <I>(render?: Render<I>) => (item: I, i: number, { highlight, select, textual, query, isSelected, }:
|
|
12
|
-
|
|
13
|
-
select: (item: I) => void;
|
|
14
|
-
isSelected: (item: I) => void;
|
|
15
|
-
query: string;
|
|
16
|
-
textual: (i: I) => string;
|
|
17
|
-
}) => TemplateResult;
|
|
18
|
-
declare const _default: (item: unknown, i: number, { highlight, select, textual, query, isSelected, }: {
|
|
19
|
-
highlight: (i: number) => void;
|
|
20
|
-
select: (item: unknown) => void;
|
|
21
|
-
isSelected: (item: unknown) => void;
|
|
22
|
-
query: string;
|
|
23
|
-
textual: (i: unknown) => string;
|
|
24
|
-
}) => TemplateResult;
|
|
12
|
+
export type ItemRenderer<I> = (item: I, i: number, opts: ItemRendererOpts<I>) => TemplateResult;
|
|
13
|
+
export declare const itemRenderer: <I>(render?: Render<I>) => (item: I, i: number, { highlight, select, textual, query, isSelected, }: ItemRendererOpts<I>) => TemplateResult;
|
|
14
|
+
declare const _default: (item: unknown, i: number, { highlight, select, textual, query, isSelected, }: ItemRendererOpts<unknown>) => TemplateResult;
|
|
25
15
|
export default _default;
|
|
@@ -3,7 +3,7 @@ import { html } from 'lit-html';
|
|
|
3
3
|
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
|
-
return html
|
|
6
|
+
return html `<div
|
|
7
7
|
class="item"
|
|
8
8
|
role="option"
|
|
9
9
|
part="option"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
declare const style: string;
|
|
2
2
|
export default style;
|
|
3
|
-
export declare const styles: ({ index,
|
|
3
|
+
export declare const styles: ({ index, itemHeight, auto, }: {
|
|
4
4
|
index?: number;
|
|
5
|
-
height: number;
|
|
6
5
|
itemHeight: number;
|
|
6
|
+
auto: boolean;
|
|
7
7
|
}) => string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { css } from '@pionjs/pion';
|
|
2
|
+
import { when } from 'lit-html/directives/when.js';
|
|
2
3
|
const svg =
|
|
3
4
|
/* eslint-disable quotes */
|
|
4
5
|
"data:image/svg+xml,%3Csvg width='11' height='8' viewBox='0 0 11 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath " +
|
|
@@ -9,10 +10,7 @@ const style = css `
|
|
|
9
10
|
position: fixed;
|
|
10
11
|
z-index: 1000;
|
|
11
12
|
font-family: var(--paper-font-subhead_-_font-family, inherit);
|
|
12
|
-
background: var(
|
|
13
|
-
--cosmoz-autocomplete-listbox-bg,
|
|
14
|
-
rgba(255, 255, 255, 0.2)
|
|
15
|
-
);
|
|
13
|
+
background: var(--cosmoz-autocomplete-listbox-bg, rgba(255, 255, 255, 0.2));
|
|
16
14
|
min-width: 50px;
|
|
17
15
|
backdrop-filter: blur(16px) saturate(180%);
|
|
18
16
|
-webkit-backdrop-filter: blur(16px) saturate(180%);
|
|
@@ -22,12 +20,23 @@ const style = css `
|
|
|
22
20
|
0 1.5px 6px 0 rgba(0, 0, 0, 0.1);
|
|
23
21
|
text-transform: var(--cosmoz-autocomplete-listbox-text-transform, initial);
|
|
24
22
|
overflow: hidden;
|
|
23
|
+
transition:
|
|
24
|
+
opacity 150ms ease-in-out,
|
|
25
|
+
transform 100ms ease-in-out;
|
|
25
26
|
}
|
|
26
27
|
:host(:popover-open) {
|
|
27
28
|
box-sizing: border-box;
|
|
28
29
|
display: block;
|
|
29
30
|
margin: 0;
|
|
30
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
|
+
}
|
|
31
40
|
}
|
|
32
41
|
:host([popover]) {
|
|
33
42
|
padding: 0;
|
|
@@ -103,16 +112,13 @@ const style = css `
|
|
|
103
112
|
}
|
|
104
113
|
`;
|
|
105
114
|
export default style;
|
|
106
|
-
export const styles = ({ index,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
line-height: ${itemHeight}px;
|
|
114
|
-
height: ${itemHeight}px;
|
|
115
|
-
}
|
|
115
|
+
export const styles = ({ index, itemHeight, auto, }) => css `
|
|
116
|
+
${when(!auto, () => css `
|
|
117
|
+
.item {
|
|
118
|
+
line-height: ${itemHeight}px;
|
|
119
|
+
height: ${itemHeight}px;
|
|
120
|
+
}
|
|
121
|
+
`)}
|
|
116
122
|
|
|
117
123
|
.item[data-index='${index || '0'}'] {
|
|
118
124
|
background: var(
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useItemHeight: (initialItemHeight: number | "auto") => readonly [number, (v: number) => void];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useState } from '@pionjs/pion';
|
|
2
|
+
export const useItemHeight = (initialItemHeight) => {
|
|
3
|
+
const auto = initialItemHeight === 'auto', [itemHeight, setItemHeight] = useState(auto ? 40 : initialItemHeight);
|
|
4
|
+
return [
|
|
5
|
+
itemHeight,
|
|
6
|
+
(v) => (auto ? setItemHeight(v) : undefined),
|
|
7
|
+
];
|
|
8
|
+
};
|
|
@@ -9,16 +9,17 @@ export interface Props<I> {
|
|
|
9
9
|
query: string;
|
|
10
10
|
textual: (i: I) => string;
|
|
11
11
|
itemRenderer?: ItemRenderer<I>;
|
|
12
|
-
itemHeight?: number;
|
|
12
|
+
itemHeight?: number | 'auto';
|
|
13
13
|
itemLimit?: number;
|
|
14
14
|
}
|
|
15
|
-
export declare const useListbox: <I>({ value, valueProperty, items: _items, onSelect, defaultIndex, query, textual, itemRenderer, itemHeight, itemLimit, }: Props<I>) => {
|
|
15
|
+
export declare const useListbox: <I>({ value, valueProperty, items: _items, onSelect, defaultIndex, query, textual, itemRenderer, itemHeight: _itemHeight, itemLimit, }: Props<I>) => {
|
|
16
16
|
position: import("./use-items").Position;
|
|
17
17
|
items: I[];
|
|
18
18
|
height: number;
|
|
19
19
|
highlight: (index: number) => void;
|
|
20
20
|
select: (item: I) => void;
|
|
21
21
|
itemHeight: number;
|
|
22
|
+
setItemHeight: (v: number) => void;
|
|
22
23
|
renderItem: (item: I, i: number) => import("lit-html").TemplateResult;
|
|
23
24
|
};
|
|
24
25
|
export { useRenderItem };
|
|
@@ -2,6 +2,7 @@ import { useMemo } from '@pionjs/pion';
|
|
|
2
2
|
import { byValue } from './util';
|
|
3
3
|
import { useItems } from './use-items';
|
|
4
4
|
import { useRenderItem } from './use-render-item';
|
|
5
|
+
import { useItemHeight } from './use-item-height';
|
|
5
6
|
export const properties = [
|
|
6
7
|
'query',
|
|
7
8
|
'items',
|
|
@@ -16,7 +17,7 @@ export const properties = [
|
|
|
16
17
|
'valueProperty',
|
|
17
18
|
'loading',
|
|
18
19
|
];
|
|
19
|
-
export const useListbox = ({ value, valueProperty, items: _items, onSelect, defaultIndex, query, textual, itemRenderer, itemHeight = 40, itemLimit = 5, }) => {
|
|
20
|
+
export const useListbox = ({ value, valueProperty, items: _items, onSelect, defaultIndex, query, textual, itemRenderer, itemHeight: _itemHeight = 40, itemLimit = 5, }) => {
|
|
20
21
|
const isSelected = useMemo(() => byValue(value, valueProperty), [value, valueProperty]),
|
|
21
22
|
// TODO: investigate if we can drop this
|
|
22
23
|
items = useMemo(() => _items.slice(), [_items, isSelected]), { position, highlight, select } = useItems({
|
|
@@ -25,7 +26,7 @@ export const useListbox = ({ value, valueProperty, items: _items, onSelect, defa
|
|
|
25
26
|
defaultIndex: isNaN(defaultIndex)
|
|
26
27
|
? undefined
|
|
27
28
|
: Number(defaultIndex),
|
|
28
|
-
});
|
|
29
|
+
}), [itemHeight, setItemHeight] = useItemHeight(_itemHeight);
|
|
29
30
|
return {
|
|
30
31
|
position,
|
|
31
32
|
items,
|
|
@@ -33,8 +34,10 @@ export const useListbox = ({ value, valueProperty, items: _items, onSelect, defa
|
|
|
33
34
|
highlight,
|
|
34
35
|
select,
|
|
35
36
|
itemHeight,
|
|
37
|
+
setItemHeight,
|
|
36
38
|
renderItem: useRenderItem({
|
|
37
39
|
itemRenderer,
|
|
40
|
+
position,
|
|
38
41
|
highlight,
|
|
39
42
|
select,
|
|
40
43
|
textual,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ItemRenderer,
|
|
2
|
-
export declare const useRenderItem: <I>({ itemRenderer, ...meta }:
|
|
1
|
+
import { ItemRenderer, ItemRendererOpts } from './item-renderer';
|
|
2
|
+
export declare const useRenderItem: <I>({ itemRenderer, ...meta }: ItemRendererOpts<I> & {
|
|
3
3
|
itemRenderer?: ItemRenderer<I>;
|
|
4
4
|
}) => (item: I, i: number) => import("lit-html").TemplateResult;
|
|
5
5
|
export type { ItemRenderer };
|