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

Added Switch component #72

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion chartlets.js/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
- using `schema` instead of `type` property for callback arguments
- using `return` object with `schema` property for callback return values

* New components
* New (MUI) components
- `LinearProgress`
- `Switch`

## Version 0.0.29 (from 2024/11/26)

Expand Down
37 changes: 37 additions & 0 deletions chartlets.js/packages/lib/src/plugins/mui/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { createChangeHandler } from "./common.test";
import { Switch } from "./Switch";

describe("Switch", () => {
it("should render the Switch component", () => {
render(
<Switch id="cb" type={"Switch"} label={"Click!"} onChange={() => {}} />,
);
// to inspect rendered component, do:
// expect(document.querySelector("#cb")).toEqual({});
expect(screen.getByText("Click!")).not.toBeUndefined();
});

it("should fire 'value' property", () => {
const { recordedEvents, onChange } = createChangeHandler();
render(
<Switch
data-testid="bt"
id="cb"
type={"Switch"}
label={"Click!"}
value={false}
onChange={onChange}
/>,
);
fireEvent.click(screen.getByText("Click!"));
expect(recordedEvents.length).toEqual(1);
expect(recordedEvents[0]).toEqual({
componentType: "Switch",
id: "cb",
property: "value",
value: true,
});
});
});
51 changes: 51 additions & 0 deletions chartlets.js/packages/lib/src/plugins/mui/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { type ChangeEvent } from "react";
import MuiSwitch from "@mui/material/Switch";
import MuiFormControl from "@mui/material/FormControl";
import MuiFormControlLabel from "@mui/material/FormControlLabel";

import type { ComponentState, ComponentProps } from "@/index";

interface SwitchState extends ComponentState {
label?: string;
value?: boolean | undefined;
}

interface SwitchProps extends ComponentProps, SwitchState {}

export function Switch({
type,
id,
name,
value,
disabled,
style,
label,
onChange,
}: SwitchProps) {
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
if (id) {
return onChange({
componentType: type,
id: id,
property: "value",
value: event.currentTarget.checked,
});
}
};
return (
<MuiFormControl variant="filled" size="small" style={style}>
<MuiFormControlLabel
label={label}
control={
<MuiSwitch
id={id}
name={name}
checked={Boolean(value)}
disabled={disabled}
onChange={handleChange}
/>
}
/>
</MuiFormControl>
);
}
2 changes: 2 additions & 0 deletions chartlets.js/packages/lib/src/plugins/mui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Checkbox } from "./Checkbox";
import { CircularProgress } from "./CircularProgress";
import { IconButton } from "./IconButton";
import { Select } from "./Select";
import { Switch } from "./Switch";
import { Typography } from "./Typography";

export default function mui(): Plugin {
Expand All @@ -16,6 +17,7 @@ export default function mui(): Plugin {
["CircularProgress", CircularProgress],
["IconButton", IconButton],
["Select", Select],
["Switch", Switch],
["Typography", Typography],
],
};
Expand Down
2 changes: 2 additions & 0 deletions chartlets.py/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- using `schema` instead of `type` property for callback arguments
- using `return` object with `schema` property for callback return values

* New components
- `Switch`

## Version 0.0.29 (from 2024/11/26)

Expand Down
1 change: 1 addition & 0 deletions chartlets.py/chartlets/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .progress import LinearProgressWithLabel
from .charts.vega import VegaChart
from .select import Select
from .switch import Switch
from .typography import Typography
14 changes: 14 additions & 0 deletions chartlets.py/chartlets/components/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass

from chartlets import Component


@dataclass(frozen=True)
class Switch(Component):
"""Switches toggle the state of a single setting on or off."""

value: bool | None = None
"""The switch value."""

label: str = ""
"""The switch label."""
11 changes: 11 additions & 0 deletions chartlets.py/tests/components/switch_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from chartlets.components import Switch
from tests.component_test import make_base


class SwitchTest(make_base(Switch)):

def test_is_json_serializable(self):
self.assert_is_json_serializable(
self.cls(value=True, label="Auto-safe"),
{"type": "Switch", "value": True, "label": "Auto-safe"},
)
Loading