PromucFlow_constructor/app/client/src/components/propertyControls/IconTabControl.tsx
Aswath K dd54ddae87
fix: Issue with the Segmented control in property pane (#18533)
In the case of IconButtons, clicking an already selected item causes the
value to toggle with the last selected value.

- This PR make sure that clicking an already selected button doesn't
trigger any events
- This PR adds a default selected value so that at least one value will
be selected rather than clearing the entire option.

Fixes #18343
Fixes #19658
2023-06-07 16:33:22 +05:30

89 lines
2.3 KiB
TypeScript

import React from "react";
import styled from "styled-components";
import type { ControlData, ControlProps } from "./BaseControl";
import BaseControl from "./BaseControl";
import type { SegmentedControlOption } from "design-system";
import { SegmentedControl } from "design-system";
import type { DSEventDetail } from "utils/AppsmithUtils";
import {
DSEventTypes,
DS_EVENT,
emitInteractionAnalyticsEvent,
} from "utils/AppsmithUtils";
const StyledSegmentedControl = styled(SegmentedControl)`
> .ads-v2-segmented-control__segments-container {
flex: 1 1 0%;
}
`;
export interface IconTabControlProps extends ControlProps {
options: SegmentedControlOption[];
defaultValue: string;
fullWidth: boolean;
}
class IconTabControl extends BaseControl<IconTabControlProps> {
componentRef = React.createRef<HTMLDivElement>();
componentDidMount() {
this.componentRef.current?.addEventListener(
DS_EVENT,
this.handleAdsEvent as (arg0: Event) => void,
);
}
componentWillUnmount() {
this.componentRef.current?.removeEventListener(
DS_EVENT,
this.handleAdsEvent as (arg0: Event) => void,
);
}
handleAdsEvent = (e: CustomEvent<DSEventDetail>) => {
if (
e.detail.component === "ButtonGroup" &&
e.detail.event === DSEventTypes.KEYPRESS
) {
emitInteractionAnalyticsEvent(this.componentRef.current, {
key: e.detail.meta.key,
});
e.stopPropagation();
}
};
selectOption = (value: string, isUpdatedViaKeyboard = false) => {
if (this.props.propertyValue !== value) {
this.updateProperty(this.props.propertyName, value, isUpdatedViaKeyboard);
}
};
render() {
return (
<StyledSegmentedControl
isFullWidth={this.props.fullWidth}
onChange={this.selectOption}
options={this.props.options}
ref={this.componentRef}
value={this.props.propertyValue || this.props.defaultValue}
/>
);
}
static getControlType() {
return "ICON_TABS";
}
static canDisplayValueInUI(config: ControlData, value: any): boolean {
if (
(config as IconTabControlProps)?.options
?.map((x: { value: string }) => x.value)
.includes(value)
)
return true;
return false;
}
}
export default IconTabControl;