Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for dash 3 #499

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
aa2b717
updates for dash 3 - persistence, loading, dashprivate_layout
AnnMarieW Jan 31, 2025
0bb88c1
change prop from required to optional so default can be set.
AnnMarieW Jan 31, 2025
9b6f087
fix default props
AnnMarieW Feb 1, 2025
a0895ac
updated stepper
AnnMarieW Feb 1, 2025
de71432
handle components like dcc.loading
AnnMarieW Feb 1, 2025
46cc48c
handle components like Checkbox and refactor dash3 utilities
AnnMarieW Feb 2, 2025
c4d80d9
updates for dash 3 - persistence, loading, dashprivate_layout
AnnMarieW Jan 31, 2025
0aae73d
change prop from required to optional so default can be set.
AnnMarieW Jan 31, 2025
56201ee
fix default props
AnnMarieW Feb 1, 2025
4ada77c
updated stepper
AnnMarieW Feb 1, 2025
6422feb
handle components like dcc.loading
AnnMarieW Feb 1, 2025
a5fce26
handle components like Checkbox and refactor dash3 utilities
AnnMarieW Feb 2, 2025
b54e17b
Merge remote-tracking branch 'origin/update-for-dash-3-0' into update…
AnnMarieW Feb 3, 2025
bbb2a15
fix getLoadingStateChildren after review
AnnMarieW Feb 4, 2025
aa986b4
update Hovercard to remove dashprivate
AnnMarieW Feb 4, 2025
ee3e9e7
update Menu to remove dashprivate
AnnMarieW Feb 4, 2025
29e7181
update Popover to remove dashprivate
AnnMarieW Feb 4, 2025
6901204
update Timeline to remove dashprivate
AnnMarieW Feb 4, 2025
1361186
update Stepper
AnnMarieW Feb 4, 2025
70aefe3
update Combobox components
AnnMarieW Feb 4, 2025
f5938b9
update react 18.3 check
AnnMarieW Feb 4, 2025
8af9abb
fix typo
AnnMarieW Feb 4, 2025
9503d92
update data-dash-is-loading
AnnMarieW Feb 5, 2025
9596c6a
update defaultProps and persistence
AnnMarieW Feb 5, 2025
fefd7cf
bump to require dash 3
AnnMarieW Feb 5, 2025
6298050
bump to require dash 3
AnnMarieW Feb 5, 2025
fea7ec5
revert requirements for dash 3 for dmc pre-release
AnnMarieW Feb 6, 2025
c17eef8
add changelog
AnnMarieW Feb 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/ts/components/core/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "props/modal";
import { StylesApiProps } from "props/styles";
import React, { useEffect, useState } from "react";
import { getLoadingState } from "../../utils/dash3";

type DrawerPosition = "bottom" | "left" | "right" | "top";

Expand All @@ -31,8 +32,8 @@ interface Props extends StylesApiProps, ModalBaseProps, DashBaseProps {
}

