2021-09-21 07:55:56 +00:00
|
|
|
import React, { useCallback, useEffect, 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 {
|
|
|
|
|
StyledInputGroup,
|
|
|
|
|
StyledPropertyPaneButton,
|
|
|
|
|
StyledDragIcon,
|
|
|
|
|
StyledDeleteIcon,
|
2021-04-27 07:16:54 +00:00
|
|
|
StyledEditIcon,
|
2021-03-15 12:17:56 +00:00
|
|
|
} 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";
|
|
|
|
|
import _, { debounce, 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";
|
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;
|
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) {
|
2021-09-21 07:55:56 +00:00
|
|
|
const { index, item, updateOption } = props;
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const deleteOption = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.WIDGET_DELETE_TAB_CHILD,
|
|
|
|
|
payload: { ...item, index },
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [value, setValue] = useState(item.label);
|
|
|
|
|
const [isEditing, setEditing] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isEditing && item && item.label) setValue(item.label);
|
|
|
|
|
}, [item?.label, isEditing]);
|
|
|
|
|
|
2021-03-30 08:14:25 +00:00
|
|
|
const debouncedUpdate = debounce(updateOption, 1000);
|
2021-04-27 07:16:54 +00:00
|
|
|
const handleChange = useCallback(() => props.onEdit && props.onEdit(index), [
|
|
|
|
|
index,
|
|
|
|
|
]);
|
2021-09-21 07:55:56 +00:00
|
|
|
|
|
|
|
|
const onChange = useCallback(
|
|
|
|
|
(index: number, value: string) => {
|
|
|
|
|
setValue(value);
|
|
|
|
|
debouncedUpdate(index, value);
|
|
|
|
|
},
|
|
|
|
|
[updateOption],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onFocus = () => setEditing(true);
|
|
|
|
|
const onBlur = () => setEditing(false);
|
|
|
|
|
|
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"
|
2021-09-21 07:55:56 +00:00
|
|
|
onBlur={onBlur}
|
2021-03-15 12:17:56 +00:00
|
|
|
onChange={(value: string) => {
|
2021-09-21 07:55:56 +00:00
|
|
|
onChange(index, value);
|
2020-04-15 11:42:11 +00:00
|
|
|
}}
|
2021-09-21 07:55:56 +00:00
|
|
|
onFocus={onFocus}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder="Tab Title"
|
2021-09-21 07:55:56 +00:00
|
|
|
value={value}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
<StyledDeleteIcon
|
2021-02-26 06:17:21 +00:00
|
|
|
className="t--delete-tab-btn"
|
2020-04-15 11:42:11 +00:00
|
|
|
height={20}
|
2021-02-26 06:17:21 +00:00
|
|
|
marginRight={12}
|
2021-09-21 07:55:56 +00:00
|
|
|
onClick={deleteOption}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
2021-04-27 07:16:54 +00:00
|
|
|
<StyledEditIcon
|
|
|
|
|
className="t--edit-column-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={handleChange}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-04-27 07:16:54 +00:00
|
|
|
/>
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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}
|
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}
|
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
|
|
|
};
|
|
|
|
|
|
2020-04-29 10:29:02 +00:00
|
|
|
static getControlType() {
|
2020-04-15 11:42:11 +00:00
|
|
|
return "TABS_INPUT";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TabControl;
|