2021-02-16 10:29:08 +00:00
|
|
|
import React from "react";
|
2022-06-03 05:07:02 +00:00
|
|
|
import BaseControl, { ControlData, ControlProps } from "./BaseControl";
|
2022-02-10 19:00:20 +00:00
|
|
|
import ButtonTabComponent, {
|
|
|
|
|
ButtonTabOption,
|
|
|
|
|
} from "components/ads/ButtonTabComponent";
|
2022-07-14 05:00:30 +00:00
|
|
|
import {
|
|
|
|
|
DSEventDetail,
|
|
|
|
|
DSEventTypes,
|
|
|
|
|
DS_EVENT,
|
|
|
|
|
emitInteractionAnalyticsEvent,
|
|
|
|
|
} from "utils/AppsmithUtils";
|
2021-02-16 10:29:08 +00:00
|
|
|
|
|
|
|
|
class IconTabControl extends BaseControl<IconTabControlProps> {
|
2022-07-14 05:00:30 +00:00
|
|
|
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 === "ButtonTab" &&
|
|
|
|
|
e.detail.event === DSEventTypes.KEYPRESS
|
|
|
|
|
) {
|
|
|
|
|
emitInteractionAnalyticsEvent(this.componentRef.current, {
|
|
|
|
|
key: e.detail.meta.key,
|
|
|
|
|
});
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
selectOption = (value: string, isUpdatedViaKeyboard = false) => {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { defaultValue, propertyValue } = this.props;
|
2021-02-16 10:29:08 +00:00
|
|
|
if (propertyValue === value) {
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
this.props.propertyName,
|
|
|
|
|
defaultValue,
|
|
|
|
|
isUpdatedViaKeyboard,
|
|
|
|
|
);
|
2021-02-16 10:29:08 +00:00
|
|
|
} else {
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(this.props.propertyName, value, isUpdatedViaKeyboard);
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
render() {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { options, propertyValue } = this.props;
|
2021-02-16 10:29:08 +00:00
|
|
|
return (
|
2022-02-10 19:00:20 +00:00
|
|
|
<ButtonTabComponent
|
2021-03-15 12:17:56 +00:00
|
|
|
options={options}
|
2022-07-14 05:00:30 +00:00
|
|
|
ref={this.componentRef}
|
2022-02-10 19:00:20 +00:00
|
|
|
selectButton={this.selectOption}
|
|
|
|
|
values={[propertyValue]}
|
2021-03-15 12:17:56 +00:00
|
|
|
/>
|
2021-02-16 10:29:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "ICON_TABS";
|
|
|
|
|
}
|
2022-06-03 05:07:02 +00:00
|
|
|
|
|
|
|
|
static canDisplayValueInUI(config: ControlData, value: any): boolean {
|
|
|
|
|
if (
|
|
|
|
|
(config as IconTabControlProps)?.options
|
|
|
|
|
?.map((x: { value: string }) => x.value)
|
|
|
|
|
.includes(value)
|
|
|
|
|
)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IconTabControlProps extends ControlProps {
|
2022-02-10 19:00:20 +00:00
|
|
|
options: ButtonTabOption[];
|
2021-02-16 10:29:08 +00:00
|
|
|
defaultValue: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default IconTabControl;
|