PromucFlow_constructor/app/client/src/components/propertyControls/TabControl.tsx
Abhinav Jha 543b7ec72d
Entity Explorer Render (#1354)
* WIP: Performance improvements in entity explorer

* WIP: Achieve feature parity for entity explorer with release

* Update unit tests

* Add sentry profiling to current page entity properties component

* Fix page add/delete not showing up on entity explorer issue. Update memoization logic for pagegroup entity

* Deal with the ban-ts-ignore eslint issues

* Update unit tests

* Fix widget entity children visibility

* Fix tests and code

* Fix tests for scenarios where the collapsed entities are unmount, as this is a part of the performance optimization

* Filter undefined children when generating structureDSL

* Remove rule from eslintrc

Consolidate createPage test command

* Update CreatePage tests to remove redundant dsl updates

* Revert CreatePage test changes, as adding more checks within this command globally causes other tests to have issues.

* re-enable eslint rule, as without it CI tests fail

* Revert to ban-ts-comment

* Fix typescript ban-ts-ignore issue by upgrading react-scripts and fixing typescript issue across the application

* Typescript errors handled

Co-authored-by: vicky-primathon.in <vicky.bansal@primathon.in>
2020-11-03 18:35:40 +05:30

188 lines
4.9 KiB
TypeScript

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";
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)`
padding: 0;
position: relative;
margin-right: 15px;
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;
`;
const ItemWrapper = styled.div`
display: flex;
justify-content: flex-start;
align-items: center;
`;
const TabsWrapper = styled.div`
width: 100%;
display: flex;
flex-direction: column;
`;
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 (
<ItemWrapper>
<StyledDragIcon height={20} width={20} />
<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);
}}
/>
</ItemWrapper>
);
}
class TabControl extends BaseControl<ControlProps> {
updateItems = (items: Array<Record<string, unknown>>) => {
this.updateProperty(this.props.propertyName, JSON.stringify(items));
};
render() {
const tabs: Array<{
id: string;
label: string;
}> = _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
return (
<TabsWrapper>
<DroppableComponent
items={tabs}
renderComponent={TabControlComponent}
deleteOption={this.deleteOption}
updateOption={this.updateOption}
updateItems={this.updateItems}
/>
<StyledPropertyPaneButtonWrapper>
<StyledPropertyPaneButton
text="Add a Tab"
color="#FFFFFF"
minimal
onClick={this.addOption}
/>
</StyledPropertyPaneButtonWrapper>
</TabsWrapper>
);
}
deleteOption = (index: number) => {
let tabs: Array<Record<string, unknown>> = _.isString(
this.props.propertyValue,
)
? JSON.parse(this.props.propertyValue).slice()
: this.props.propertyValue.slice();
if (tabs.length === 1) return;
delete tabs[index];
tabs = tabs.filter(Boolean);
this.updateProperty(this.props.propertyName, JSON.stringify(tabs));
};
updateOption = (index: number, updatedLabel: string) => {
const tabs: Array<{
id: string;
label: string;
}> = _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
const updatedTabs = tabs.map((tab, tabIndex) => {
if (index === tabIndex) {
tab.label = updatedLabel;
}
return tab;
});
this.updateProperty(this.props.propertyName, JSON.stringify(updatedTabs));
};
addOption = () => {
let tabs: Array<{
id: string;
label: string;
widgetId: string;
}> = _.isString(this.props.propertyValue)
? JSON.parse(this.props.propertyValue)
: this.props.propertyValue;
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, JSON.stringify(tabs));
};
static getControlType() {
return "TABS_INPUT";
}
}
export default TabControl;