Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ISelectProps {
ItemRenderer?: Function;
selectAllLabel?: string;
isLoading?: boolean;
onClose?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of onClose we can give developer freedom to subscribe to both events for example perform some data fetching onOpen and do cleanup onClose so I think it will be better if we can have single simplified prop like onMenuToggle returning boolean value

disabled?: boolean;
disableSearch?: boolean;
shouldToggleOnHover?: boolean;
Expand Down
36 changes: 25 additions & 11 deletions src/multi-select/dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/**
* A generic dropdown component. It takes the children of the component
* and hosts it in the component. When the component is selected, it
* A generic dropdown component. It takes the children of the component
* and hosts it in the component. When the component is selected, it
* drops-down the contentComponent and applies the contentProps.
*/
import useOutsideClick from "@rooks/use-outside-click";
import { css } from "goober";
import React, { useRef, useState } from "react";
import React, { useRef, useState, useEffect } from "react";

import Arrow from "./arrow";
import Loading from "./loading";

interface IDropdownProps {
children?;
contentComponent;
contentProps: object;
isLoading?: boolean;
disabled?: boolean;
shouldToggleOnHover?: boolean;
labelledBy?: string;
contentProps: object;
onClose?;
contentComponent;
shouldToggleOnHover?: boolean;
Comment on lines 14 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's lot of unnecessary diff here and can be avoided easily can you reorder this as it was and just pass onMenuToggle prop?

}

const PanelContainer = css({
Expand Down Expand Up @@ -68,21 +69,31 @@ const DropdownHeading = css({
});

const Dropdown = ({
onClose,
children,
contentComponent: ContentComponent,
contentProps,
isLoading,
disabled,
shouldToggleOnHover,
isLoading,
labelledBy,
contentProps,
contentComponent: ContentComponent,
shouldToggleOnHover,
Comment on lines +72 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previous diff

}: IDropdownProps) => {
const [expanded, setExpanded] = useState(false);
const [hasFocus, setHasFocus] = useState(false);

const [onCloseEnabled, enableOnClose] = useState(false);
const wrapper: any = useRef();

useOutsideClick(wrapper, () => setExpanded(false));

/* eslint-disable react-hooks/exhaustive-deps */
useEffect(() => {
if (expanded === true) {
enableOnClose(true);
} else if (onCloseEnabled && !expanded) {
onClose();
}
}, [expanded]);

Comment on lines +88 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this logic even more like below this might automatically remove exhaustive-deps

useEffect(() => {
  onMenuToggle && onMenuToggle(expanded);
}, [expanded])

const handleKeyDown = e => {
switch (e.which) {
case 27: // Escape
Expand All @@ -98,12 +109,15 @@ const Dropdown = ({
}
e.preventDefault();
};

const handleHover = (iexpanded: boolean) => {
shouldToggleOnHover && setExpanded(iexpanded);
};

const handleFocus = e => {
e.target === wrapper && !hasFocus && setHasFocus(true);
};

const handleBlur = () => hasFocus && setHasFocus(false);
const handleMouseEnter = () => handleHover(true);
const handleMouseLeave = () => handleHover(false);
Expand Down
2 changes: 2 additions & 0 deletions src/multi-select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const MultiSelect = ({
overrideStrings,
onChange,
disabled,
onClose,
ItemRenderer,
selectAllLabel,
isLoading,
Expand All @@ -49,6 +50,7 @@ const MultiSelect = ({
<div className={`${MultiSelectBox} ${className}`}>
<Dropdown
isLoading={isLoading}
onClose={onClose}
contentComponent={SelectPanel}
shouldToggleOnHover={shouldToggleOnHover}
contentProps={{
Expand Down
1 change: 1 addition & 0 deletions stories/default.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const toStorybook = () => {
shouldToggleOnHover={boolean("shouldToggleOnHover", false)}
disableSearch={boolean("disableSearch", false)}
value={selected}
onClose={()=>{console.log("Closed")}}
disabled={boolean("disabled", false)}
onChange={setSelected}
labelledBy={text("labelledBy", "Select Fruits")}
Expand Down