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-08-04 10:30:36 +00:00
|
|
|
import { ButtonTab, ButtonTabOption } from "design-system";
|
2021-02-16 10:29:08 +00:00
|
|
|
import produce from "immer";
|
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 ButtonTabControl extends BaseControl<ButtonTabControlProps> {
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
selectButton = (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
|
|
|
const values: string[] = propertyValue
|
|
|
|
|
? propertyValue.split(",")
|
|
|
|
|
: defaultValue
|
|
|
|
|
? defaultValue.split(",")
|
|
|
|
|
: [];
|
|
|
|
|
if (values.includes(value)) {
|
|
|
|
|
values.splice(values.indexOf(value), 1);
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
this.props.propertyName,
|
|
|
|
|
values.join(","),
|
|
|
|
|
isUpdatedViaKeyboard,
|
|
|
|
|
);
|
2021-02-16 10:29:08 +00:00
|
|
|
} else {
|
|
|
|
|
const updatedValues: string[] = produce(values, (draft: string[]) => {
|
|
|
|
|
draft.push(value);
|
|
|
|
|
});
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
this.props.propertyName,
|
|
|
|
|
updatedValues.join(","),
|
|
|
|
|
isUpdatedViaKeyboard,
|
|
|
|
|
);
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
};
|
2022-07-14 05:00:30 +00:00
|
|
|
|
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-08-04 10:30:36 +00:00
|
|
|
<ButtonTab
|
2021-03-15 12:17:56 +00:00
|
|
|
options={options}
|
2022-07-14 05:00:30 +00:00
|
|
|
ref={this.componentRef}
|
2021-03-15 12:17:56 +00:00
|
|
|
selectButton={this.selectButton}
|
2022-02-10 19:00:20 +00:00
|
|
|
values={propertyValue ? propertyValue.split(",") : []}
|
2021-03-15 12:17:56 +00:00
|
|
|
/>
|
2021-02-16 10:29:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "BUTTON_TABS";
|
|
|
|
|
}
|
2022-06-03 05:07:02 +00:00
|
|
|
|
|
|
|
|
static canDisplayValueInUI(config: ControlData, value: any): boolean {
|
|
|
|
|
const allowedValues = new Set(
|
2022-07-14 05:00:30 +00:00
|
|
|
(config as ButtonTabControlProps)?.options?.map(
|
2022-06-03 05:07:02 +00:00
|
|
|
(x: { value: string }) => x.value,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const values = value.split(",");
|
|
|
|
|
|
|
|
|
|
for (const x of values) {
|
|
|
|
|
if (!allowedValues.has(x)) return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-02-16 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ButtonTabControlProps extends ControlProps {
|
|
|
|
|
options: ButtonTabOption[];
|
|
|
|
|
defaultValue: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ButtonTabControl;
|