Skip to content

Commit

Permalink
Add rovingInput option
Browse files Browse the repository at this point in the history
  • Loading branch information
szhsin committed Jan 12, 2024
1 parent f035aaf commit 2654a98
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
14 changes: 8 additions & 6 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ const useAutocomplete = ({
};
};

const autocomplete = () => ({
const autocomplete = ({
rovingInput
} = {}) => ({
_,
items,
onChange,
inputValue,
setInputValue,
focusIndex,
setFocusIndex,
Expand All @@ -104,8 +105,9 @@ const autocomplete = () => ({
setFocusIndex(-1);
}
};
const traverseItems = (isUp, baseIndex = -1) => {
const traverseItems = isUp => {
var _items$nextIndex;
const baseIndex = rovingInput ? -1 : 0;
let nextIndex = focusIndex;
const itemLength = items.length;
if (isUp) {
Expand All @@ -114,7 +116,7 @@ const autocomplete = () => ({
if (++nextIndex >= itemLength) nextIndex = baseIndex;
}
setFocusIndex(nextIndex);
setInputValue((_items$nextIndex = items[nextIndex]) != null ? _items$nextIndex : _.b);
rovingInput && setInputValue((_items$nextIndex = items[nextIndex]) != null ? _items$nextIndex : _.b);
};
return {
onItemClick: ({
Expand All @@ -128,7 +130,7 @@ const autocomplete = () => ({
setOpen(true);
},
onInputClick: () => setOpen(true),
onBlur: () => updateAndCloseList(inputValue),
onBlur: () => updateAndCloseList(items[focusIndex]),
onKeyDown: ({
key
}) => {
Expand All @@ -151,7 +153,7 @@ const autocomplete = () => ({
updateAndCloseList(items[focusIndex]);
break;
case 'Escape':
updateAndCloseList(inputValue);
updateAndCloseList(_.b);
break;
}
}
Expand Down
14 changes: 8 additions & 6 deletions dist/esm/features/autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const autocomplete = () => ({
const autocomplete = ({
rovingInput
} = {}) => ({
_,
items,
onChange,
inputValue,
setInputValue,
focusIndex,
setFocusIndex,
Expand All @@ -23,8 +24,9 @@ const autocomplete = () => ({
setFocusIndex(-1);
}
};
const traverseItems = (isUp, baseIndex = -1) => {
const traverseItems = isUp => {
var _items$nextIndex;
const baseIndex = rovingInput ? -1 : 0;
let nextIndex = focusIndex;
const itemLength = items.length;
if (isUp) {
Expand All @@ -33,7 +35,7 @@ const autocomplete = () => ({
if (++nextIndex >= itemLength) nextIndex = baseIndex;
}
setFocusIndex(nextIndex);
setInputValue((_items$nextIndex = items[nextIndex]) != null ? _items$nextIndex : _.b);
rovingInput && setInputValue((_items$nextIndex = items[nextIndex]) != null ? _items$nextIndex : _.b);
};
return {
onItemClick: ({
Expand All @@ -47,7 +49,7 @@ const autocomplete = () => ({
setOpen(true);
},
onInputClick: () => setOpen(true),
onBlur: () => updateAndCloseList(inputValue),
onBlur: () => updateAndCloseList(items[focusIndex]),
onKeyDown: ({
key
}) => {
Expand All @@ -70,7 +72,7 @@ const autocomplete = () => ({
updateAndCloseList(items[focusIndex]);
break;
case 'Escape':
updateAndCloseList(inputValue);
updateAndCloseList(_.b);
break;
}
}
Expand Down
25 changes: 8 additions & 17 deletions src/features/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { Feature } from '../common';

const autocomplete: () => Feature =
() =>
({
_,
items,
onChange,
inputValue,
setInputValue,
focusIndex,
setFocusIndex,
isOpen,
setOpen
}) => {
const autocomplete: (props?: { rovingInput?: boolean }) => Feature =
({ rovingInput } = {}) =>
({ _, items, onChange, setInputValue, focusIndex, setFocusIndex, isOpen, setOpen }) => {
const updateValue = (value: string) => {
_.b = value;
setInputValue(value);
Expand All @@ -29,7 +19,8 @@ const autocomplete: () => Feature =
}
};

const traverseItems = (isUp: boolean, baseIndex = -1) => {
const traverseItems = (isUp: boolean) => {
const baseIndex = rovingInput ? -1 : 0;
let nextIndex = focusIndex;
const itemLength = items.length;
if (isUp) {
Expand All @@ -38,7 +29,7 @@ const autocomplete: () => Feature =
if (++nextIndex >= itemLength) nextIndex = baseIndex;
}
setFocusIndex(nextIndex);
setInputValue(items[nextIndex] ?? _.b);
rovingInput && setInputValue(items[nextIndex] ?? _.b);
};

return {
Expand All @@ -52,7 +43,7 @@ const autocomplete: () => Feature =

onInputClick: () => setOpen(true),

onBlur: () => updateAndCloseList(inputValue),
onBlur: () => updateAndCloseList(items[focusIndex]),

onKeyDown: ({ key }) => {
switch (key) {
Expand All @@ -74,7 +65,7 @@ const autocomplete: () => Feature =
updateAndCloseList(items[focusIndex]);
break;
case 'Escape':
updateAndCloseList(inputValue);
updateAndCloseList(_.b);
break;
}
}
Expand Down
4 changes: 3 additions & 1 deletion types/features/autocomplete.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Feature } from '../common';
declare const autocomplete: () => Feature;
declare const autocomplete: (props?: {
rovingInput?: boolean;
}) => Feature;
export { autocomplete };

0 comments on commit 2654a98

Please sign in to comment.