Skip to content

Commit 664e6dd

Browse files
committed
Fix tab new loading
1 parent eb65854 commit 664e6dd

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

components/dash-core-components/src/components/Tabs.react.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ const EnhancedTab = ({
2727
amountOfTabs,
2828
colors,
2929
vertical,
30-
loading_state,
30+
componentPath,
3131
}) => {
32+
const ctx = window.dash_component_api.useDashContext();
33+
// We use the raw path here since it's up one level from
34+
// the tabs child.
35+
const isLoading = ctx.useLoading({rawPath: componentPath});
36+
3237
let tabStyle = style;
3338
if (disabled) {
3439
tabStyle = {tabStyle, ...disabled_style};
@@ -53,9 +58,7 @@ const EnhancedTab = ({
5358
}
5459
return (
5560
<div
56-
data-dash-is-loading={
57-
(loading_state && loading_state.is_loading) || undefined
58-
}
61+
data-dash-is-loading={isLoading}
5962
className={tabClassName}
6063
id={id}
6164
style={tabStyle}
@@ -227,7 +230,7 @@ export default class Tabs extends Component {
227230
vertical={this.props.vertical}
228231
amountOfTabs={amountOfTabs}
229232
colors={this.props.colors}
230-
loading_state={childProps.loading_state}
233+
componentPath={child.componentPath}
231234
/>
232235
);
233236
});

components/dash-table/src/dash-table/dash/fragments/DataTable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {propTypes} from '../DataTable';
1010

1111
const DataTable = props => {
1212
const ctx = window.dash_component_api.useDashContext();
13-
const isLoading = ctx.useLoading(
14-
loading =>
13+
const isLoading = ctx.useLoading({
14+
filterFunc: loading =>
1515
loading.property === 'data' ||
1616
loading.property === '' ||
1717
loading.property === undefined
18-
);
18+
});
1919
const id = useMemo(() => id || genRandomId('table-'), [id]);
2020
const sanitizer = useMemo(() => new Sanitizer(), []);
2121

dash/dash-renderer/src/wrapper/DashContext.tsx

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
import React, {useCallback, useContext, useMemo} from 'react';
22
import {useStore, useSelector, useDispatch} from 'react-redux';
3-
import {pathOr} from 'ramda';
3+
import {concat, pathOr} from 'ramda';
44

55
import {DashLayoutPath} from '../types/component';
66
import {LoadingPayload} from '../actions/loading';
77

8+
type LoadingFilterFunc = (loading: LoadingPayload) => boolean;
9+
10+
type LoadingOptions = {
11+
/**
12+
*
13+
*/
14+
extraPath?: DashLayoutPath;
15+
/**
16+
*
17+
*/
18+
rawPath?: boolean;
19+
/**
20+
* Function used to filter the properties of the loading component.
21+
*/
22+
filterFunc?: LoadingFilterFunc;
23+
};
24+
825
type DashContextType = {
926
componentPath: DashLayoutPath;
1027

11-
isLoading: () => boolean;
12-
useLoading: () => boolean;
28+
isLoading: (options?: LoadingOptions) => boolean;
29+
useLoading: (options?: LoadingOptions) => boolean;
1330

1431
// Give access to the right store.
1532
useSelector: typeof useSelector;
1633
useDispatch: typeof useDispatch;
1734
useStore: typeof useStore;
1835
};
1936

20-
type LoadingFilterFunc = (loading: LoadingPayload) => boolean;
21-
2237
export const DashContext = React.createContext<DashContextType>({} as any);
2338

2439
type DashContextProviderProps = {
@@ -35,19 +50,42 @@ export function DashContextProvider(props: DashContextProviderProps) {
3550
);
3651
const store = useStore();
3752

38-
const isLoading = useCallback(() => {
39-
const loading = pathOr(
40-
[],
41-
[stringPath],
42-
(store.getState() as any).loading
43-
);
44-
return loading.length > 0;
45-
}, [stringPath]);
53+
const isLoading = useCallback(
54+
(options?: LoadingOptions) => {
55+
const {extraPath, rawPath, filterFunc} = options || {};
56+
let loadingPath = [stringPath];
57+
if (extraPath) {
58+
loadingPath = [
59+
JSON.stringify(concat(componentPath, extraPath))
60+
];
61+
} else if (rawPath) {
62+
loadingPath = [JSON.stringify(rawPath)];
63+
}
64+
const loading = pathOr(
65+
[],
66+
loadingPath,
67+
(store.getState() as any).loading
68+
);
69+
return filterFunc
70+
? loading.filter(filterFunc).length > 0
71+
: loading.length > 0;
72+
},
73+
[stringPath]
74+
);
4675

4776
const useLoading = useCallback(
48-
(filterFunc?: LoadingFilterFunc) => {
77+
(options?: LoadingOptions) => {
78+
const {filterFunc, extraPath, rawPath} = options || {};
4979
return useSelector((state: any) => {
50-
const load = pathOr([], [stringPath], state.loading);
80+
let loadingPath = [stringPath];
81+
if (extraPath) {
82+
loadingPath = [
83+
JSON.stringify(concat(componentPath, extraPath))
84+
];
85+
} else if (rawPath) {
86+
loadingPath = [JSON.stringify(rawPath)];
87+
}
88+
const load = pathOr([], loadingPath, state.loading);
5189
if (filterFunc) {
5290
return load.filter(filterFunc).length > 0;
5391
}

0 commit comments

Comments
 (0)