2020-04-28 06:52:53 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
2022-10-06 11:06:29 +00:00
|
|
|
import { Switch } from "design-system";
|
2020-04-28 06:52:53 +00:00
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
|
|
|
|
import { Field, WrappedFieldProps } from "redux-form";
|
2020-08-05 08:02:24 +00:00
|
|
|
import styled from "styled-components";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
type SwitchFieldProps = WrappedFieldProps & {
|
2020-11-19 03:32:58 +00:00
|
|
|
label: string;
|
|
|
|
|
isRequired: boolean;
|
|
|
|
|
info: string;
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled: boolean;
|
2020-11-19 03:32:58 +00:00
|
|
|
};
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2020-08-05 08:02:24 +00:00
|
|
|
const SwitchWrapped = styled.div`
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
2022-10-06 11:06:29 +00:00
|
|
|
position: relative;
|
2020-08-05 08:02:24 +00:00
|
|
|
.bp3-control {
|
|
|
|
|
margin-bottom: 0px;
|
|
|
|
|
}
|
2021-10-04 06:57:17 +00:00
|
|
|
max-width: 60vw;
|
2022-10-06 11:06:29 +00:00
|
|
|
&& .bp3-control.bp3-switch .bp3-control-indicator {
|
|
|
|
|
width: 40px;
|
|
|
|
|
height: 20px;
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
margin: 0;
|
|
|
|
|
margin-left: 10px;
|
|
|
|
|
}
|
|
|
|
|
.bp3-control.bp3-switch .bp3-control-indicator::before {
|
|
|
|
|
width: 16px;
|
|
|
|
|
height: 16px;
|
|
|
|
|
}
|
|
|
|
|
.bp3-control.bp3-switch input:checked ~ .bp3-control-indicator::before {
|
|
|
|
|
left: calc(100% - 20px);
|
|
|
|
|
}
|
2020-08-05 08:02:24 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
export class SwitchField extends React.Component<SwitchFieldProps, any> {
|
2021-02-19 11:00:07 +00:00
|
|
|
get value() {
|
|
|
|
|
const { input } = this.props;
|
|
|
|
|
if (typeof input.value !== "string") return !!input.value;
|
|
|
|
|
else {
|
|
|
|
|
if (input.value.toLocaleLowerCase() === "false") return false;
|
|
|
|
|
else return !!input.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:06:29 +00:00
|
|
|
onChange: React.FormEventHandler<HTMLInputElement> = () => {
|
|
|
|
|
this.props.input.onChange(!this.value);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
render() {
|
|
|
|
|
return (
|
2022-10-06 11:06:29 +00:00
|
|
|
<SwitchWrapped data-cy={this.props.input.name}>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={this.value}
|
|
|
|
|
className="switch-control"
|
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
|
large
|
|
|
|
|
name={this.props.input.name}
|
|
|
|
|
onChange={this.onChange}
|
|
|
|
|
/>
|
|
|
|
|
</SwitchWrapped>
|
2020-04-28 06:52:53 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SwitchControl extends BaseControl<SwitchControlProps> {
|
|
|
|
|
render() {
|
2022-01-06 14:39:21 +00:00
|
|
|
const { configProperty, disabled, info, isRequired, label } = this.props;
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<Field
|
|
|
|
|
component={SwitchField}
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled={disabled}
|
2021-04-28 10:28:39 +00:00
|
|
|
info={info}
|
|
|
|
|
isRequired={isRequired}
|
|
|
|
|
label={label}
|
|
|
|
|
name={configProperty}
|
|
|
|
|
/>
|
2020-04-28 06:52:53 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
2020-09-15 16:54:15 +00:00
|
|
|
return "SWITCH";
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 15:39:16 +00:00
|
|
|
export interface SwitchControlProps extends ControlProps {
|
|
|
|
|
info?: string;
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
export default SwitchControl;
|