Skip to content

Commit

Permalink
perf: Prevent rerendering and re-querying metadata of filters in hori…
Browse files Browse the repository at this point in the history
…zontal bar (apache#22389)
  • Loading branch information
kgabryje committed Dec 12, 2022
1 parent a5a0cf9 commit 605cfa0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* under the License.
*/
import React, { useContext, useMemo, useState } from 'react';
import {
createHtmlPortalNode,
InPortal,
OutPortal,
} from 'react-reverse-portal';
import { styled, SupersetTheme } from '@superset-ui/core';
import { FormItem as StyledFormItem, Form } from 'src/components/Form';
import { Tooltip } from 'src/components/Tooltip';
Expand Down Expand Up @@ -224,6 +229,7 @@ const FilterControl = ({
orientation = FilterBarOrientation.VERTICAL,
overflow = false,
}: FilterControlProps) => {
const portalNode = useMemo(() => createHtmlPortalNode(), []);
const [isFilterActive, setIsFilterActive] = useState(false);

const { name = '<undefined>' } = filter;
Expand Down Expand Up @@ -276,40 +282,45 @@ const FilterControl = ({
}, [orientation, overflow]);

return (
<FilterControlContainer
layout={
orientation === FilterBarOrientation.HORIZONTAL && !overflow
? 'horizontal'
: 'vertical'
}
>
<FilterCard
filter={filter}
isVisible={!isFilterActive && !isScrolling}
placement={filterCardPlacement}
<>
<InPortal node={portalNode}>
<FilterValue
dataMaskSelected={dataMaskSelected}
filter={filter}
showOverflow={showOverflow}
focusedFilterId={focusedFilterId}
onFilterSelectionChange={onFilterSelectionChange}
inView={inView}
parentRef={parentRef}
setFilterActive={setIsFilterActive}
orientation={orientation}
overflow={overflow}
/>
</InPortal>
<FilterControlContainer
layout={
orientation === FilterBarOrientation.HORIZONTAL && !overflow
? 'horizontal'
: 'vertical'
}
>
<div>
<FormItem
label={label}
required={filter?.controlValues?.enableEmptyFilter}
validateStatus={isMissingRequiredValue ? 'error' : undefined}
>
<FilterValue
dataMaskSelected={dataMaskSelected}
filter={filter}
showOverflow={showOverflow}
focusedFilterId={focusedFilterId}
onFilterSelectionChange={onFilterSelectionChange}
inView={inView}
parentRef={parentRef}
setFilterActive={setIsFilterActive}
orientation={orientation}
overflow={overflow}
/>
</FormItem>
</div>
</FilterCard>
</FilterControlContainer>
<FilterCard
filter={filter}
isVisible={!isFilterActive && !isScrolling}
placement={filterCardPlacement}
>
<div>
<FormItem
label={label}
required={filter?.controlValues?.enableEmptyFilter}
validateStatus={isMissingRequiredValue ? 'error' : undefined}
>
<OutPortal node={portalNode} />
</FormItem>
</div>
</FilterCard>
</FilterControlContainer>
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,4 @@ const FilterValue: React.FC<FilterControlProps> = ({
</StyledDiv>
);
};
export default FilterValue;
export default React.memo(FilterValue);

0 comments on commit 605cfa0

Please sign in to comment.