@neovici/cosmoz-autocomplete 13.1.0 → 13.2.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.
@@ -24,5 +24,5 @@ export interface Props<I> extends Base<I> {
24
24
  type AProps<I> = Omit<Props<I>, keyof RProps<I>> & RProps<I> & {
25
25
  onInputRef?: (el?: Element) => void;
26
26
  };
27
- 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"];
27
+ 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", "lazy-open"];
28
28
  export { Autocomplete, autocomplete, observedAttributes, style };
@@ -28,6 +28,7 @@ const autocomplete = (props) => {
28
28
  const hasResultsOrQuery = loading || items.length > 0 || (text != null && text.length > 0);
29
29
  return html `<cosmoz-dropdown-next
30
30
  open-on-focus
31
+ ?disabled=${disabled}
31
32
  .opened=${opened}
32
33
  @dropdown-toggle=${onToggle}
33
34
  part="dropdown"
@@ -102,5 +103,6 @@ const autocomplete = (props) => {
102
103
  'item-height',
103
104
  'item-limit',
104
105
  'wrap',
106
+ 'lazy-open',
105
107
  ];
106
108
  export { Autocomplete, autocomplete, observedAttributes, style };
@@ -26,8 +26,9 @@ export interface Props<I> extends Base<I> {
26
26
  preserveOrder?: boolean;
27
27
  defaultIndex?: number;
28
28
  externalSearch?: boolean;
29
+ lazyOpen?: 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, externalSearch, }: 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, disabled, lazyOpen, }: Props<I>) => {
31
32
  opened: boolean;
32
33
  query: string | undefined;
33
34
  textual: (i: I) => string;
@@ -11,13 +11,15 @@ const useNotify = (host, fn, name) => useCallback((val) => {
11
11
  fn?.(val);
12
12
  host.dispatchEvent(new CustomEvent(name, { detail: val }));
13
13
  }, [fn]);
14
- export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, externalSearch, }) => {
14
+ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, externalSearch, disabled, lazyOpen, }) => {
15
15
  const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), host = useHost(), [opened, setOpened] = useProperty('opened', false), empty = !text, query = useMemo(() => text?.trim(), [text]), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
16
16
  _onChange?.(val, () => setOpened(false));
17
17
  notify(host, 'value', val);
18
- }, [_onChange]), [options, setOptions] = useState([]), source$ = useMemo(() => Promise.resolve(typeof source === 'function'
19
- ? source({ query, active: opened })
20
- : source).then(normalize), [source, opened, query]), value = useMemo(() => array(_value), [_value]);
18
+ }, [_onChange]), [options, setOptions] = useState([]), belowMinLength = Boolean(lazyOpen && !query), source$ = useMemo(() => belowMinLength
19
+ ? Promise.resolve([])
20
+ : Promise.resolve(typeof source === 'function'
21
+ ? source({ query, active: opened })
22
+ : source).then(normalize), [source, opened, query, belowMinLength]), value = useMemo(() => array(_value), [_value]);
21
23
  useEffect(() => source$.then(setOptions), [source$]);
22
24
  // Backspace to deselect last chip (when input is empty and multi-select)
23
25
  useActivity({
@@ -28,7 +30,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
28
30
  onChange(values.slice(0, -1));
29
31
  }
30
32
  },
31
- check: () => empty && limit !== 1 && host.matches(':focus-within'),
33
+ check: () => !disabled && empty && limit !== 1 && host.matches(':focus-within'),
32
34
  element: () => host,
33
35
  }, []);
34
36
  // Clear query when popover closes
@@ -62,6 +64,8 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
62
64
  items: useMemo(() => {
63
65
  if (!opened)
64
66
  return EMPTY;
67
+ if (belowMinLength)
68
+ return EMPTY;
65
69
  const items = preserveOrder
66
70
  ? options
67
71
  : [...value, ...without(value, prop(valueProperty))(options)];
@@ -76,15 +80,22 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
76
80
  preserveOrder,
77
81
  valueProperty,
78
82
  externalSearch,
83
+ belowMinLength,
79
84
  ]),
80
85
  onToggle: useCallback((e) => {
86
+ if (disabled)
87
+ return;
81
88
  setOpened(e.newState === 'open');
82
- }, []),
89
+ }, [disabled]),
83
90
  onText: useCallback((e) => {
91
+ if (disabled)
92
+ return;
84
93
  onText(e.target.value);
85
94
  setOpened(true);
86
- }, [onText, text, setOpened]),
95
+ }, [disabled, onText, text, setOpened]),
87
96
  onSelect: useCallback((newVal) => {
97
+ if (disabled)
98
+ return;
88
99
  meta.onSelect?.(newVal, meta);
89
100
  const { onChange, onText, limit, min, value: val, keepQuery, keepOpened, setOpened, } = meta;
90
101
  if (!keepQuery)
@@ -97,8 +108,12 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
97
108
  onChange((deselect
98
109
  ? without(newVal)(value)
99
110
  : [...value, newVal]).slice(-limit));
100
- }, [meta]),
101
- onDeselect: useCallback((val) => meta.onChange(without(val)(meta.value)), [meta]),
111
+ }, [disabled, meta]),
112
+ onDeselect: useCallback((val) => {
113
+ if (disabled)
114
+ return;
115
+ meta.onChange(without(val)(meta.value));
116
+ }, [disabled, meta]),
102
117
  // whenever there is a query, override defaultIndex to 0, so the first result gets selected
103
118
  defaultIndex: query !== undefined && query?.length > 0 ? 0 : defaultIndex,
104
119
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "13.1.0",
3
+ "version": "13.2.0",
4
4
  "type": "module",
5
5
  "description": "A autocomplete input web component",
6
6
  "keywords": [
@@ -79,7 +79,7 @@
79
79
  },
80
80
  "dependencies": {
81
81
  "@lit-labs/virtualizer": "^2.0.0",
82
- "@neovici/cosmoz-dropdown": "^7.4.0",
82
+ "@neovici/cosmoz-dropdown": "^7.5.0",
83
83
  "@neovici/cosmoz-input": "^5.6.0",
84
84
  "@neovici/cosmoz-utils": "^6.19.0",
85
85
  "@pionjs/pion": "^2.7.1",