@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.
@@ -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, nothing } from 'lit-html'; // eslint-disable-line object-curly-newline
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(active && !(isSingle && !showSingle) && items.length > 0, () => listbox({
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(items.length < 5, () => until(source$.then(blank, blank), html `<cosmoz-autocomplete-skeleton-span></cosmoz-autocomplete-skeleton-span>`))))}`;
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,
@@ -34,6 +34,7 @@ export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onCh
34
34
  textual: (i: I) => string;
35
35
  value: I[];
36
36
  source$: Promise<I[]>;
37
+ loading: boolean;
37
38
  items: I[];
38
39
  onClick: () => void;
39
40
  onFocus: (e: FocusEvent) => void;
@@ -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;
@@ -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
- return setItemHeight(vl['_layout']._metricsCache.averageChildSize);
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' });
@@ -1,6 +1,7 @@
1
1
  import { TemplateResult } from 'lit-html';
2
2
  import { Position } from './use-items';
3
3
  export interface ItemRendererOpts<I> {
4
+ items: I[];
4
5
  position: Position;
5
6
  highlight: (i: number) => void;
6
7
  select: (item: I) => void;
@@ -37,6 +37,7 @@ export const useListbox = ({ value, valueProperty, items: _items, onSelect, defa
37
37
  setItemHeight,
38
38
  renderItem: useRenderItem({
39
39
  itemRenderer,
40
+ items,
40
41
  position,
41
42
  highlight,
42
43
  select,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "10.5.0",
3
+ "version": "10.6.1",
4
4
  "description": "A autocomplete input web component",
5
5
  "keywords": [
6
6
  "lit-html",