Skip to content

Commit c520a82

Browse files
committed
Fix table loading.
1 parent fbdd794 commit c520a82

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

components/dash-table/src/dash-table/dash/Sanitizer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from 'dash-table/components/Table/props';
2323
import headerRows from 'dash-table/derived/header/headerRows';
2424
import resolveFlag from 'dash-table/derived/cell/resolveFlag';
25-
import dataLoading from 'dash-table/derived/table/data_loading';
2625
import {Column, Columns} from '../components/Table/props';
2726

2827
const D3_DEFAULT_LOCALE: INumberLocale = {
@@ -118,7 +117,7 @@ const getVisibleColumns = (
118117
);
119118

120119
export default class Sanitizer {
121-
sanitize(props: PropsWithDefaults): SanitizedProps {
120+
sanitize(props: PropsWithDefaults, isLoading: boolean): SanitizedProps {
122121
const locale_format = this.applyDefaultToLocale(props.locale_format);
123122
const data = props.data ?? [];
124123
const columns = props.columns
@@ -172,7 +171,7 @@ export default class Sanitizer {
172171
columns,
173172
props.filter_action
174173
),
175-
loading_state: dataLoading(props.loading_state),
174+
loading_state: isLoading,
176175
locale_format,
177176
selected_cells,
178177
visibleColumns
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Component} from 'react';
1+
import React, {useMemo} from 'react';
22

33
import RealTable from 'dash-table/components/Table';
44

@@ -8,27 +8,28 @@ import Sanitizer from '../Sanitizer';
88

99
import {propTypes, defaultProps} from '../DataTable';
1010

11-
export default class DataTable extends Component {
12-
constructor(props) {
13-
super(props);
14-
let id;
15-
this.getId = () => (id = id || genRandomId('table-'));
16-
this.sanitizer = new Sanitizer();
17-
}
18-
19-
render() {
20-
if (!isValidProps(this.props)) {
21-
return <div>Invalid props combination</div>;
22-
}
11+
const DataTable = props => {
12+
const ctx = window.dash_component_api.useDashContext();
13+
const isLoading = ctx.useLoading(
14+
loading =>
15+
loading.property === 'data' ||
16+
loading.property === '' ||
17+
loading.property === undefined
18+
);
19+
const id = useMemo(() => id || genRandomId('table-'), [id]);
20+
const sanitizer = useMemo(() => new Sanitizer(), []);
2321

24-
const sanitizedProps = this.sanitizer.sanitize(this.props);
25-
return this.props.id ? (
26-
<RealTable {...sanitizedProps} />
27-
) : (
28-
<RealTable {...sanitizedProps} id={this.getId()} />
29-
);
22+
if (!isValidProps(props)) {
23+
return <div>Invalid props combination</div>;
3024
}
31-
}
25+
26+
const sanitizedProps = sanitizer.sanitize(props, isLoading);
27+
return props.id ? (
28+
<RealTable {...sanitizedProps} />
29+
) : (
30+
<RealTable {...sanitizedProps} id={id} />
31+
);
32+
};
3233

3334
DataTable.defaultProps = defaultProps;
3435
DataTable.propTypes = propTypes;

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {useStore, useSelector, useDispatch} from 'react-redux';
33
import {pathOr} from 'ramda';
44

55
import {DashLayoutPath} from '../types/component';
6+
import {LoadingPayload} from '../actions/loading';
67

78
type DashContextType = {
89
componentPath: DashLayoutPath;
@@ -16,6 +17,8 @@ type DashContextType = {
1617
useStore: typeof useStore;
1718
};
1819

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

2124
type DashContextProviderProps = {
@@ -41,12 +44,18 @@ export function DashContextProvider(props: DashContextProviderProps) {
4144
return loading.length > 0;
4245
}, [stringPath]);
4346

44-
const useLoading = useCallback(() => {
45-
return useSelector((state: any) => {
46-
const load = pathOr([], [stringPath], state.loading);
47-
return load.length > 0;
48-
});
49-
}, [stringPath]);
47+
const useLoading = useCallback(
48+
(filterFunc?: LoadingFilterFunc) => {
49+
return useSelector((state: any) => {
50+
const load = pathOr([], [stringPath], state.loading);
51+
if (filterFunc) {
52+
return load.filter(filterFunc).length > 0;
53+
}
54+
return load.length > 0;
55+
});
56+
},
57+
[stringPath]
58+
);
5059

5160
const ctxValue = useMemo(() => {
5261
return {

0 commit comments

Comments
 (0)