2022-02-04 10:59:54 +00:00
|
|
|
import React from "react";
|
2020-04-15 11:42:11 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
2022-02-04 10:59:54 +00:00
|
|
|
import { StyledPropertyPaneButton } 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";
|
2021-09-21 07:55:56 +00:00
|
|
|
import { getNextEntityName, noop } from "utils/AppsmithUtils";
|
2022-02-04 10:59:54 +00:00
|
|
|
import _, { orderBy } 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";
|
2021-09-21 07:55:56 +00:00
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
import { ReduxActionTypes } from "constants/ReduxActionConstants";
|
2022-02-04 10:59:54 +00:00
|
|
|
import { DraggableListCard } from "components/ads/DraggableListCard";
|
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 TabsWrapper = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
type RenderComponentProps = {
|
2022-02-04 10:59:54 +00:00
|
|
|
focusedIndex: number | null | undefined;
|
2020-04-15 11:42:11 +00:00
|
|
|
index: number;
|
2022-02-04 10:59:54 +00:00
|
|
|
isDragging: boolean;
|
2020-04-15 11:42:11 +00:00
|
|
|
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;
|
2022-02-04 10:59:54 +00:00
|
|
|
updateFocus?: (index: number, isFocused: boolean) => void;
|
2020-04-15 11:42:11 +00:00
|
|
|
updateOption: (index: number, value: string) => void;
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility?: (index: number) => void;
|
2021-04-27 07:16:54 +00:00
|
|
|
onEdit?: (props: any) => void;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
function AddTabButtonComponent({ widgetId }: any) {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const addOption = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.WIDGET_ADD_NEW_TAB_CHILD,
|
|
|
|
|
payload: {
|
|
|
|
|
widgetId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<StyledPropertyPaneButtonWrapper>
|
|
|
|
|
<StyledPropertyPaneButton
|
|
|
|
|
category={Category.tertiary}
|
|
|
|
|
icon="plus"
|
|
|
|
|
onClick={addOption}
|
|
|
|
|
size={Size.medium}
|
|
|
|
|
tag="button"
|
|
|
|
|
text="Add a Tab"
|
|
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
</StyledPropertyPaneButtonWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
function TabControlComponent(props: RenderComponentProps) {
|
2022-02-04 10:59:54 +00:00
|
|
|
const { index, item } = props;
|
2021-09-21 07:55:56 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const deleteOption = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.WIDGET_DELETE_TAB_CHILD,
|
|
|
|
|
payload: { ...item, index },
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
2022-02-04 10:59:54 +00:00
|
|
|
<DraggableListCard
|
|
|
|
|
{...props}
|
|
|
|
|
deleteOption={deleteOption}
|
|
|
|
|
isDelete
|
|
|
|
|
placeholder="Tab Title"
|
|
|
|
|
/>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 10:59:54 +00:00
|
|
|
type State = {
|
|
|
|
|
focusedIndex: number | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TabControl extends BaseControl<ControlProps, State> {
|
|
|
|
|
constructor(props: ControlProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
focusedIndex: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-12-02 12:53:51 +00:00
|
|
|
componentDidMount() {
|
|
|
|
|
this.migrateTabData(this.props.propertyValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 10:59:54 +00:00
|
|
|
componentDidUpdate(prevProps: ControlProps): void {
|
|
|
|
|
//on adding a new column last column should get focused
|
|
|
|
|
if (
|
|
|
|
|
Object.keys(prevProps.propertyValue).length + 1 ===
|
|
|
|
|
Object.keys(this.props.propertyValue).length
|
|
|
|
|
) {
|
|
|
|
|
this.updateFocus(Object.keys(this.props.propertyValue).length - 1, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 12:53:51 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 07:16:54 +00:00
|
|
|
updateItems = (items: Array<Record<string, any>>) => {
|
|
|
|
|
const tabsObj = items.reduce((obj: any, each: any, index: number) => {
|
|
|
|
|
obj[each.id] = {
|
|
|
|
|
...each,
|
|
|
|
|
index,
|
|
|
|
|
};
|
|
|
|
|
return obj;
|
|
|
|
|
}, {});
|
|
|
|
|
this.updateProperty(this.props.propertyName, tabsObj);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onEdit = (index: number) => {
|
|
|
|
|
const tabs: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
|
|
|
|
}> = Object.values(this.props.propertyValue);
|
|
|
|
|
const tabToChange = tabs[index];
|
|
|
|
|
this.props.openNextPanel({
|
|
|
|
|
index,
|
|
|
|
|
...tabToChange,
|
|
|
|
|
propPaneId: this.props.widgetProperties.widgetId,
|
|
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
render() {
|
|
|
|
|
const tabs: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
2021-04-27 07:16:54 +00:00
|
|
|
}> = _.isString(this.props.propertyValue)
|
|
|
|
|
? []
|
|
|
|
|
: Object.values(this.props.propertyValue);
|
2021-09-21 07:55:56 +00:00
|
|
|
|
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
|
2021-09-21 07:55:56 +00:00
|
|
|
deleteOption={noop}
|
2022-02-04 10:59:54 +00:00
|
|
|
fixedHeight={370}
|
|
|
|
|
focusedIndex={this.state.focusedIndex}
|
2021-03-29 15:47:22 +00:00
|
|
|
itemHeight={45}
|
2021-09-21 07:55:56 +00:00
|
|
|
items={orderBy(tabs, ["index"], ["asc"])}
|
2021-04-28 10:28:39 +00:00
|
|
|
onEdit={this.onEdit}
|
2020-04-15 11:42:11 +00:00
|
|
|
renderComponent={TabControlComponent}
|
2021-02-26 06:17:21 +00:00
|
|
|
toggleVisibility={this.toggleVisibility}
|
2022-02-04 10:59:54 +00:00
|
|
|
updateFocus={this.updateFocus}
|
2021-04-28 10:28:39 +00:00
|
|
|
updateItems={this.updateItems}
|
|
|
|
|
updateOption={this.updateOption}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
2021-09-21 07:55:56 +00:00
|
|
|
<AddTabButtonComponent
|
|
|
|
|
widgetId={this.props.widgetProperties.widgetId}
|
|
|
|
|
/>
|
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
|
|
|
updateOption = (index: number, updatedLabel: string) => {
|
2021-04-27 07:16:54 +00:00
|
|
|
const tabsArray: any = Object.values(this.props.propertyValue);
|
2021-09-21 07:55:56 +00:00
|
|
|
const { id: itemId } = tabsArray[index];
|
2021-04-27 07:16:54 +00:00
|
|
|
this.updateProperty(
|
|
|
|
|
`${this.props.propertyName}.${itemId}.label`,
|
|
|
|
|
updatedLabel,
|
|
|
|
|
);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addOption = () => {
|
2021-04-27 07:16:54 +00:00
|
|
|
let tabs = this.props.propertyValue;
|
|
|
|
|
const tabsArray = Object.values(tabs);
|
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 ",
|
2021-04-27 07:16:54 +00:00
|
|
|
tabsArray.map((tab: any) => tab.label),
|
2020-08-13 08:08:16 +00:00
|
|
|
);
|
2021-04-27 07:16:54 +00:00
|
|
|
tabs = {
|
2020-11-03 13:02:50 +00:00
|
|
|
...tabs,
|
2021-04-27 07:16:54 +00:00
|
|
|
[newTabId]: {
|
2021-03-30 08:14:25 +00:00
|
|
|
id: newTabId,
|
|
|
|
|
label: newTabLabel,
|
|
|
|
|
widgetId: generateReactKey(),
|
|
|
|
|
isVisible: true,
|
|
|
|
|
},
|
2021-04-27 07:16:54 +00:00
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
|
2022-02-04 10:59:54 +00:00
|
|
|
updateFocus = (index: number, isFocused: boolean) => {
|
|
|
|
|
this.setState({ focusedIndex: isFocused ? index : null });
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-29 10:29:02 +00:00
|
|
|
static getControlType() {
|
2020-04-15 11:42:11 +00:00
|
|
|
return "TABS_INPUT";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TabControl;
|