2020-04-28 06:52:53 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { StyledSwitch } from "./StyledControls";
|
|
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
|
|
|
|
import FormLabel from "components/editorComponents/FormLabel";
|
|
|
|
|
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
|
|
|
|
2020-11-19 03:32:58 +00:00
|
|
|
type Props = WrappedFieldProps & {
|
|
|
|
|
label: string;
|
|
|
|
|
isRequired: boolean;
|
|
|
|
|
info: string;
|
|
|
|
|
};
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2020-08-05 08:02:24 +00:00
|
|
|
const StyledFormLabel = styled(FormLabel)`
|
|
|
|
|
margin-bottom: 0px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SwitchWrapped = styled.div`
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
.bp3-control {
|
|
|
|
|
margin-bottom: 0px;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2020-08-27 15:39:16 +00:00
|
|
|
const Info = styled.div`
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
opacity: 0.7;
|
|
|
|
|
margin-top: 8px;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-11-19 03:32:58 +00:00
|
|
|
export class SwitchField extends React.Component<Props, 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
render() {
|
2020-08-27 15:39:16 +00:00
|
|
|
const { label, isRequired, input, info } = this.props;
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
return (
|
2020-08-27 15:39:16 +00:00
|
|
|
<div>
|
2020-11-19 03:32:58 +00:00
|
|
|
<SwitchWrapped data-cy={this.props.input.name}>
|
2020-08-27 15:39:16 +00:00
|
|
|
<StyledFormLabel>
|
|
|
|
|
{label} {isRequired && "*"}
|
|
|
|
|
</StyledFormLabel>
|
|
|
|
|
<StyledSwitch
|
2021-02-19 11:00:07 +00:00
|
|
|
checked={this.value}
|
2020-08-27 15:39:16 +00:00
|
|
|
large
|
2021-04-28 10:28:39 +00:00
|
|
|
onChange={(value) => input.onChange(value)}
|
2020-08-27 15:39:16 +00:00
|
|
|
/>
|
|
|
|
|
</SwitchWrapped>
|
|
|
|
|
{info && <Info>{info}</Info>}
|
|
|
|
|
</div>
|
2020-04-28 06:52:53 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SwitchControl extends BaseControl<SwitchControlProps> {
|
|
|
|
|
render() {
|
2020-11-19 03:32:58 +00:00
|
|
|
const { configProperty, label, isRequired, info } = this.props;
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
return (
|
2021-04-28 10:28:39 +00:00
|
|
|
<Field
|
|
|
|
|
component={SwitchField}
|
|
|
|
|
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;
|