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

213 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-04-15 11:42:11 +00:00
import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
import { StyledInputGroup, StyledPropertyPaneButton } from "./StyledControls";
import styled from "constants/DefaultTheme";
import { FormIcons } from "icons/FormIcons";
import { ControlIcons } from "icons/ControlIcons";
import { AnyStyledComponent } from "styled-components";
import { generateReactKey } from "utils/generators";
import { DroppableComponent } from "../designSystems/appsmith/DraggableListComponent";
import { getNextEntityName } from "utils/AppsmithUtils";
import _ from "lodash";
import * as Sentry from "@sentry/react";
2020-04-15 11:42:11 +00:00
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
padding: 0;
position: relative;
margin-left: 15px;
cursor: pointer;
`;
const StyledDragIcon = styled(ControlIcons.DRAG_CONTROL as AnyStyledComponent)`
2020-04-15 11:42:11 +00:00
padding: 0;
position: relative;
margin-right: 15px;
2020-04-15 11:42:11 +00:00
cursor: move;
svg {
path {
fill: ${props => props.theme.colors.paneSectionLabel};
}
}
`;
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;
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;
&&& {
input {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
&:focus {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
background: ${props => props.theme.colors.paneInputBG};
}
}
}
`;
type RenderComponentProps = {
index: number;
item: {
label: string;
};
deleteOption: (index: number) => void;
updateOption: (index: number, value: string) => void;
};
function TabControlComponent(props: RenderComponentProps) {
const { deleteOption, updateOption, item, index } = props;
return (
2020-05-07 10:51:37 +00:00
<ItemWrapper>
<StyledDragIcon height={20} width={20} />
2020-04-15 11:42:11 +00:00
<StyledOptionControlInputGroup
type="text"
placeholder="Tab Title"
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
updateOption(index, event.target.value);
}}
defaultValue={item.label}
/>
<StyledDeleteIcon
height={20}
width={20}
onClick={() => {
deleteOption(index);
}}
/>
2020-05-07 10:51:37 +00:00
</ItemWrapper>
2020-04-15 11:42:11 +00:00
);
}
class TabControl extends BaseControl<ControlProps> {
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;
}
}
updateItems = (items: Array<Record<string, unknown>>) => {
this.updateProperty(this.props.propertyName, items);
2020-04-15 11:42:11 +00:00
};
render() {
const tabs: Array<{
id: string;
label: string;
}> = _.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}
renderComponent={TabControlComponent}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
2020-05-07 10:51:37 +00:00
updateItems={this.updateItems}
2020-04-15 11:42:11 +00:00
/>
<StyledPropertyPaneButtonWrapper>
<StyledPropertyPaneButton
text="Add a Tab"
color="#FFFFFF"
minimal
onClick={this.addOption}
/>
</StyledPropertyPaneButtonWrapper>
2020-05-07 10:51:37 +00:00
</TabsWrapper>
2020-04-15 11:42:11 +00:00
);
}
deleteOption = (index: number) => {
let tabs: Array<Record<string, unknown>> = this.props.propertyValue.slice();
if (tabs.length === 1) return;
delete tabs[index];
tabs = tabs.filter(Boolean);
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;
}> = this.props.propertyValue;
const updatedTabs = tabs.map((tab, tabIndex) => {
if (index === tabIndex) {
return {
...tab,
label: updatedLabel,
};
}
return tab;
});
this.updateProperty(this.props.propertyName, updatedTabs);
2020-04-15 11:42:11 +00:00
};
addOption = () => {
let tabs: Array<{
2020-04-15 11:42:11 +00:00
id: string;
label: string;
widgetId: string;
}> = this.props.propertyValue;
2020-04-15 11:42:11 +00:00
const newTabId = generateReactKey({ prefix: "tab" });
const newTabLabel = getNextEntityName(
"Tab ",
tabs.map(tab => tab.label),
);
tabs = [
...tabs,
{ id: newTabId, label: newTabLabel, widgetId: generateReactKey() },
];
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;