import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
import { ControlWrapper, StyledPropertyPaneButton } from "./StyledControls";
import { ControlType } from "constants/PropertyControlConstants";
import { AppState } from "reducers";
import { ActionDataState } from "reducers/entityReducers/actionsReducer";
import { connect } from "react-redux";
import { ActionPayload } from "constants/ActionConstants";
import { FinalActionSelector } from "./ActionSelectorControl";
import { generateReactKey } from "utils/generators";
import styled, { theme } from "constants/DefaultTheme";
import { AnyStyledComponent } from "styled-components";
import { FormIcons } from "icons/FormIcons";
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<
ColumnActionSelectorControlProps & ActionDataState
> {
render() {
return (
{this.props.propertyValue &&
this.props.propertyValue.map(
(columnAction: ColumnAction, index: number) => {
return (
);
},
)}
);
}
onTextChange = (event: React.ChangeEvent | 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({
label: "newColumnAction",
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;
const mapStateToProps = (state: AppState): ActionDataState => ({
...state.entities.actions,
});
export default connect(mapStateToProps)(ColumnActionSelectorControl);