Skip to content

Commit 5c22939

Browse files
committed
use always Label component for Switch label
1 parent 43cd92a commit 5c22939

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
3737

3838
- `<ReactFlow />`
3939
- property color for `graph` configuration was adjusted
40+
- `<Switch />`
41+
- use always `<Label/>` component for `label` value
4042

4143
## [24.0.1] - 2025-02-06
4244

src/components/Switch/Switch.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
1-
import React, { memo, SyntheticEvent } from "react";
2-
import { Switch as BlueprintSwitch, SwitchProps as BlueprintSwitchProps } from "@blueprintjs/core";
1+
import React, { memo } from "react";
2+
import {
3+
Classes as BlueprintClasses,
4+
Switch as BlueprintSwitch,
5+
SwitchProps as BlueprintSwitchProps,
6+
} from "@blueprintjs/core";
37

48
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
9+
import { Label } from "../Label/Label";
510

611
export interface SwitchProps extends Omit<BlueprintSwitchProps, "onChange"> {
712
/**
813
* Event handler for changed state.
914
*/
10-
onChange?: (value: boolean) => any;
15+
onChange?: (value: boolean) => void;
1116
/**
1217
* class names
1318
*/
1419
className?: string;
1520
}
1621

17-
export const Switch = ({ onChange, className, ...otherProps }: SwitchProps) => {
18-
const handleChange = (e: SyntheticEvent<HTMLInputElement>) => {
19-
const checked = !!(e as any).target?.checked;
20-
onChange && onChange(checked);
22+
export const Switch = ({ onChange, className, label, ...otherProps }: SwitchProps) => {
23+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
24+
if (onChange) {
25+
onChange(!!e.target?.checked);
26+
}
2127
};
2228

23-
return <BlueprintSwitch className={`${eccgui}-switch ${className}`} {...otherProps} onChange={handleChange} />;
29+
return (
30+
<BlueprintSwitch
31+
className={`${eccgui}-switch ${className ?? ""} ${
32+
label && !otherProps.labelElement ? BlueprintClasses.INLINE : ""
33+
}`}
34+
labelElement={
35+
label ? (
36+
<Label text={label} isLayoutForElement="span" disabled={otherProps.disabled} inline />
37+
) : undefined
38+
}
39+
{...otherProps}
40+
onChange={handleChange}
41+
/>
42+
);
2443
};
2544

2645
export default memo(Switch);

0 commit comments

Comments
 (0)