Skip to content

Commit

Permalink
fix(dropdown & button): add ability to click on the "button" part of …
Browse files Browse the repository at this point in the history
…the modal to close it when open

This also removes the requirement for onClick to be passed to Button

fix #210
  • Loading branch information
aVileBroker committed Jul 28, 2021
1 parent 575141f commit b5c7bd0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type ButtonProps = {
feedbackType?: FeedbackTypes;
interactionFeedbackProps?: Omit<InteractionFeedbackProps, 'children'>;
disabled?: boolean;
onClick: (...args: any[]) => void;
onClick?: (...args: any[]) => void;
onBlur?: (e: React.FocusEvent) => void;
onFocus?: (e: React.FocusEvent) => void;
onMouseDown?: (e: React.MouseEvent) => void;
Expand Down
20 changes: 13 additions & 7 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,18 @@ const Dropdown = ({
[multi, onClear, onSelect],
);

const clickHandler = useCallback(() => {
if (containerInternalRef && containerInternalRef.current) {
// Focus the container even when clicking
containerInternalRef.current.focus();
}
}, []);
const handleMouseDownOnButton = useCallback(
(e: React.MouseEvent) => {
if (isOpen) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - It's okay if target is null in this case as we want it to close regardless
handleBlur(e);
} else {
handleFocus();
}
},
[isOpen, handleBlur, handleFocus],
);

const keyDownHandler = useCallback(
({ key }) => {
Expand Down Expand Up @@ -471,7 +477,7 @@ const Dropdown = ({
StyledContainer={StyledValueContainer}
id={`${name}-dropdown-button`}
color={defaultedColor}
onClick={clickHandler}
onMouseDown={handleMouseDownOnButton}
variant={variant}
containerRef={valueContainerRef}
{...valueContainerProps}
Expand Down

0 comments on commit b5c7bd0

Please sign in to comment.