@neovici/cosmoz-autocomplete 9.0.1 → 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.
|
@@ -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:
|
|
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]);
|