-
-
Notifications
You must be signed in to change notification settings - Fork 95
Feature/onClose prop support #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| const PanelContainer = css({ | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of
onClosewe can give developer freedom to subscribe to both events for example perform some data fetchingonOpenand do cleanuponCloseso I think it will be better if we can have single simplified prop likeonMenuTogglereturning boolean value