|
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"; |
3 | 7 |
|
4 | 8 | import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 9 | +import { Label } from "../Label/Label"; |
5 | 10 |
|
6 | 11 | export interface SwitchProps extends Omit<BlueprintSwitchProps, "onChange"> { |
7 | 12 | /** |
8 | 13 | * Event handler for changed state. |
9 | 14 | */ |
10 | | - onChange?: (value: boolean) => any; |
| 15 | + onChange?: (value: boolean) => void; |
11 | 16 | /** |
12 | 17 | * class names |
13 | 18 | */ |
14 | 19 | className?: string; |
15 | 20 | } |
16 | 21 |
|
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 | + } |
21 | 27 | }; |
22 | 28 |
|
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 | + ); |
24 | 43 | }; |
25 | 44 |
|
26 | 45 | export default memo(Switch); |
0 commit comments