Skip to content

Commit 51a1d51

Browse files
committed
Add custom reducer to Multiselect
1 parent e0322ca commit 51a1d51

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

example/MultiSelect/MultiSelect.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import classNames from 'classnames';
22
import React, { ChangeEvent, KeyboardEvent, useMemo, useState } from 'react';
3-
import { useDropdown } from '../../src';
3+
import { DropdownState, ReducerAction, StateChangeType, useDropdown } from '../../src';
44
import './MultiSelect.css';
55

66
export type Item = {
@@ -52,6 +52,18 @@ export const Dropdown: React.FC<Props> = ({ onSelect, values }) => {
5252
const [inputValue, setInputValue] = useState<string>('');
5353
const [isFocused, setFocused] = useState<Boolean>(false);
5454

55+
const reducer = (state: DropdownState, action: ReducerAction) => {
56+
const { type } = action;
57+
58+
switch (type) {
59+
case StateChangeType.ITEM_CLICK:
60+
return { ...state, isOpen: true };
61+
62+
default:
63+
return state;
64+
}
65+
};
66+
5567
const handleSelect = (item: Item) => {
5668
let newOptions = [];
5769
if (values.some((el) => el.value === item.value)) {
@@ -75,7 +87,7 @@ export const Dropdown: React.FC<Props> = ({ onSelect, values }) => {
7587
getMenuProps,
7688
getItemProps,
7789
setOpen,
78-
} = useDropdown<Item>({ items: options, onSelect: handleSelect });
90+
} = useDropdown<Item>({ items: options, onSelect: handleSelect, reducer });
7991

8092
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
8193
setOpen(true);
@@ -142,7 +154,7 @@ export const Dropdown: React.FC<Props> = ({ onSelect, values }) => {
142154
autoComplete="off"
143155
/>
144156

145-
{(isOpen || isFocused) && (
157+
{isOpen && (
146158
<ul className="menu" {...(getMenuProps() as any)}>
147159
{options.length === 0 ? (
148160
<li>No data</li>

0 commit comments

Comments
 (0)