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

149 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-01-23 07:53:36 +00:00
import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
import { ControlWrapper, StyledPropertyPaneButton } from "./StyledControls";
import { ControlType } from "constants/PropertyControlConstants";
import { AppState } from "reducers";
import { connect } from "react-redux";
import { ActionPayload } from "constants/ActionConstants";
import { FinalActionSelector } from "./ActionSelectorControl";
import { generateReactKey } from "utils/generators";
import styled from "constants/DefaultTheme";
2020-01-23 07:53:36 +00:00
import { AnyStyledComponent } from "styled-components";
import { FormIcons } from "icons/FormIcons";
2020-01-30 13:23:04 +00:00
import { RestAction } from "api/ActionAPI";
2020-01-23 07:53:36 +00:00
export interface ColumnAction {
label: string;
id: string;
actionPayloads: ActionPayload[];
}
const StyledDeleteIcon = styled(FormIcons.DELETE_ICON as AnyStyledComponent)`
padding: 5px 5px;
position: absolute;
right: 0px;
cursor: pointer;
top: 23px;
`;
class ColumnActionSelectorControl extends BaseControl<
2020-01-30 13:23:04 +00:00
ColumnActionSelectorControlProps & { data: RestAction[] }
2020-01-23 07:53:36 +00:00
> {
render() {
return (
<ControlWrapper orientation={"VERTICAL"}>
{this.props.propertyValue &&
this.props.propertyValue.map(
(columnAction: ColumnAction, index: number) => {
return (
<div
key={columnAction.id}
style={{
position: "relative",
// position: "absolute",
}}
>
<FinalActionSelector
identifier={columnAction.id}
actionsData={this.props.data}
actions={columnAction.actionPayloads}
updateActions={this.updateActions}
label={columnAction.label}
labelEditable={true}
updateLabel={this.updateLabel}
2020-01-30 13:23:04 +00:00
/>
2020-01-23 07:53:36 +00:00
<StyledDeleteIcon
height={20}
width={20}
onClick={this.removeColumnAction.bind(this, index)}
2020-01-30 13:23:04 +00:00
/>
2020-01-23 07:53:36 +00:00
</div>
);
},
)}
<StyledPropertyPaneButton
text={"Column Action"}
icon={"plus"}
color={"#FFFFFF"}
minimal={true}
onClick={this.addColumnAction}
2020-01-30 13:23:04 +00:00
/>
2020-01-23 07:53:36 +00:00
</ControlWrapper>
);
}
onTextChange = (event: React.ChangeEvent<HTMLTextAreaElement> | string) => {
let value = event;
if (typeof event !== "string") {
value = event.target.value;
}
this.updateProperty(this.props.propertyName, value);
};
updateActions = (actions: ActionPayload[], key?: string) => {
const columnActions = this.props.propertyValue || [];
const columnActionsClone = columnActions.slice();
const foundColumnActionIndex = columnActionsClone.findIndex(
(columnAction: ColumnAction) => columnAction.id === key,
);
if (foundColumnActionIndex !== -1) {
let foundColumnAction = columnActionsClone[foundColumnActionIndex];
foundColumnAction = Object.assign({}, foundColumnAction);
foundColumnAction.actionPayloads = actions;
columnActionsClone.splice(foundColumnActionIndex, 1, foundColumnAction);
}
this.updateProperty(this.props.propertyName, columnActionsClone);
};
updateLabel = (label: string, key: string) => {
const columnActions = this.props.propertyValue || [];
const columnActionsClone = columnActions.slice();
const foundColumnActionIndex = columnActionsClone.findIndex(
(columnAction: ColumnAction) => columnAction.id === key,
);
if (foundColumnActionIndex !== -1) {
let foundColumnAction = columnActionsClone[foundColumnActionIndex];
foundColumnAction = Object.assign({}, foundColumnAction);
foundColumnAction.label = label;
columnActionsClone.splice(foundColumnActionIndex, 1, foundColumnAction);
}
this.updateProperty(this.props.propertyName, columnActionsClone);
};
removeColumnAction = (index: number) => {
const columnActions = this.props.propertyValue || [];
const columnActionsClone = columnActions.slice();
columnActionsClone.splice(index, 1);
this.updateProperty(this.props.propertyName, columnActionsClone);
};
addColumnAction = () => {
const columnActions = this.props.propertyValue || [];
const columnActionsClone = columnActions.slice();
columnActionsClone.push({
2020-01-23 13:32:05 +00:00
label: "Action",
2020-01-23 07:53:36 +00:00
id: generateReactKey(),
actionPayloads: [],
});
this.updateProperty(this.props.propertyName, columnActionsClone);
};
onToggle = () => {
this.updateProperty(this.props.propertyName, !this.props.propertyValue);
};
getControlType(): ControlType {
return "COLUMN_ACTION_SELECTOR";
}
}
export type ColumnActionSelectorControlProps = ControlProps;
2020-01-30 13:23:04 +00:00
const mapStateToProps = (state: AppState): { data: RestAction[] } => ({
data: state.entities.actions.map(a => a.config),
2020-01-23 07:53:36 +00:00
});
export default connect(mapStateToProps)(ColumnActionSelectorControl);