Skip to content

Commit

Permalink
Merge pull request #43 from szhsin/feat/onchange-type
Browse files Browse the repository at this point in the history
feat: close on enter
  • Loading branch information
szhsin authored Jul 8, 2024
2 parents 631a966 + 1a2aa01 commit 064009b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 62 deletions.
25 changes: 10 additions & 15 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ const autocompleteLite = ({
}
break;
case 'Enter':
if (open && focusItem) {
resetState(selectItemOrAction(focusItem));
if (open) {
if (focusItem) {
resetState(selectItemOrAction(focusItem));
} else if (!select) {
resetState(true);
}
}
break;
case 'Escape':
Expand Down Expand Up @@ -488,15 +492,10 @@ const autoInline = ({
})
});

const supercomplete = ({
getFocusItem,
...rest
}) => mergeModules(autocomplete({
...rest,
const supercomplete = props => mergeModules(autocomplete({
...props,
rovingText: true
}), autoInline({
getFocusItem
}));
}), autoInline(props));

const linearTraversal = ({
traverseInput,
Expand Down Expand Up @@ -542,11 +541,7 @@ const groupedTraversal = ({
...restProps
}) => {
const groups = isArray(groupedItems) ? groupedItems : Object.values(groupedItems);
const items = [];
groups.forEach(group => {
const itemsInGroup = isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : [];
items.push(...itemsInGroup);
});
const items = groups.reduce((accu, group) => accu.concat(isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : []), []);
return linearTraversal({
...restProps,
items
Expand Down
8 changes: 6 additions & 2 deletions dist/esm/features/atom/autocompleteLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ const autocompleteLite = ({
}
break;
case 'Enter':
if (open && focusItem) {
resetState(selectItemOrAction(focusItem));
if (open) {
if (focusItem) {
resetState(selectItemOrAction(focusItem));
} else if (!select) {
resetState(true);
}
}
break;
case 'Escape':
Expand Down
11 changes: 3 additions & 8 deletions dist/esm/features/molecule/supercomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import { mergeModules } from '../../utils/mergeModules.js';
import { autocomplete } from './autocomplete.js';
import { autoInline } from '../atom/autoInline.js';

const supercomplete = ({
getFocusItem,
...rest
}) => mergeModules(autocomplete({
...rest,
const supercomplete = props => mergeModules(autocomplete({
...props,
rovingText: true
}), autoInline({
getFocusItem
}));
}), autoInline(props));

export { supercomplete };
6 changes: 1 addition & 5 deletions dist/esm/traversals/groupedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ const groupedTraversal = ({
...restProps
}) => {
const groups = isArray(groupedItems) ? groupedItems : Object.values(groupedItems);
const items = [];
groups.forEach(group => {
const itemsInGroup = isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : [];
items.push(...itemsInGroup);
});
const items = groups.reduce((accu, group) => accu.concat(isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : []), []);
return linearTraversal({
...restProps,
items
Expand Down
8 changes: 6 additions & 2 deletions src/features/atom/autocompleteLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ const autocompleteLite =
}
break;
case 'Enter':
if (open && focusItem) {
resetState(selectItemOrAction(focusItem));
if (open) {
if (focusItem) {
resetState(selectItemOrAction(focusItem));
} else if (!select) {
resetState(true);
}
}
break;
case 'Escape':
Expand Down
28 changes: 12 additions & 16 deletions src/features/molecule/supercomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ type SupercompleteFeature<T> = MergedFeature<
[AutocompleteFeature<T>, AutoInlineFeature<T>]
>;

const supercomplete = <T>({
getFocusItem,
...rest
}: Pick<
FeatureProps<T>,
| 'getFocusItem'
| 'select'
| 'selectOnBlur'
| 'deselectOnClear'
| 'deselectOnChange'
| 'closeOnSelect'
>): SupercompleteFeature<T> =>
mergeModules(
autocomplete<T>({ ...rest, rovingText: true }),
autoInline<T>({ getFocusItem })
);
const supercomplete = <T>(
props: Pick<
FeatureProps<T>,
| 'getFocusItem'
| 'select'
| 'selectOnBlur'
| 'deselectOnClear'
| 'deselectOnChange'
| 'closeOnSelect'
>
): SupercompleteFeature<T> =>
mergeModules(autocomplete<T>({ ...props, rovingText: true }), autoInline<T>(props));

export { type SupercompleteFeature, supercomplete };
6 changes: 3 additions & 3 deletions src/hooks/useAutocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useRef } from 'react';
import type { AutocompleteProps, AutocompleteState } from '../common';
import type { AutocompleteProps, AutocompleteState, Contextual } from '../common';

const useAutocomplete = <T, FeatureYield extends object>({
value,
Expand All @@ -21,11 +21,11 @@ const useAutocomplete = <T, FeatureYield extends object>({
setOpen
};

const contextual = {
const contextual: Contextual<T> = {
tmpValue,
setTmpValue,
value,
onChange: (newValue?: string | undefined) => value != newValue && onChange?.(newValue),
onChange: (newValue) => value != newValue && onChange?.(newValue),
...passthrough,
...state
};
Expand Down
15 changes: 5 additions & 10 deletions src/traversals/groupedTraversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ const groupedTraversal = <G, T>({
...restProps
}: GroupedTraversalProps<G, T>): Traversal<T> => {
const groups = isArray(groupedItems) ? groupedItems : Object.values(groupedItems);

const items: T[] = [];
groups.forEach((group) => {
const itemsInGroup = isArray(group)
? group
: getItemsInGroup
? getItemsInGroup(group)
: [];
items.push(...itemsInGroup);
});
const items = groups.reduce<T[]>(
(accu, group) =>
accu.concat(isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : []),
[]
);

return linearTraversal({ ...restProps, items });
};
Expand Down
2 changes: 1 addition & 1 deletion types/features/molecule/supercomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type SupercompleteFeature<T> = MergedFeature<T, [
AutocompleteFeature<T>,
AutoInlineFeature<T>
]>;
declare const supercomplete: <T>({ getFocusItem, ...rest }: Pick<FeatureProps<T>, "select" | "selectOnBlur" | "deselectOnClear" | "deselectOnChange" | "closeOnSelect" | "getFocusItem">) => SupercompleteFeature<T>;
declare const supercomplete: <T>(props: Pick<FeatureProps<T>, "select" | "selectOnBlur" | "deselectOnClear" | "deselectOnChange" | "closeOnSelect" | "getFocusItem">) => SupercompleteFeature<T>;
export { type SupercompleteFeature, supercomplete };

0 comments on commit 064009b

Please sign in to comment.