PromucFlow_constructor/app/client/src/components/formControls/SwitchControl.tsx
Ayush Pahwa 3ab11fa942
feat: Add enable/disable functionality to form components (#10215)
* Added new condition type

* Added new variables to cater to enable/disable conditionals

* Adding functionality to disable the drop down control

* Added ability to enable/disable to dynamic input text fields

* Updated input text control to have enabled/disabled feature

* Added enable/disable functionality to FixedKeyInputControl

* Added enable/disable func to switch control and stanrdasied var name

* Added disable functionality for file picker

* Added enable/disable functionality to QUER_DYNAMIC_TEXT
2022-01-06 20:09:21 +05:30

95 lines
2.1 KiB
TypeScript

import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
import Toggle from "components/ads/Toggle";
import { ControlType } from "constants/PropertyControlConstants";
import { Field, WrappedFieldProps } from "redux-form";
import styled from "styled-components";
type SwitchFieldProps = WrappedFieldProps & {
label: string;
isRequired: boolean;
info: string;
disabled: boolean;
};
const StyledToggle = styled(Toggle)`
.slider {
margin-left: 10px;
width: 40px;
height: 20px;
}
.slider::before {
height: 16px;
width: 16px;
}
input:checked + .slider::before {
transform: translateX(19px);
}
`;
const SwitchWrapped = styled.div`
flex-direction: row;
display: flex;
align-items: center;
.bp3-control {
margin-bottom: 0px;
}
max-width: 60vw;
`;
export class SwitchField extends React.Component<SwitchFieldProps, any> {
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;
}
}
render() {
return (
<div>
<SwitchWrapped data-cy={this.props.input.name}>
<StyledToggle
className="switch-control"
disabled={this.props.disabled}
name={this.props.input.name}
onToggle={(value: any) => {
this.props.input.onChange(value);
}}
value={this.value}
/>
</SwitchWrapped>
</div>
);
}
}
class SwitchControl extends BaseControl<SwitchControlProps> {
render() {
const { configProperty, disabled, info, isRequired, label } = this.props;
return (
<Field
component={SwitchField}
disabled={disabled}
info={info}
isRequired={isRequired}
label={label}
name={configProperty}
/>
);
}
getControlType(): ControlType {
return "SWITCH";
}
}
export interface SwitchControlProps extends ControlProps {
info?: string;
}
export default SwitchControl;