@neovici/cosmoz-autocomplete 13.1.1 → 13.2.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.
@@ -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 };
@@ -43,8 +43,8 @@ const autocomplete = (props) => {
43
43
  ?always-float-label=${value?.length > 0 || alwaysFloatLabel}
44
44
  ?readonly=${isSingle}
45
45
  ?disabled=${disabled}
46
- ?invalid=${guard([source$], () => until(source$.then(() => invalid, () => true), invalid))}
47
- .errorMessage=${guard([source$], () => until(source$.then(() => errorMessage, (e) => e.message), errorMessage))}
46
+ ?invalid=${guard([source$, invalid], () => until(source$.then(() => invalid, () => true), invalid))}
47
+ .errorMessage=${guard([source$, errorMessage], () => until(source$.then(() => errorMessage, (e) => e.message), errorMessage))}
48
48
  .value=${live(text)}
49
49
  @value-changed=${onText}
50
50
  autocomplete="off"
@@ -103,5 +103,6 @@ const autocomplete = (props) => {
103
103
  'item-height',
104
104
  'item-limit',
105
105
  'wrap',
106
+ 'lazy-open',
106
107
  ];
107
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, disabled, }: 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, disabled, }) => {
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({
@@ -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,6 +80,7 @@ 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) => {
81
86
  if (disabled)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "13.1.1",
3
+ "version": "13.2.1",
4
4
  "type": "module",
5
5
  "description": "A autocomplete input web component",
6
6
  "keywords": [