Skip to content

Commit 1a56d4f

Browse files
committed
Add useLoading
1 parent 5424fe0 commit 1a56d4f

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function wait(ms) {
1414
*/
1515

1616
export default class Clipboard extends React.Component {
17+
context = window.dash_component_api.DashContext;
18+
1719
constructor(props) {
1820
super(props);
1921
this.copyToClipboard = this.copyToClipboard.bind(this);
@@ -96,7 +98,7 @@ export default class Clipboard extends React.Component {
9698
}
9799

98100
async loading() {
99-
while (this.props.loading_state?.is_loading) {
101+
while (this.context.isLoading()) {
100102
await wait(100);
101103
}
102104
}
@@ -124,7 +126,7 @@ export default class Clipboard extends React.Component {
124126
}
125127

126128
render() {
127-
const {id, title, className, style, loading_state} = this.props;
129+
const {id, title, className, style} = this.props;
128130
const copyIcon = <FontAwesomeIcon icon={faCopy} />;
129131
const copiedIcon = <FontAwesomeIcon icon={faCheckCircle} />;
130132
const btnIcon = this.state.copied ? copiedIcon : copyIcon;
@@ -136,9 +138,6 @@ export default class Clipboard extends React.Component {
136138
style={style}
137139
className={className}
138140
onClick={this.onClickHandler}
139-
data-dash-is-loading={
140-
(loading_state && loading_state.is_loading) || undefined
141-
}
142141
>
143142
<i> {btnIcon}</i>
144143
</div>
@@ -196,24 +195,6 @@ Clipboard.propTypes = {
196195
*/
197196
className: PropTypes.string,
198197

199-
/**
200-
* Object that holds the loading state object coming from dash-renderer
201-
*/
202-
loading_state: PropTypes.shape({
203-
/**
204-
* Determines if the component is loading or not
205-
*/
206-
is_loading: PropTypes.bool,
207-
/**
208-
* Holds which property is loading
209-
*/
210-
prop_name: PropTypes.string,
211-
/**
212-
* Holds the name of the component that is loading
213-
*/
214-
component_name: PropTypes.string,
215-
}),
216-
217198
/**
218199
* Dash-assigned callback that gets fired when the value changes.
219200
*/

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type DashContextType = {
88
componentPath: DashLayoutPath;
99

1010
isLoading: () => boolean;
11+
useLoading: () => boolean;
1112

1213
// Give access to the right store.
1314
useSelector: typeof useSelector;
@@ -34,16 +35,21 @@ export function DashContextProvider(props: DashContextProviderProps) {
3435
const isLoading = useCallback(() => {
3536
const loading = pathOr(
3637
[],
37-
componentPath,
38+
[stringPath],
3839
(store.getState() as any).loading
3940
);
4041
return loading.length > 0;
4142
}, [stringPath]);
4243

44+
const useLoading = useCallback(() => {
45+
return useSelector(state => !!pathOr(false, [stringPath], state));
46+
}, [stringPath]);
47+
4348
const ctxValue = useMemo(() => {
4449
return {
4550
componentPath,
4651
isLoading,
52+
useLoading,
4753

4854
useSelector,
4955
useStore,

0 commit comments

Comments
 (0)