PromucFlow_constructor/app/client/src/components/propertyControls/SwitchControl.tsx

89 lines
2.0 KiB
TypeScript
Raw Normal View History

import React from "react";
import styled from "styled-components";
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
feat: import changes batch 2 (#15722) * Remove treedropdown from ads * Change Treedropdown imports * Remove Notification Banner, change imports * Remove Toggle from ads * Change toggle imports * explicitly declare function argument types * Remove Menu from ads * Change menu imports * Remove Spinner from ads * Change spinner imports * Remove Radio, import changes * test: updated flaky test under default meta (#15707) * updated flaky test * Updated tests * updated tests * updated the tests * updated tests * Update constants.ts * add more typecasting * Remove ListSegmentHeader, import changes * Remove TagInputComponent, import changes * Remove Switch, import changes * Remove SearchInput, change imports * Rename TagInputComponent to TagInput * Remove ProgressiveImage, import changes * import changes for SearchVariant * Remove menu divider, import changes * Remove TableDropdown, import changes * Remove Switcher * Remove StatusBar, import changes * Remove showcase carousel * Remove RectangularSwitcher, import change * Add types to TableDropdown's args * Remove MultiSwitch, import change * Remove GifPlayerComponent, import change * Remove DraggableList, import change * Remove DisplayImageUpload, import change * Remove DatePickerComponent, import change * Remove CopyToClipBoard, import change * Remove ColorSelector, import change * Remove TabItemBackgroundFill, NumberedStep, ColorPickerComponent * GifPlayerComponent -> GifPlayer * change named import * Remove FormFieldError, change imports * Update to new version of Tree Dropdown * Fix issue with ads/index.ts * Test file fix * Fix issue with merge?!?!?? * update design system to 1.0.18 * Bump ds version * bump ds version * bump ds version Co-authored-by: NandanAnantharamu <67676905+NandanAnantharamu@users.noreply.github.com> Co-authored-by: Albin <albin@appsmith.com>
2022-09-02 08:38:17 +00:00
import { Switch } from "design-system";
import {
DSEventDetail,
DSEventTypes,
DS_EVENT,
emitInteractionAnalyticsEvent,
} from "utils/AppsmithUtils";
const StyledSwitch = styled(Switch)`
&&&& {
padding: 0;
margin-bottom: 4px;
}
&&&& .bp3-control-indicator {
margin: 0;
}
`;
class SwitchControl extends BaseControl<ControlProps> {
isUpdatedViaKeyboard = false;
containerRef = React.createRef<HTMLDivElement>();
componentDidMount() {
this.containerRef.current?.addEventListener(
DS_EVENT,
this.handleAdsEvent as (arg0: Event) => void,
);
}
componentWillUnmount() {
this.containerRef.current?.removeEventListener(
DS_EVENT,
this.handleAdsEvent as (arg0: Event) => void,
);
}
handleAdsEvent = (e: CustomEvent<DSEventDetail>) => {
if (
e.detail.component === "AdsSwitch" &&
e.detail.event === DSEventTypes.KEYPRESS
) {
this.isUpdatedViaKeyboard = true;
emitInteractionAnalyticsEvent(this.containerRef.current, {
key: e.detail.meta.key,
});
e.stopPropagation();
}
};
render() {
return (
<div ref={this.containerRef}>
<StyledSwitch
checked={this.props.propertyValue}
className={this.props.propertyValue ? "checked" : "unchecked"}
defaultChecked={this.props.propertyValue}
large
onChange={this.onToggle}
/>
</div>
);
}
onToggle = () => {
this.updateProperty(
this.props.propertyName,
!this.props.propertyValue,
this.isUpdatedViaKeyboard,
);
this.isUpdatedViaKeyboard = false;
};
static getControlType() {
return "SWITCH";
}
static canDisplayValueInUI(config: ControlData, value: any): boolean {
return value === "true" || value === "false";
}
}
export type SwitchControlProps = ControlProps;
export default SwitchControl;