Skip to content

Commit

Permalink
Remove onActiveItemChange
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgio committed Oct 23, 2023
1 parent 712a430 commit ef32d72
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 35 deletions.
16 changes: 0 additions & 16 deletions src/components/MenuItem/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ describe('<MenuItem>', () => {
expect(onInitialItemChange).toHaveBeenCalledTimes(1);
});

it('conditionally calls `onInitialItemChange`', () => {
const onActiveItemChange = jest.fn();
const context = {
activeIndex: 1,
onActiveItemChange,
};

const { rerender } = render(
<Default context={context} props={{ option, position: 0 }} />
);
expect(onActiveItemChange).toHaveBeenCalledTimes(0);

rerender(<Default context={context} props={{ option, position: 1 }} />);
expect(onActiveItemChange).toHaveBeenCalledTimes(1);
});

it('changes the active state of the menu item', () => {
render(<Default context={{ activeIndex: 0 }} />);

Expand Down
2 changes: 0 additions & 2 deletions src/core/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface TypeaheadContextType {
initialItem?: Option;
inputNode: HTMLInputElement | null;
isOnlyResult: boolean;
onActiveItemChange: OptionHandler;
onAdd: OptionHandler;
onInitialItemChange: (option?: Option) => void;
onMenuItemClick: (option: Option, event: SelectEvent<HTMLElement>) => void;
Expand All @@ -24,7 +23,6 @@ export const defaultContext = {
initialItem: undefined,
inputNode: null,
isOnlyResult: false,
onActiveItemChange: noop,
onAdd: noop,
onInitialItemChange: noop,
onMenuItemClick: noop,
Expand Down
1 change: 0 additions & 1 deletion src/core/TypeaheadManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const TypeaheadManager = (props: TypeaheadManagerProps) => {
initialItem: props.initialItem,
inputNode: props.inputNode,
isOnlyResult: props.isOnlyResult,
onActiveItemChange: props.onActiveItemChange,
onAdd: props.onAdd,
onInitialItemChange: props.onInitialItemChange,
onMenuItemClick: props.onMenuItemClick,
Expand Down
5 changes: 1 addition & 4 deletions src/core/useItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function useItem<T extends HTMLElement>({
activeIndex,
id,
isOnlyResult,
onActiveItemChange,
onInitialItemChange,
onMenuItemClick,
setItem,
Expand All @@ -46,8 +45,6 @@ function useItem<T extends HTMLElement>({

useEffect(() => {
if (position === activeIndex) {
onActiveItemChange(option);

// Automatically scroll the menu as the user keys through it.
const node = itemRef.current;

Expand All @@ -57,7 +54,7 @@ function useItem<T extends HTMLElement>({
scrollMode: 'if-needed',
});
}
}, [activeIndex, onActiveItemChange, option, position]);
}, [activeIndex, option, position]);

const handleClick = useCallback(
(e: MouseEvent<T>) => {
Expand Down
14 changes: 3 additions & 11 deletions src/core/useTypeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,10 @@ function useTypeahead(
}

function onActiveIndexChange(index: number) {
setState((currentState) => ({
setState({
activeIndex: index,
activeItem: index >= 0 ? currentState.activeItem : undefined,
}));
}

function onActiveItemChange(activeItem: Option) {
// Don't update the active item if it hasn't changed.
if (!isEqual(activeItem, state.activeItem)) {
setState({ activeItem });
}
activeItem: index >= 0 ? items[index] : undefined,
});
}

function onBlur(e: React.FocusEvent<HTMLInputElement>) {
Expand Down Expand Up @@ -451,7 +444,6 @@ function useTypeahead(
inputRef: setInputNode,
isMenuShown,
isOnlyResult,
onActiveItemChange,
onAdd,
onBlur,
onChange: onInputChange,
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export interface TypeaheadManagerProps extends TypeaheadPropsAndState {
inputRef: RefCallback<HTMLInputElement>;
isMenuShown: boolean;
isOnlyResult: boolean;
onActiveItemChange: OptionHandler;
onAdd: OptionHandler;
onChange: ChangeEventHandler<HTMLInputElement>;
onClear: () => void;
Expand Down

0 comments on commit ef32d72

Please sign in to comment.