import React from "react";
import { Icon } from "@blueprintjs/core";
import moment from "moment-timezone";
import {
MenuColumnWrapper,
CellWrapper,
ActionWrapper,
} from "./TableStyledWrappers";
import { ColumnAction } from "components/propertyControls/ColumnActionSelectorControl";
import { ColumnMenuOptionProps } from "./ReactTableComponent";
import { isString } from "lodash";
interface MenuOptionProps {
columnAccessor?: string;
isColumnHidden: boolean;
columnType: string;
format?: string;
hideColumn: (isColumnHidden: boolean) => void;
updateAction: (action: string) => void;
updateColumnType: (columnType: string) => void;
handleUpdateCurrencySymbol: (currencySymbol: string) => void;
handleDateFormatUpdate: (dateFormat: string) => void;
}
export const getMenuOptions = (props: MenuOptionProps) => {
const basicOptions: ColumnMenuOptionProps[] = [
{
content: "Rename a Column",
closeOnClick: true,
id: "rename_column",
onClick: () => {
props.updateAction("rename_column");
},
},
{
content: props.isColumnHidden ? "Show Column" : "Hide Column",
closeOnClick: true,
id: "hide_column",
onClick: () => {
props.hideColumn(props.isColumnHidden);
},
},
];
if (props.columnAccessor && props.columnAccessor === "actions") {
return basicOptions;
}
const columnMenuOptions: ColumnMenuOptionProps[] = [
...basicOptions,
{
content: "Select a Data Type",
id: "change_column_type",
category: true,
},
{
content: (
Image
),
closeOnClick: true,
isSelected: props.columnType === "image",
onClick: (isSelected: boolean) => {
if (isSelected) {
props.updateColumnType("");
} else {
props.updateColumnType("image");
}
},
},
{
content: (
Video
),
isSelected: props.columnType === "video",
closeOnClick: true,
onClick: (isSelected: boolean) => {
if (isSelected) {
props.updateColumnType("");
} else {
props.updateColumnType("video");
}
},
},
{
content: (
Text
),
closeOnClick: true,
isSelected: props.columnType === "text",
onClick: (isSelected: boolean) => {
if (isSelected) {
props.updateColumnType("");
} else {
props.updateColumnType("text");
}
},
},
{
content: (
Currency
),
closeOnClick: false,
isSelected: props.columnType === "currency",
options: [
{
content: "USD - $",
isSelected: props.format === "$",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("$");
},
},
{
content: "INR - ₹",
isSelected: props.format === "₹",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("₹");
},
},
{
content: "GBP - £",
isSelected: props.format === "£",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("£");
},
},
{
content: "AUD - A$",
isSelected: props.format === "A$",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("A$");
},
},
{
content: "EUR - €",
isSelected: props.format === "€",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("€");
},
},
{
content: "SGD - S$",
isSelected: props.format === "S$",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("S$");
},
},
{
content: "CAD - C$",
isSelected: props.format === "C$",
closeOnClick: true,
onClick: () => {
props.handleUpdateCurrencySymbol("C$");
},
},
],
},
{
content: (
Date
),
closeOnClick: false,
isSelected: props.columnType === "date",
options: [
{
content: "MM-DD-YY",
isSelected: props.format === "MM-DD-YY",
closeOnClick: true,
onClick: () => {
props.handleDateFormatUpdate("MM-DD-YY");
},
},
{
content: "DD-MM-YY",
isSelected: props.format === "DD-MM-YY",
closeOnClick: true,
onClick: () => {
props.handleDateFormatUpdate("DD-MM-YY");
},
},
{
content: "DD/MM/YY",
isSelected: props.format === "DD/MM/YY",
closeOnClick: true,
onClick: () => {
props.handleDateFormatUpdate("DD/MM/YY");
},
},
{
content: "MM/DD/YY",
isSelected: props.format === "MM/DD/YY",
closeOnClick: true,
onClick: () => {
props.handleDateFormatUpdate("MM/DD/YY");
},
},
],
},
{
content: (
Time
),
closeOnClick: true,
isSelected: props.columnType === "time",
onClick: (isSelected: boolean) => {
if (isSelected) {
props.updateColumnType("");
} else {
props.updateColumnType("time");
}
},
},
];
return columnMenuOptions;
};
export const renderCell = (
value: any,
rowIndex: number,
columnType: string,
isHidden: boolean,
widgetId: string,
format?: string,
) => {
if (!value) {
return
;
}
switch (columnType) {
case "image":
if (!isString(value)) {
return (
Invalid Image
);
}
return (
{value
.toString()
.split(",")
.map((item: string, index: number) => {
if (item.match(/\.(jpeg|jpg|gif|png)$/)) {
return (
);
} else {
return Invalid Image
;
}
})}
);
case "video":
const youtubeRegex = new RegExp(
"^(https?://)?(www.)?(youtube.com|youtu.?be)/embed/.+$",
);
if (isString(value) && youtubeRegex.test(value)) {
return (
);
} else {
return (
Invalid Video Link
);
}
case "currency":
if (!isNaN(value)) {
return (
{`${format}${value}`}
);
} else {
return Invalid Value;
}
case "date":
let isValidDate = true;
if (isNaN(value)) {
const dateTime = Date.parse(value);
if (isNaN(dateTime)) {
isValidDate = false;
}
}
if (isValidDate) {
return (
{moment(value).format(format)}
);
} else {
return Invalid Date;
}
case "time":
let isValidTime = true;
if (isNaN(value)) {
const time = Date.parse(value);
if (isNaN(time)) {
isValidTime = false;
}
}
if (isValidTime) {
return (
{moment(value).format("HH:mm")}
);
} else {
return Invalid Time;
}
case "text":
const text = isString(value) ? value : JSON.stringify(value);
return {text};
default:
const data = isString(value) ? value : JSON.stringify(value);
return {data};
}
};
interface RenderActionProps {
columnActions?: ColumnAction[];
onCommandClick: (dynamicTrigger: string) => void;
}
export const renderActions = (props: RenderActionProps) => {
if (!props.columnActions) return ;
return (
{props.columnActions.map((action: ColumnAction, index: number) => {
return (
{
props.onCommandClick(action.dynamicTrigger);
}}
>
{action.label}
);
})}
);
};