@neovici/cosmoz-autocomplete 9.0.0 → 9.0.2

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.
@@ -9,10 +9,10 @@ const style = css `
9
9
  background-position: right;
10
10
  animation: sweep 1.5s cubic-bezier(0.3, 1, 0.3, 1) infinite;
11
11
  border-radius: 3px;
12
- width: calc(100% - 50px);
12
+ width: calc(100% - 20px);
13
13
  max-width: 150px;
14
14
  height: 20px;
15
- margin: 10px 0 10px 33px;
15
+ margin: 10px;
16
16
  }
17
17
 
18
18
  :host-context([show-single]) {
@@ -25,8 +25,9 @@ export interface Props<I> extends Base<I> {
25
25
  disabled?: boolean;
26
26
  onFocus?: (focused?: boolean) => void;
27
27
  preserveOrder?: boolean;
28
+ defaultIndex?: number;
28
29
  }
29
- export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, ...thru }: Props<I>) => {
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>) => {
30
31
  active: boolean | undefined;
31
32
  query: string;
32
33
  textual: (i: I) => string;
@@ -38,6 +39,7 @@ export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onCh
38
39
  onText: (e: InputEvent) => void;
39
40
  onSelect: (newVal: I) => void;
40
41
  onDeselect: (val: I | I[]) => void;
42
+ defaultIndex: number | undefined;
41
43
  };
42
44
  export type RProps<I> = ReturnType<typeof useAutocomplete<I>>;
43
45
  export {};
@@ -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, ...thru }) => {
9
+ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, min, source, textProperty, textual: _textual, valueProperty, keepOpened, keepQuery, preserveOrder, defaultIndex, ...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);
@@ -79,5 +79,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
79
79
  : [...value, newVal]).slice(-limit));
80
80
  }, [meta]),
81
81
  onDeselect: useCallback((val) => meta.onChange(without(val)(meta.value)), [meta]),
82
+ // whenever there is a query, override defaultIndex to 0, so the first result gets selected
83
+ defaultIndex: query?.length > 0 ? 0 : defaultIndex,
82
84
  };
83
85
  };
@@ -5,8 +5,11 @@ export const useItems = ({ items, onSelect, defaultIndex = 0, }) => {
5
5
  index: defaultIndex,
6
6
  }), { index } = position, { length } = items;
7
7
  useEffect(() => {
8
+ // whenever the items list is empty, reset the position to defaultIndex
8
9
  setPosition({
9
- index: Math.min(position.index, items.length - 1),
10
+ index: position.index < 0
11
+ ? defaultIndex
12
+ : Math.min(position.index, items.length - 1),
10
13
  scroll: true,
11
14
  });
12
15
  }, [items, defaultIndex]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "9.0.0",
3
+ "version": "9.0.2",
4
4
  "description": "A autocomplete input web component",
5
5
  "keywords": [
6
6
  "lit-html",