Skip to content

Commit

Permalink
fix(dropdown): fix Apply and Cancel buttons behavior (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudmanson authored Mar 22, 2023
1 parent 0a3c8e4 commit 47f3ee5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/core/Dropdown/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Default.args = {
buttons: false,
closeOnBlur: false,
disabled: false,
isTriggerChangeOnOptionClick: true,
isTriggerChangeOnOptionClick: false,
label: LABEL,
multiple: true,
onChange: noop,
Expand Down
29 changes: 21 additions & 8 deletions src/core/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ const Dropdown = <Multiple extends boolean | undefined = false>({

const [open, setOpen] = useState(false);

const shouldShowButtons =
buttons && !isTriggerChangeOnOptionClick && multiple;

return (
<>
<InputDropdownComponent
Expand All @@ -136,7 +139,7 @@ const Dropdown = <Multiple extends boolean | undefined = false>({
onClickAway={handleClickAway}
{...DropdownMenuProps}
>
{buttons ? (
{shouldShowButtons ? (
<div>
{buttonPosition === "left" ? (
<div>
Expand Down Expand Up @@ -219,10 +222,9 @@ const Dropdown = <Multiple extends boolean | undefined = false>({
return;
}

if (buttons && reason === "blur") {
if (closeOnBlur) {
handleCancel();
}
// (masoudmanson): When the dropdown loses focus, we will not close it immediately. Instead, we will update the values and
// only close the dropdown at the end of this block by calling setOpen(false).
if (shouldShowButtons && reason === "blur") {
return;
}

Expand All @@ -234,7 +236,10 @@ const Dropdown = <Multiple extends boolean | undefined = false>({
anchorEl.focus();
}

if (onClose) onClose();
if (closeOnBlur) {
if (onClose) onClose();
setOpen(false);
}
}

function handleChange(
Expand All @@ -257,13 +262,21 @@ const Dropdown = <Multiple extends boolean | undefined = false>({
}

function handleCancel() {
setPendingValue(null);
if (multiple) {
// (masoudmanson): To undo the latest actions made on the selections,
// we set the value of the selection to the pendingValue. This allows us to
// cancel any recent changes and restore the previous selection state.
setPendingValue(value as Value<DefaultDropdownMenuOption, true>);
}

if (anchorEl) {
anchorEl.focus();
}

if (onClose) onClose();
if (closeOnBlur) {
if (onClose) onClose();
setOpen(false);
}
}

function getInitialValue(): Value<DefaultDropdownMenuOption, Multiple> {
Expand Down

0 comments on commit 47f3ee5

Please sign in to comment.