/** Drawer */
const Drawer = (props: Props) => {
const { setProps, loading_state, opened, children, ...others } = props;
const Drawer = ({ setProps, loading_state, opened = false, children, ...others }: Props) => {

const [open, setOpen] = useState(opened);

useEffect(() => {
Expand All @@ -46,9 +47,7 @@ const Drawer = (props: Props) => {

return (
<MantineDrawer
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
data-dash-is-loading={getLoadingState(loading_state) || undefined}
opened={open}
onClose={onClose}
{...others}
Expand All @@ -58,6 +57,4 @@ const Drawer = (props: Props) => {
);
};

Drawer.defaultProps = { opened: false };

export default Drawer;
11 changes: 4 additions & 7 deletions src/ts/components/core/Skeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BoxProps } from "props/box";
import { DashBaseProps } from "props/dash";
import { StylesApiProps } from "props/styles";
import React from "react";
import { getLoadingStateChildren } from "../../utils/dash3";

interface Props extends BoxProps, StylesApiProps, DashBaseProps {
/** Determines whether Skeleton overlay should be displayed, `true` by default */
Expand All @@ -22,23 +23,19 @@ interface Props extends BoxProps, StylesApiProps, DashBaseProps {
}

/** Skeleton */
const Skeleton = (props: Props) => {
const { setProps, visible, loading_state, children, ...others } = props;
const Skeleton = ({ setProps, visible = true, loading_state, children, ...others }: Props) => {

return (
<MantineSkeleton
{...others}
visible={visible || (loading_state && loading_state.is_loading)}
visible={visible || getLoadingStateChildren(loading_state, children)}
>
{children}
</MantineSkeleton>
);
};

// for dash2
Skeleton._dashprivate_isLoadingComponent = true;

Skeleton.defaultProps = {
visible: true,
};

export default Skeleton;
20 changes: 7 additions & 13 deletions src/ts/components/core/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BoxProps } from "props/box";
import { DashBaseProps, PersistenceProps } from "props/dash";
import { StylesApiProps } from "props/styles";
import React from "react";
import { setPersistence, getLoadingState, applyDashProps } from "../../../utils/dash3";

interface Props
extends BoxProps,
Expand Down Expand Up @@ -49,8 +50,7 @@ interface Props
}

/** Checkbox */
const Checkbox = (props: Props) => {
const {
const Checkbox = ({
setProps,
loading_state,
persistence,
Expand All @@ -59,30 +59,24 @@ const Checkbox = (props: Props) => {
icon,
indeterminateIcon,
...others
} = props;
}: Props) => {


const iconFunc = ({ indeterminate, ...others }) => {
const selected: any = indeterminate ? indeterminateIcon : icon;
Object.assign(selected.props._dashprivate_layout.props, others);
return selected;
return applyDashProps(selected, others);
};

return (
<MantineCheckbox
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
data-dash-is-loading={getLoadingState(loading_state) || undefined}
onChange={(ev) => setProps({ checked: ev.currentTarget.checked })}
icon={icon || indeterminateIcon ? iconFunc : undefined}
{...others}
/>
);
};

Checkbox.defaultProps = {
persisted_props: ["checked"],
persistence_type: "local",
checked: false,
};
setPersistence(Checkbox, ["checked"] )

export default Checkbox;
45 changes: 18 additions & 27 deletions src/ts/components/core/input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DashBaseProps, PersistenceProps, DebounceProps } from "props/dash";
import { __BaseInputProps } from "props/input";
import { StylesApiProps } from "props/styles";
import React, { useState } from "react";
import { setPersistence, getLoadingState } from "../../../utils/dash3";

interface Props
extends BoxProps,
Expand Down Expand Up @@ -61,23 +62,23 @@ interface Props
autoComplete?: string;
/** Sets disabled attribute on the input element */
disabled?: boolean;

}

/** The NumberInput component allows users to input numeric values */
const NumberInput = (props: Props) => {
const {
setProps,
persistence,
persisted_props,
persistence_type,
loading_state,
value,
n_submit,
n_blur,
debounce,
...others
} = props;

const NumberInput = ({
setProps,
persistence,
persisted_props,
persistence_type,
loading_state,
value,
n_submit = 0,
n_blur = 0,
debounce = false,
autoComplete = "off",
...others
}: Props) => {

const [val, setVal] = useState(value);

Expand Down Expand Up @@ -112,27 +113,17 @@ const NumberInput = (props: Props) => {

return (
<MantineNumberInput
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
data-dash-is-loading={getLoadingState(loading_state) || undefined}
onChange={setVal}
value={val}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
autoComplete={autoComplete}
{...others}
/>
);
};


NumberInput.defaultProps = {
debounce: false,
value: "",
persisted_props: ["value"],
persistence_type: "local",
n_submit: 0,
n_blur: 0,
autoComplete: "off"
};
setPersistence(NumberInput)

export default NumberInput;
12 changes: 4 additions & 8 deletions src/ts/components/core/stepper/Stepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DashBaseProps } from "props/dash";
import { StylesApiProps } from "props/styles";
import { omit } from "ramda";
import React, { useState } from "react";
import { getChildLayout, getLoadingState } from "../../../utils/dash3";

interface Props extends BoxProps, DashBaseProps, StylesApiProps {
/** Index of the active step */
Expand Down Expand Up @@ -47,8 +48,7 @@ interface Props extends BoxProps, DashBaseProps, StylesApiProps {
}

/** Stepper */
const Stepper = (props: Props) => {
const { setProps, loading_state, active, children, ...others } = props;
const Stepper = ({ setProps, loading_state, active, children, ...others }: Props) => {

const [act, setAct] = useState(active);

Expand All @@ -58,23 +58,21 @@ const Stepper = (props: Props) => {

return (
<MantineStepper
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
data-dash-is-loading={ getLoadingState(loading_state) || undefined
}
active={act}
onStepClick={(a) => setProps({active: a})}
{...others}
>
{React.Children.map(children, (child: any, index) => {
const childType = child.props._dashprivate_layout.type;
const { type: childType, props: childProps } = getChildLayout(child);
if (childType === "StepperCompleted") {
return (
<MantineStepper.Completed>
{child}
</MantineStepper.Completed>
);
} else {
const childProps = child.props._dashprivate_layout.props;
const renderedProps = renderDashComponents(
omit(["children"], childProps),
[
Expand All @@ -97,6 +95,4 @@ const Stepper = (props: Props) => {
);
};

Stepper.defaultProps = {};

export default Stepper;
2 changes: 0 additions & 2 deletions src/ts/components/styles/MantineProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ const MantineProvider = (props: Props) => {
);
};

MantineProvider.defaultProps = {};

export default MantineProvider;
2 changes: 1 addition & 1 deletion src/ts/props/dash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface DashBaseProps {
"aria-*"?: string;
/** tab-index */
tabIndex?: number;
/** Object that holds the loading state object coming from dash-renderer */
/** Object that holds the loading state object coming from dash-renderer. For use with dash<3. */
loading_state?: {
/** Determines if the component is loading or not */
is_loading: boolean;
Expand Down
81 changes: 81 additions & 0 deletions src/ts/utils/dash3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Utility functions to support both Dash 2 and Dash 3 components
*
* For more details, refer to the Dash documentation:
* Dash 3 for Component Developers - https://dash.plotly.com/dash-3-for-component-developers
*/
import React from "react";
import { DashBaseProps } from "props/dash";

/** check for dash version */
export const isDash3 = (): boolean => {
return !!(window as any).dash_component_api;
};

/** Apply persistence settings based on React version */
export const setPersistence = (Component: any, props: string[] = ["value"]): void => {
const persistence = { persisted_props: props, persistence_type: "local" };

if (parseFloat(React.version) < 18.3) {
Component.defaultProps = persistence;
} else {
Component.dashPersistence = persistence;
}
};

/** Get the loading state for the current component */
export const getLoadingState = (loading_state?: DashBaseProps["loading_state"]): boolean => {
return isDash3()
? (window as any).dash_component_api.useDashContext().useLoading()
: loading_state?.is_loading ?? false;
};

/** Get layout information for a child component */
export const getChildLayout = (child: any): { type: any; props: any } => {
if (isDash3()) {
return (window as any).dash_component_api.getLayout(child.props.componentPath);
}

return {
type: child.props?._dashprivate_layout?.type,
props: child.props?._dashprivate_layout?.props,
};
};

/** Get only the props of a child component */
export const getChildProps = (child: any): any => getChildLayout(child).props;

/** Get the loading state of child components */
export const getLoadingStateChildren = (
loading_state?: DashBaseProps["loading_state"],
children?: any
): boolean => {
if (isDash3()) {
const ctx = (window as any).dash_component_api.useDashContext();
const childArray = React.Children.toArray(children);

// Get loading states for all children
const loadingStates = childArray.map((child: any) =>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you use some here rather than map you can benefit from short-circuit evaluation. I.e. you're return true the first time you encounter a loading child rather than calling useLoading on all children then calling some on the resultant array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated 🙂

ctx.useLoading({ rawPath: child.props?.componentPath })
);

return loadingStates.some(Boolean); // Check if any child is loading
}

return loading_state?.is_loading ?? false; // Dash 2 fallback
};



/** Apply props to a Dash component, handling Dash 2 & Dash 3 compatibility */
export const applyDashProps = (component: any, props: Record<string, any>) => {
if (isDash3()) {
return React.cloneElement(component, { ...props });
}

if (component.props?._dashprivate_layout?.props) {
Object.assign(component.props._dashprivate_layout.props, props);
}

return component;
};