2021-03-30 08:14:25 +00:00
|
|
|
import React, { useState } from "react";
|
2020-04-15 11:42:11 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
2021-03-15 12:17:56 +00:00
|
|
|
import {
|
|
|
|
|
StyledHiddenIcon,
|
|
|
|
|
StyledInputGroup,
|
|
|
|
|
StyledPropertyPaneButton,
|
|
|
|
|
StyledVisibleIcon,
|
|
|
|
|
StyledDragIcon,
|
|
|
|
|
StyledDeleteIcon,
|
|
|
|
|
} from "./StyledControls";
|
2020-04-15 11:42:11 +00:00
|
|
|
import styled from "constants/DefaultTheme";
|
|
|
|
|
import { generateReactKey } from "utils/generators";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { DroppableComponent } from "components/ads/DraggableListComponent";
|
2020-08-13 08:08:16 +00:00
|
|
|
import { getNextEntityName } from "utils/AppsmithUtils";
|
2021-03-30 08:14:25 +00:00
|
|
|
import _, { debounce } from "lodash";
|
2020-12-02 12:53:51 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { Category, Size } from "components/ads/Button";
|
2020-04-15 11:42:11 +00:00
|
|
|
|
|
|
|
|
const StyledPropertyPaneButtonWrapper = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
width: 100%;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
margin-top: 10px;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
const ItemWrapper = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-start;
|
2020-07-29 09:01:17 +00:00
|
|
|
align-items: center;
|
2020-05-07 10:51:37 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const TabsWrapper = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
const StyledOptionControlInputGroup = styled(StyledInputGroup)`
|
|
|
|
|
margin-right: 2px;
|
2021-03-15 12:17:56 +00:00
|
|
|
margin-bottom: 2px;
|
2021-02-26 06:17:21 +00:00
|
|
|
width: 100%;
|
2021-03-15 12:17:56 +00:00
|
|
|
padding-left: 30px;
|
2020-04-15 11:42:11 +00:00
|
|
|
&&& {
|
|
|
|
|
input {
|
|
|
|
|
border: none;
|
2021-03-15 12:17:56 +00:00
|
|
|
color: ${(props) => props.theme.colors.propertyPane.radioGroupText};
|
|
|
|
|
background: ${(props) => props.theme.colors.propertyPane.radioGroupBg};
|
2020-04-15 11:42:11 +00:00
|
|
|
&:focus {
|
|
|
|
|
border: none;
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
|
|
|
|
background: ${(props) => props.theme.colors.paneInputBG};
|
2020-04-15 11:42:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type RenderComponentProps = {
|
|
|
|
|
index: number;
|
|
|
|
|
item: {
|
|
|
|
|
label: string;
|
2021-02-26 06:17:21 +00:00
|
|
|
isVisible?: boolean;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
deleteOption: (index: number) => void;
|
|
|
|
|
updateOption: (index: number, value: string) => void;
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility?: (index: number) => void;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function TabControlComponent(props: RenderComponentProps) {
|
2021-02-26 06:17:21 +00:00
|
|
|
const { deleteOption, updateOption, item, index, toggleVisibility } = props;
|
2021-03-30 08:14:25 +00:00
|
|
|
const debouncedUpdate = debounce(updateOption, 1000);
|
|
|
|
|
const [visibility, setVisibility] = useState(item.isVisible);
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
2020-05-07 10:51:37 +00:00
|
|
|
<ItemWrapper>
|
2020-07-29 09:01:17 +00:00
|
|
|
<StyledDragIcon height={20} width={20} />
|
2020-04-15 11:42:11 +00:00
|
|
|
<StyledOptionControlInputGroup
|
2021-03-15 12:17:56 +00:00
|
|
|
dataType="text"
|
2020-04-15 11:42:11 +00:00
|
|
|
placeholder="Tab Title"
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
2021-03-30 08:14:25 +00:00
|
|
|
debouncedUpdate(index, value);
|
2020-04-15 11:42:11 +00:00
|
|
|
}}
|
|
|
|
|
defaultValue={item.label}
|
|
|
|
|
/>
|
|
|
|
|
<StyledDeleteIcon
|
2021-02-26 06:17:21 +00:00
|
|
|
className="t--delete-tab-btn"
|
2020-04-15 11:42:11 +00:00
|
|
|
height={20}
|
|
|
|
|
width={20}
|
2021-02-26 06:17:21 +00:00
|
|
|
marginRight={12}
|
2020-04-15 11:42:11 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
deleteOption(index);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-03-30 08:14:25 +00:00
|
|
|
{visibility || visibility === undefined ? (
|
2021-02-26 06:17:21 +00:00
|
|
|
<StyledVisibleIcon
|
|
|
|
|
className="t--show-tab-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
width={20}
|
|
|
|
|
marginRight={36}
|
|
|
|
|
onClick={() => {
|
2021-03-30 08:14:25 +00:00
|
|
|
setVisibility(!visibility);
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility && toggleVisibility(index);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<StyledHiddenIcon
|
|
|
|
|
className="t--show-tab-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
width={20}
|
|
|
|
|
marginRight={36}
|
|
|
|
|
onClick={() => {
|
2021-03-30 08:14:25 +00:00
|
|
|
setVisibility(!visibility);
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility && toggleVisibility(index);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-05-07 10:51:37 +00:00
|
|
|
</ItemWrapper>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TabControl extends BaseControl<ControlProps> {
|
2020-12-02 12:53:51 +00:00
|
|
|
componentDidMount() {
|
|
|
|
|
this.migrateTabData(this.props.propertyValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
migrateTabData(
|
|
|
|
|
tabData: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
|
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
// Added a migration script for older tab data that was strings
|
|
|
|
|
// deprecate after enough tabs have moved to the new format
|
|
|
|
|
if (_.isString(tabData)) {
|
|
|
|
|
try {
|
|
|
|
|
const parsedData: Array<{
|
|
|
|
|
sid: string;
|
|
|
|
|
label: string;
|
|
|
|
|
}> = JSON.parse(tabData);
|
|
|
|
|
this.updateProperty(this.props.propertyName, parsedData);
|
|
|
|
|
return parsedData;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
Sentry.captureException({
|
|
|
|
|
message: "Tab Migration Failed",
|
|
|
|
|
oldData: this.props.propertyValue,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return this.props.propertyValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 13:05:40 +00:00
|
|
|
updateItems = (items: Array<Record<string, unknown>>) => {
|
2020-12-02 12:53:51 +00:00
|
|
|
this.updateProperty(this.props.propertyName, items);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const tabs: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
2020-12-02 12:53:51 +00:00
|
|
|
}> = _.isString(this.props.propertyValue) ? [] : this.props.propertyValue;
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
2020-05-07 10:51:37 +00:00
|
|
|
<TabsWrapper>
|
2020-04-15 11:42:11 +00:00
|
|
|
<DroppableComponent
|
|
|
|
|
items={tabs}
|
2021-03-29 15:47:22 +00:00
|
|
|
itemHeight={45}
|
2020-04-15 11:42:11 +00:00
|
|
|
renderComponent={TabControlComponent}
|
|
|
|
|
deleteOption={this.deleteOption}
|
|
|
|
|
updateOption={this.updateOption}
|
2020-05-07 10:51:37 +00:00
|
|
|
updateItems={this.updateItems}
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility={this.toggleVisibility}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
<StyledPropertyPaneButtonWrapper>
|
|
|
|
|
<StyledPropertyPaneButton
|
2021-03-15 12:17:56 +00:00
|
|
|
icon="plus"
|
|
|
|
|
tag="button"
|
|
|
|
|
type="button"
|
2020-04-15 11:42:11 +00:00
|
|
|
text="Add a Tab"
|
|
|
|
|
onClick={this.addOption}
|
2021-03-15 12:17:56 +00:00
|
|
|
size={Size.medium}
|
|
|
|
|
category={Category.tertiary}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
</StyledPropertyPaneButtonWrapper>
|
2020-05-07 10:51:37 +00:00
|
|
|
</TabsWrapper>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility = (index: number) => {
|
|
|
|
|
const tabs: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
|
|
|
|
isVisible: boolean;
|
|
|
|
|
widgetId: string;
|
|
|
|
|
}> = this.props.propertyValue.slice();
|
|
|
|
|
const isVisible = tabs[index].isVisible === true ? false : true;
|
|
|
|
|
const updatedTabs = tabs.map((tab, tabIndex) => {
|
|
|
|
|
if (index === tabIndex) {
|
|
|
|
|
return {
|
|
|
|
|
...tab,
|
|
|
|
|
isVisible: isVisible,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return tab;
|
|
|
|
|
});
|
|
|
|
|
this.updateProperty(this.props.propertyName, updatedTabs);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
deleteOption = (index: number) => {
|
2020-12-02 12:53:51 +00:00
|
|
|
let tabs: Array<Record<string, unknown>> = this.props.propertyValue.slice();
|
2020-09-16 10:28:01 +00:00
|
|
|
if (tabs.length === 1) return;
|
|
|
|
|
delete tabs[index];
|
|
|
|
|
tabs = tabs.filter(Boolean);
|
2020-12-02 12:53:51 +00:00
|
|
|
this.updateProperty(this.props.propertyName, tabs);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateOption = (index: number, updatedLabel: string) => {
|
|
|
|
|
const tabs: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
2020-12-02 12:53:51 +00:00
|
|
|
}> = this.props.propertyValue;
|
2020-05-29 06:07:56 +00:00
|
|
|
const updatedTabs = tabs.map((tab, tabIndex) => {
|
|
|
|
|
if (index === tabIndex) {
|
2020-12-02 12:53:51 +00:00
|
|
|
return {
|
|
|
|
|
...tab,
|
|
|
|
|
label: updatedLabel,
|
|
|
|
|
};
|
2020-05-29 06:07:56 +00:00
|
|
|
}
|
|
|
|
|
return tab;
|
|
|
|
|
});
|
2020-12-02 12:53:51 +00:00
|
|
|
this.updateProperty(this.props.propertyName, updatedTabs);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addOption = () => {
|
2020-11-03 13:02:50 +00:00
|
|
|
let tabs: Array<{
|
2020-04-15 11:42:11 +00:00
|
|
|
id: string;
|
|
|
|
|
label: string;
|
2020-09-16 10:28:01 +00:00
|
|
|
widgetId: string;
|
2021-03-30 08:14:25 +00:00
|
|
|
isVisible: boolean;
|
2020-12-02 12:53:51 +00:00
|
|
|
}> = this.props.propertyValue;
|
2020-04-15 11:42:11 +00:00
|
|
|
const newTabId = generateReactKey({ prefix: "tab" });
|
2020-08-13 08:08:16 +00:00
|
|
|
const newTabLabel = getNextEntityName(
|
|
|
|
|
"Tab ",
|
2020-12-24 04:32:25 +00:00
|
|
|
tabs.map((tab) => tab.label),
|
2020-08-13 08:08:16 +00:00
|
|
|
);
|
2020-11-03 13:02:50 +00:00
|
|
|
tabs = [
|
|
|
|
|
...tabs,
|
2021-03-30 08:14:25 +00:00
|
|
|
{
|
|
|
|
|
id: newTabId,
|
|
|
|
|
label: newTabLabel,
|
|
|
|
|
widgetId: generateReactKey(),
|
|
|
|
|
isVisible: true,
|
|
|
|
|
},
|
2020-11-03 13:02:50 +00:00
|
|
|
];
|
|
|
|
|
|
2020-12-02 12:53:51 +00:00
|
|
|
this.updateProperty(this.props.propertyName, tabs);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-04-29 10:29:02 +00:00
|
|
|
static getControlType() {
|
2020-04-15 11:42:11 +00:00
|
|
|
return "TABS_INPUT";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TabControl;
|