Skip to content

Commit dd6c4af

Browse files
committed
memoize classnames
1 parent 577158a commit dd6c4af

File tree

1 file changed

+43
-33
lines changed
  • components/dash-core-components/src/components

1 file changed

+43
-33
lines changed

components/dash-core-components/src/components/Tabs.tsx

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useEffect, useRef, useState} from 'react';
1+
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
22
import {has, isNil} from 'ramda';
33

44
import LoadingElement from '../utils/_LoadingElement';
@@ -34,20 +34,24 @@ const EnhancedTab = ({
3434
// We use the raw path here since it's up one level from
3535
// the tabs child.
3636
const isLoading = ctx.useLoading({rawPath: componentPath});
37-
const tabStyle = {
38-
...style,
39-
...(disabled ? disabled_style : {}),
40-
...(selected ? selected_style : {}),
41-
};
42-
43-
const tabClassNames = [
44-
'tab',
45-
className,
46-
disabled ? 'tab--disabled' : null,
47-
disabled ? disabled_className : null,
48-
selected ? 'tab--selected' : null,
49-
selected ? selected_className : null,
50-
].filter(Boolean);
37+
const tabStyle = useMemo(() => {
38+
return {
39+
...style,
40+
...(disabled ? disabled_style : {}),
41+
...(selected ? selected_style : {}),
42+
};
43+
}, [style, disabled, disabled_style, selected, selected_style]);
44+
45+
const tabClassNames = useMemo(() => {
46+
return [
47+
'tab',
48+
className,
49+
disabled ? 'tab--disabled' : null,
50+
disabled ? disabled_className : null,
51+
selected ? 'tab--selected' : null,
52+
selected ? selected_className : null,
53+
].filter((el): el is string => Boolean(el));
54+
}, [className, disabled, disabled_className, selected, selected_className]);
5155

5256
let labelDisplay;
5357
if (typeof label === 'object') {
@@ -217,24 +221,30 @@ function Tabs({
217221

218222
const selectedTabContent = !isNil(selectedTab) ? selectedTab : '';
219223

220-
const tabContainerClassNames = [
221-
'tab-container',
222-
vertical ? 'tab-container--vert' : null,
223-
props.className,
224-
].filter(Boolean);
225-
226-
const tabContentClassNames = [
227-
'tab-content',
228-
vertical ? 'tab-content--vert' : null,
229-
props.content_className,
230-
].filter(Boolean);
231-
232-
const tabParentClassNames = [
233-
'tab-parent',
234-
vertical ? ' tab-parent--vert' : null,
235-
isAboveBreakpoint ? ' tab-parent--above-breakpoint' : null,
236-
props.parent_className,
237-
].filter(Boolean);
224+
const tabContainerClassNames = useMemo(() => {
225+
return [
226+
'tab-container',
227+
vertical ? 'tab-container--vert' : null,
228+
props.className,
229+
].filter((el): el is string => Boolean(el));
230+
}, [vertical, props.className]);
231+
232+
const tabContentClassNames = useMemo(() => {
233+
return [
234+
'tab-content',
235+
vertical ? 'tab-content--vert' : null,
236+
props.content_className,
237+
].filter((el): el is string => Boolean(el));
238+
}, [vertical, props.content_className]);
239+
240+
const tabParentClassNames = useMemo(() => {
241+
return [
242+
'tab-parent',
243+
vertical ? ' tab-parent--vert' : null,
244+
isAboveBreakpoint ? ' tab-parent--above-breakpoint' : null,
245+
props.parent_className,
246+
].filter((el): el is string => Boolean(el));
247+
}, [vertical, isAboveBreakpoint, props.parent_className]);
238248

239249
// Set CSS variables for dynamic styling
240250
const cssVars = {

0 commit comments

Comments
 (0)