@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.
@@ -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), () => 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);
@@ -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' });
@@ -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;
@@ -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
- 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>`;
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,6 +1,6 @@
1
1
  declare const style: string;
2
2
  export default style;
3
- export declare const styles: ({ index, itemHeight, auto }: {
3
+ export declare const styles: ({ index, itemHeight, auto, }: {
4
4
  index?: number;
5
5
  itemHeight: number;
6
6
  auto: boolean;
@@ -1,4 +1,4 @@
1
- import { tagged as css } from '@neovici/cosmoz-utils';
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;
@@ -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.4.0",
3
+ "version": "10.6.0",
4
4
  "description": "A autocomplete input web component",
5
5
  "keywords": [
6
6
  "lit-html",