Skip to content

Commit

Permalink
fix: fixed variable dropdown closing on select
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarRajput-7 committed Oct 2, 2024
1 parent 5ba9c9d commit 340270d
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { commaValuesParser } from 'lib/dashbaordVariables/customCommaValuesParse
import sortValues from 'lib/dashbaordVariables/sortVariableValues';
import { debounce, isArray, isString } from 'lodash-es';
import map from 'lodash-es/map';
import { ChangeEvent, memo, useEffect, useMemo, useState } from 'react';
import { ChangeEvent, memo, useEffect, useMemo, useRef, useState } from 'react';
import { useQuery } from 'react-query';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
Expand Down Expand Up @@ -278,6 +278,25 @@ function VariableItem({

const enableSelectAll = variableData.multiSelect && variableData.showALLOption;

const [variableDropdownOpen, setVariableDropdownOpen] = useState(false);
const wrapperRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent): void => {
if (
wrapperRef.current &&
!wrapperRef.current.contains(event.target as Node)
) {
setVariableDropdownOpen(false);
}
};

window.addEventListener('click', handleClickOutside);
return (): void => {
window.removeEventListener('click', handleClickOutside);
};
}, []);

const selectValue =
variableData.allSelected && enableSelectAll
? 'ALL'
Expand Down Expand Up @@ -412,7 +431,7 @@ function VariableItem({
<Typography.Text className="variable-name" ellipsis>
${variableData.name}
</Typography.Text>
<div className="variable-value">
<div className="variable-value" ref={wrapperRef}>
{variableData.type === 'TEXTBOX' ? (
<Input
placeholder="Enter value"
Expand Down Expand Up @@ -445,6 +464,10 @@ function VariableItem({
style={SelectItemStyle}
loading={isLoading}
showSearch
open={variableDropdownOpen}
onDropdownVisibleChange={(visible): void =>
setVariableDropdownOpen(visible)
}
data-testid="variable-select"
className="variable-select"
popupClassName="dropdown-styles"
Expand Down

0 comments on commit 340270d

Please sign in to comment.