Skip to content

✨ Add props to control menu close on select #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const SELECT_OPTIONS = [
"isMultiple",
"isDisabled",
"loading",
"isGroupOption"
"isGroupOption",
"closeOnSelect"
];

const printAlertContent = (element, value) => {
Expand All @@ -79,6 +80,8 @@ const printAlertContent = (element, value) => {
return printText("A loader appears on the field", value);
case "isGroupOption":
return printText("The options of the select field are grouped", value);
case "closeOnSelect":
return printText("Close dropdown every time an option is selected", value);
default:
return null;
}
Expand All @@ -91,6 +94,7 @@ const Home = () => {
const [value, setValue] = useState(null);
const [isClearable, setIsClearable] = useState(false);
const [isMultiple, setIsMultiple] = useState(false);
const [closeOnSelect, setCloseOnSelect] = useState(false);
const [isSearchable, setIsSearchable] = useState(false);
const [isDisabled, setIsDisabled] = useState(false);
const [isGroupOption, setIsGroupOption] = useState(false);
Expand Down Expand Up @@ -139,6 +143,10 @@ const Home = () => {
}
if (action === "get") return isMultiple;
break;
case "closeOnSelect":
if (action === "set") setCloseOnSelect(valueData);
if (action === "get") return closeOnSelect;
break;
case "isDisabled":
if (action === "set") setIsDisabled(valueData);
if (action === "get") return isDisabled;
Expand All @@ -158,7 +166,16 @@ const Home = () => {
break;
}
},
[isClearable, isDisabled, isGroupOption, isMultiple, isSearchable, loading, value]
[
isClearable,
isDisabled,
isGroupOption,
isMultiple,
closeOnSelect,
isSearchable,
loading,
value
]
);

const handleCheck = useCallback(
Expand Down Expand Up @@ -239,6 +256,7 @@ const Home = () => {
isClearable={isClearable}
isSearchable={isSearchable}
isMultiple={isMultiple}
closeOnSelect={closeOnSelect}
/*formatGroupLabel={(data) => (
<div className={`py-2 text-xs flex items-center justify-between`}>
<span className="font-bold">{data.label}</span>
Expand Down Expand Up @@ -282,6 +300,7 @@ const Home = () => {
{printAlertContent("isDisabled", isDisabled)}
{printAlertContent("loading", loading)}
{printAlertContent("isGroupOption", isGroupOption)}
{printAlertContent("closeOnSelect", closeOnSelect)}
</Alert>
</div>
)}
Expand Down
5 changes: 3 additions & 2 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const Select: React.FC<SelectProps> = ({
primaryColor = DEFAULT_THEME,
formatGroupLabel = null,
formatOptionLabel = null,
classNames
classNames,
closeOnSelect = false
}) => {
const [open, setOpen] = useState<boolean>(menuIsOpen);
const [list, setList] = useState<ListOption>(options);
Expand Down Expand Up @@ -95,7 +96,7 @@ const Select: React.FC<SelectProps> = ({
const handleValueChange = useCallback(
(selected: Option) => {
function update() {
if (!isMultiple && !Array.isArray(value)) {
if ((!isMultiple && !Array.isArray(value)) || closeOnSelect) {
closeDropDown();
onChange(selected);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface SelectProps {
formatGroupLabel?: ((data: GroupOption) => JSX.Element) | null;
formatOptionLabel?: ((data: Option) => JSX.Element) | null;
classNames?: ClassNames;
closeOnSelect?: boolean;
}