2021-09-21 07:55:56 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
2021-02-16 10:29:08 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import {
|
|
|
|
|
StyledInputGroup,
|
|
|
|
|
StyledDragIcon,
|
|
|
|
|
StyledEditIcon,
|
|
|
|
|
StyledDeleteIcon,
|
|
|
|
|
StyledVisibleIcon,
|
|
|
|
|
StyledHiddenIcon,
|
|
|
|
|
StyledPropertyPaneButton,
|
|
|
|
|
} from "./StyledControls";
|
|
|
|
|
import styled from "constants/DefaultTheme";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { DroppableComponent } from "components/ads/DraggableListComponent";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { ColumnProperties } from "widgets/TableWidget/component/Constants";
|
2021-02-16 10:29:08 +00:00
|
|
|
import EmptyDataState from "components/utils/EmptyDataState";
|
|
|
|
|
import { getNextEntityName } from "utils/AppsmithUtils";
|
|
|
|
|
import {
|
|
|
|
|
getDefaultColumnProperties,
|
|
|
|
|
getTableStyles,
|
2021-09-09 15:10:22 +00:00
|
|
|
} from "widgets/TableWidget/component/TableUtilities";
|
2021-02-16 10:29:08 +00:00
|
|
|
import { debounce } from "lodash";
|
2021-03-15 12:17:56 +00:00
|
|
|
import { Size, Category } from "components/ads/Button";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { reorderColumns } from "widgets/TableWidget/component/TableHelpers";
|
2021-02-16 10:29:08 +00:00
|
|
|
|
|
|
|
|
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;
|
2021-03-15 12:17:56 +00:00
|
|
|
margin-bottom: 2px;
|
2021-02-16 10:29:08 +00:00
|
|
|
width: 100%;
|
2021-10-04 15:34:37 +00:00
|
|
|
padding-left: 10px;
|
2021-05-26 12:21:09 +00:00
|
|
|
padding-right: 60px;
|
|
|
|
|
text-overflow: ellipsis;
|
2021-10-04 15:34:37 +00:00
|
|
|
background: inherit;
|
2021-02-16 10:29:08 +00:00
|
|
|
&&& {
|
|
|
|
|
input {
|
|
|
|
|
padding-left: 24px;
|
|
|
|
|
border: none;
|
|
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
|
|
|
|
&:focus {
|
|
|
|
|
border: none;
|
|
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AddColumnButton = styled(StyledPropertyPaneButton)`
|
|
|
|
|
width: 100%;
|
2021-03-15 12:17:56 +00:00
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
2021-02-16 10:29:08 +00:00
|
|
|
&&&& {
|
|
|
|
|
margin-top: 12px;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type RenderComponentProps = {
|
|
|
|
|
index: number;
|
|
|
|
|
item: {
|
|
|
|
|
label: string;
|
|
|
|
|
isDerived?: boolean;
|
|
|
|
|
isVisible?: boolean;
|
|
|
|
|
};
|
|
|
|
|
updateOption: (index: number, value: string) => void;
|
|
|
|
|
onEdit?: (index: number) => void;
|
|
|
|
|
deleteOption: (index: number) => void;
|
|
|
|
|
toggleVisibility?: (index: number) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getOriginalColumn = (
|
|
|
|
|
columns: Record<string, ColumnProperties>,
|
|
|
|
|
index: number,
|
|
|
|
|
columnOrder?: string[],
|
|
|
|
|
): ColumnProperties | undefined => {
|
|
|
|
|
const reorderedColumns = reorderColumns(columns, columnOrder || []);
|
|
|
|
|
const column: ColumnProperties | undefined = Object.values(
|
|
|
|
|
reorderedColumns,
|
|
|
|
|
).find((column: ColumnProperties) => column.index === index);
|
|
|
|
|
return column;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function ColumnControlComponent(props: RenderComponentProps) {
|
|
|
|
|
const [value, setValue] = useState(props.item.label);
|
2021-09-21 07:55:56 +00:00
|
|
|
const [isEditing, setEditing] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isEditing && props.item && props.item.label)
|
|
|
|
|
setValue(props.item.label);
|
|
|
|
|
}, [props.item?.label, isEditing]);
|
2021-03-30 08:14:25 +00:00
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
const {
|
|
|
|
|
deleteOption,
|
|
|
|
|
index,
|
2021-05-13 08:35:39 +00:00
|
|
|
item,
|
|
|
|
|
onEdit,
|
|
|
|
|
toggleVisibility,
|
|
|
|
|
updateOption,
|
2021-02-16 10:29:08 +00:00
|
|
|
} = props;
|
2021-03-30 08:14:25 +00:00
|
|
|
const [visibility, setVisibility] = useState(item.isVisible);
|
2021-02-16 10:29:08 +00:00
|
|
|
const debouncedUpdate = debounce(updateOption, 1000);
|
|
|
|
|
const onChange = useCallback(
|
|
|
|
|
(index: number, value: string) => {
|
|
|
|
|
setValue(value);
|
|
|
|
|
debouncedUpdate(index, value);
|
|
|
|
|
},
|
|
|
|
|
[updateOption],
|
|
|
|
|
);
|
2021-09-21 07:55:56 +00:00
|
|
|
|
|
|
|
|
const onFocus = () => setEditing(true);
|
|
|
|
|
const onBlur = () => setEditing(false);
|
|
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
return (
|
|
|
|
|
<ItemWrapper>
|
|
|
|
|
<StyledDragIcon height={20} width={20} />
|
|
|
|
|
<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) => {
|
|
|
|
|
onChange(index, value);
|
2021-02-16 10:29:08 +00:00
|
|
|
}}
|
2021-09-21 07:55:56 +00:00
|
|
|
onFocus={onFocus}
|
2021-04-28 10:28:39 +00:00
|
|
|
placeholder="Column Title"
|
2021-09-21 07:55:56 +00:00
|
|
|
value={value}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
<StyledEditIcon
|
|
|
|
|
className="t--edit-column-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onEdit && onEdit(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
{!!item.isDerived ? (
|
|
|
|
|
<StyledDeleteIcon
|
|
|
|
|
className="t--delete-column-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
deleteOption && deleteOption(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
2021-03-30 08:14:25 +00:00
|
|
|
) : visibility ? (
|
2021-02-16 10:29:08 +00:00
|
|
|
<StyledVisibleIcon
|
|
|
|
|
className="t--show-column-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
2021-03-30 08:14:25 +00:00
|
|
|
setVisibility(!visibility);
|
2021-02-16 10:29:08 +00:00
|
|
|
toggleVisibility && toggleVisibility(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<StyledHiddenIcon
|
|
|
|
|
className="t--show-column-btn"
|
|
|
|
|
height={20}
|
|
|
|
|
onClick={() => {
|
2021-03-30 08:14:25 +00:00
|
|
|
setVisibility(!visibility);
|
2021-02-16 10:29:08 +00:00
|
|
|
toggleVisibility && toggleVisibility(index);
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
width={20}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</ItemWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PrimaryColumnsControl extends BaseControl<ControlProps> {
|
|
|
|
|
render() {
|
|
|
|
|
// Get columns from widget properties
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || {};
|
|
|
|
|
|
|
|
|
|
// If there are no columns, show empty state
|
|
|
|
|
if (Object.keys(columns).length === 0) {
|
|
|
|
|
return <EmptyDataState />;
|
|
|
|
|
}
|
|
|
|
|
// Get an empty array of length of columns
|
|
|
|
|
let columnOrder: string[] = new Array(Object.keys(columns).length);
|
|
|
|
|
|
|
|
|
|
if (this.props.widgetProperties.columnOrder) {
|
|
|
|
|
columnOrder = this.props.widgetProperties.columnOrder;
|
|
|
|
|
} else {
|
|
|
|
|
columnOrder = Object.keys(columns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reorderedColumns = reorderColumns(columns, columnOrder);
|
|
|
|
|
|
|
|
|
|
const draggableComponentColumns = Object.values(reorderedColumns).map(
|
|
|
|
|
(column: ColumnProperties) => {
|
|
|
|
|
return {
|
|
|
|
|
label: column.label,
|
|
|
|
|
id: column.id,
|
|
|
|
|
isVisible: column.isVisible,
|
|
|
|
|
isDerived: column.isDerived,
|
|
|
|
|
index: column.index,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TabsWrapper>
|
|
|
|
|
<DroppableComponent
|
2021-04-28 10:28:39 +00:00
|
|
|
deleteOption={this.deleteOption}
|
2021-03-29 15:47:22 +00:00
|
|
|
itemHeight={45}
|
2021-04-28 10:28:39 +00:00
|
|
|
items={draggableComponentColumns}
|
|
|
|
|
onEdit={this.onEdit}
|
2021-02-16 10:29:08 +00:00
|
|
|
renderComponent={ColumnControlComponent}
|
|
|
|
|
toggleVisibility={this.toggleVisibility}
|
2021-04-28 10:28:39 +00:00
|
|
|
updateItems={this.updateItems}
|
|
|
|
|
updateOption={this.updateOption}
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
2021-03-15 12:17:56 +00:00
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
<AddColumnButton
|
2021-04-28 10:28:39 +00:00
|
|
|
category={Category.tertiary}
|
2021-02-16 10:29:08 +00:00
|
|
|
className="t--add-column-btn"
|
|
|
|
|
icon="plus"
|
|
|
|
|
onClick={this.addNewColumn}
|
2021-03-15 12:17:56 +00:00
|
|
|
size={Size.medium}
|
2021-04-28 10:28:39 +00:00
|
|
|
tag="button"
|
|
|
|
|
text="Add a new column"
|
|
|
|
|
type="button"
|
2021-02-16 10:29:08 +00:00
|
|
|
/>
|
|
|
|
|
</TabsWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addNewColumn = () => {
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || {};
|
|
|
|
|
const columnIds = Object.keys(columns);
|
|
|
|
|
const newColumnName = getNextEntityName("customColumn", columnIds);
|
|
|
|
|
const nextIndex = columnIds.length;
|
|
|
|
|
const columnProps: ColumnProperties = getDefaultColumnProperties(
|
|
|
|
|
newColumnName,
|
|
|
|
|
nextIndex,
|
|
|
|
|
this.props.widgetProperties.widgetName,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
const tableStyles = getTableStyles(this.props.widgetProperties);
|
|
|
|
|
const column = {
|
|
|
|
|
...columnProps,
|
2021-03-02 03:13:43 +00:00
|
|
|
buttonStyle: "rgb(3, 179, 101)",
|
2021-02-16 10:29:08 +00:00
|
|
|
buttonLabelColor: "#FFFFFF",
|
2021-08-17 12:54:43 +00:00
|
|
|
isDisabled: false,
|
2021-02-16 10:29:08 +00:00
|
|
|
...tableStyles,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.updateProperty(`${this.props.propertyName}.${column.id}`, column);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onEdit = (index: number) => {
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || [];
|
|
|
|
|
|
|
|
|
|
const originalColumn = getOriginalColumn(
|
|
|
|
|
columns,
|
|
|
|
|
index,
|
|
|
|
|
this.props.widgetProperties.columnOrder,
|
|
|
|
|
);
|
|
|
|
|
|
2021-03-29 15:47:22 +00:00
|
|
|
this.props.openNextPanel({
|
|
|
|
|
...originalColumn,
|
2021-04-27 07:16:54 +00:00
|
|
|
propPaneId: this.props.widgetProperties.widgetId,
|
2021-03-29 15:47:22 +00:00
|
|
|
});
|
2021-02-16 10:29:08 +00:00
|
|
|
};
|
|
|
|
|
//Used to reorder columns
|
|
|
|
|
updateItems = (items: Array<Record<string, unknown>>) => {
|
|
|
|
|
this.updateProperty(
|
|
|
|
|
"columnOrder",
|
|
|
|
|
items.map(({ id }) => id),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
toggleVisibility = (index: number) => {
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || {};
|
|
|
|
|
const originalColumn = getOriginalColumn(
|
|
|
|
|
columns,
|
|
|
|
|
index,
|
|
|
|
|
this.props.widgetProperties.columnOrder,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (originalColumn) {
|
|
|
|
|
this.updateProperty(
|
|
|
|
|
`${this.props.propertyName}.${originalColumn.id}.isVisible`,
|
|
|
|
|
!originalColumn.isVisible,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
deleteOption = (index: number) => {
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || {};
|
|
|
|
|
const derivedColumns = this.props.widgetProperties.derivedColumns || {};
|
2021-03-15 05:27:34 +00:00
|
|
|
const columnOrder = this.props.widgetProperties.columnOrder || [];
|
2021-02-16 10:29:08 +00:00
|
|
|
|
2021-03-15 05:27:34 +00:00
|
|
|
const originalColumn = getOriginalColumn(columns, index, columnOrder);
|
2021-02-16 10:29:08 +00:00
|
|
|
|
|
|
|
|
if (originalColumn) {
|
|
|
|
|
const propertiesToDelete = [
|
|
|
|
|
`${this.props.propertyName}.${originalColumn.id}`,
|
|
|
|
|
];
|
|
|
|
|
if (derivedColumns[originalColumn.id])
|
|
|
|
|
propertiesToDelete.push(`derivedColumns.${originalColumn.id}`);
|
|
|
|
|
|
2021-03-15 05:27:34 +00:00
|
|
|
const columnOrderIndex = columnOrder.findIndex(
|
2021-02-16 10:29:08 +00:00
|
|
|
(column: string) => column === originalColumn.id,
|
|
|
|
|
);
|
|
|
|
|
if (columnOrderIndex > -1)
|
|
|
|
|
propertiesToDelete.push(`columnOrder[${columnOrderIndex}]`);
|
|
|
|
|
|
|
|
|
|
this.deleteProperties(propertiesToDelete);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateOption = (index: number, updatedLabel: string) => {
|
|
|
|
|
const columns: Record<string, ColumnProperties> =
|
|
|
|
|
this.props.propertyValue || {};
|
|
|
|
|
const originalColumn = getOriginalColumn(
|
|
|
|
|
columns,
|
|
|
|
|
index,
|
|
|
|
|
this.props.widgetProperties.columnOrder,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (originalColumn) {
|
|
|
|
|
this.updateProperty(
|
|
|
|
|
`${this.props.propertyName}.${originalColumn.id}.label`,
|
|
|
|
|
updatedLabel,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "PRIMARY_COLUMNS";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PrimaryColumnsControl;
|