Skip to content

Commit e753d88

Browse files
authored
Merge branch 'main' into forman-x-dont_render_falsifying_children
2 parents 9c355f4 + fe2985c commit e753d88

File tree

20 files changed

+330
-69
lines changed

20 files changed

+330
-69
lines changed

chartlets.js/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Replacing entire components if a related component `StateChange`
44
has an empty `property`. (#38)
55

6+
* Added handy hooks `useContributions` and `useComponentChangeHandlers`.
7+
8+
69
## Version 0.0.22 (from 2024/11/19)
710

811
* Improved robustness while rendering the in `Select` component

chartlets.js/src/demo/components/PanelsControl.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import Checkbox from "@mui/material/Checkbox";
22
import FormControlLabel from "@mui/material/FormControlLabel";
33
import FormGroup from "@mui/material/FormGroup";
44

5+
import { useContributions } from "@/lib";
56
import { hidePanel } from "@/demo/actions/hidePanel";
67
import { showPanel } from "@/demo/actions/showPanel";
7-
import { usePanelStates } from "@/demo/hooks";
8+
import type { PanelState } from "@/demo/types";
89

910
function PanelsControl() {
10-
const panelStates = usePanelStates();
11+
const panelStates = useContributions<PanelState>("panels");
1112
if (!panelStates) {
1213
// Ok, not ready yet
1314
return null;

chartlets.js/src/demo/components/PanelsRow.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { type ComponentChangeEvent, handleComponentChange } from "@/lib";
2-
import { usePanelStates } from "@/demo/hooks";
1+
import type { PanelState } from "@/demo/types";
2+
import { useComponentChangeHandlers, useContributions } from "@/lib/hooks";
33
import Panel from "./Panel";
44

55
function PanelsRow() {
6-
const panelStates = usePanelStates();
6+
const panelStates = useContributions<PanelState>("panels");
7+
const panelChangeHandlers = useComponentChangeHandlers(
8+
"panels",
9+
panelStates?.length || 0,
10+
);
711
if (!panelStates) {
812
// Ok, not ready yet
913
return null;
1014
}
1115

12-
const handlePanelChange = (
13-
panelIndex: number,
14-
panelEvent: ComponentChangeEvent,
15-
) => {
16-
handleComponentChange("panels", panelIndex, panelEvent);
17-
};
1816
const panels = panelStates.map((panelState, panelIndex) => {
1917
const { container, component, componentResult } = panelState;
2018
return (
@@ -24,7 +22,7 @@ function PanelsRow() {
2422
componentProps={component}
2523
componentStatus={componentResult.status}
2624
componentError={componentResult.error}
27-
onChange={(e) => handlePanelChange(panelIndex, e)}
25+
onChange={panelChangeHandlers[panelIndex]}
2826
/>
2927
);
3028
});

chartlets.js/src/demo/hooks.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

chartlets.js/src/lib/actions/helpers/applyStateChangeRequests.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe("Test that applyComponentStateChange()", () => {
114114
expect(newState).toBe(componentTree);
115115
});
116116

117-
it("replaces state if no property given", () => {
117+
it("replaces state if property is empty string", () => {
118118
const value: BoxState = {
119119
type: "Box",
120120
id: "b1",

chartlets.js/src/lib/hooks.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import type { StoreState } from "@/lib/types/state/store";
22
import { store } from "@/lib/store";
33
import { useMemo } from "react";
44
import type { ContributionState } from "@/lib/types/state/contribution";
5+
import type {
6+
ComponentChangeEvent,
7+
ComponentChangeHandler,
8+
} from "@/lib/types/state/event";
9+
import { handleComponentChange } from "@/lib/actions/handleComponentChange";
510

611
const selectConfiguration = (state: StoreState) => state.configuration;
712

@@ -32,3 +37,44 @@ export function makeContributionsHook<S extends object = object>(
3237
}, [contributions]);
3338
};
3439
}
40+
41+
/**
42+
* A hook that retrieves the contributions for the given contribution
43+
* point given by `contribPoint`.
44+
*
45+
* @param contribPoint Contribution point name.
46+
* @typeParam S Type of the container state.
47+
*/
48+
export function useContributions<S extends object = object>(
49+
contribPoint: string,
50+
): ContributionState<S>[] {
51+
const contributionsRecord = useContributionsRecord();
52+
return contributionsRecord[contribPoint] as ContributionState<S>[];
53+
}
54+
55+
/**
56+
* A hook that creates an array of length `numContribs` with stable
57+
* component change handlers of type `ComponentChangeHandler` for
58+
* the contribution point given by `contribPoint`.
59+
*
60+
* @param contribPoint Contribution point name.
61+
* @param numContribs Number of contributions. This should be the length
62+
* of the array of contributions you get using the `useContributions` hook.
63+
*/
64+
export function useComponentChangeHandlers(
65+
contribPoint: string,
66+
numContribs: number,
67+
): ComponentChangeHandler[] {
68+
return useMemo(
69+
() =>
70+
Array.from({ length: numContribs }).map(
71+
(_, contribIndex) => (componentEvent: ComponentChangeEvent) =>
72+
void handleComponentChange(
73+
contribPoint,
74+
contribIndex,
75+
componentEvent,
76+
),
77+
),
78+
[contribPoint, numContribs],
79+
);
80+
}

chartlets.js/src/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ export {
1818
useExtensions,
1919
useContributionsResult,
2020
useContributionsRecord,
21+
useContributions,
22+
useComponentChangeHandlers,
2123
makeContributionsHook,
2224
} from "@/lib/hooks";

chartlets.py/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Version 0.0.22 (in development)
22

3+
* Added progress components `CircularProgress`, `CircularProgressWithLabel`,
4+
`LinearProgress`, `LinearProgressWithLabel`.
5+
36
* Replacing components is now possible by using an
47
`Output` with `property` set to an empty string. (#38)
58

chartlets.py/chartlets/component.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Component(ABC):
3131
label: str | None = None
3232
"""HTML `label` attribute. Optional."""
3333

34+
color: str | None = None
35+
"""HTML `color` attribute. Optional."""
36+
3437
children: list[Union["Component", str, None]] | None = None
3538
"""Children used by many specific components. Optional."""
3639

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from .box import Box
22
from .button import Button
33
from .checkbox import Checkbox
4+
from .progress import CircularProgress
5+
from .progress import CircularProgressWithLabel
6+
from .progress import LinearProgress
7+
from .progress import LinearProgressWithLabel
48
from .plot import Plot
59
from .select import Select
610
from .typography import Typography

0 commit comments

Comments
 (0)