Get new widget name based on widget configs

This commit is contained in:
Abhinav Jha 2019-10-03 22:36:44 +05:30
parent 58c0c647bf
commit eb195aa321
3 changed files with 21 additions and 12 deletions

View File

@ -7,12 +7,14 @@ const WidgetConfigResponse: WidgetConfigReducerState = {
buttonStyle: "PRIMARY_BUTTON", buttonStyle: "PRIMARY_BUTTON",
rows: 1, rows: 1,
columns: 2, columns: 2,
widgetName: "Button",
}, },
TEXT_WIDGET: { TEXT_WIDGET: {
text: "Not all labels are bad!", text: "Not all labels are bad!",
textStyle: "LABEL", textStyle: "LABEL",
rows: 1, rows: 1,
columns: 3, columns: 3,
widgetName: "Text",
}, },
IMAGE_WIDGET: { IMAGE_WIDGET: {
defaultImage: "", defaultImage: "",
@ -20,27 +22,32 @@ const WidgetConfigResponse: WidgetConfigReducerState = {
image: "", image: "",
rows: 3, rows: 3,
columns: 3, columns: 3,
widgetName: "Image",
}, },
INPUT_WIDGET: { INPUT_WIDGET: {
inputType: "TEXT", inputType: "TEXT",
label: "Label me", label: "Label me",
rows: 1, rows: 1,
columns: 3, columns: 3,
widgetName: "Input",
}, },
SWITCH_WIDGET: { SWITCH_WIDGET: {
isOn: false, isOn: false,
label: "Turn me on", label: "Turn me on",
rows: 1, rows: 1,
columns: 4, columns: 4,
widgetName: "Switch",
}, },
CONTAINER_WIDGET: { CONTAINER_WIDGET: {
backgroundColor: "#FFFFFF", backgroundColor: "#FFFFFF",
rows: 1, rows: 1,
columns: 4, columns: 4,
widgetName: "Container",
}, },
SPINNER_WIDGET: { SPINNER_WIDGET: {
rows: 1, rows: 1,
columns: 1, columns: 1,
widgetName: "Spinner",
}, },
DATE_PICKER_WIDGET: { DATE_PICKER_WIDGET: {
enableTime: false, enableTime: false,
@ -48,23 +55,27 @@ const WidgetConfigResponse: WidgetConfigReducerState = {
rows: 1, rows: 1,
columns: 3, columns: 3,
label: "Date", label: "Date",
widgetName: "Datepicker",
}, },
TABLE_WIDGET: { TABLE_WIDGET: {
rows: 5, rows: 5,
columns: 7, columns: 7,
label: "Don't table me!", label: "Don't table me!",
widgetName: "Table",
}, },
DROP_DOWN_WIDGET: { DROP_DOWN_WIDGET: {
rows: 1, rows: 1,
columns: 3, columns: 3,
selectionType: "SINGLE_SELECT", selectionType: "SINGLE_SELECT",
label: "Pick me!", label: "Pick me!",
widgetName: "Dropdown",
}, },
CHECKBOX_WIDGET: { CHECKBOX_WIDGET: {
rows: 1, rows: 1,
columns: 3, columns: 3,
label: "Label - CHECK!", label: "Label - CHECK!",
defaultCheckedState: true, defaultCheckedState: true,
widgetName: "Checkbox",
}, },
RADIO_GROUP_WIDGET: { RADIO_GROUP_WIDGET: {
rows: 3, rows: 3,
@ -76,6 +87,7 @@ const WidgetConfigResponse: WidgetConfigReducerState = {
{ label: "Charlie", value: "3" }, { label: "Charlie", value: "3" },
], ],
defaultOptionValue: "1", defaultOptionValue: "1",
widgetName: "RadioGroup",
}, },
ALERT_WIDGET: { ALERT_WIDGET: {
alertType: "NOTIFICATION", alertType: "NOTIFICATION",
@ -84,6 +96,7 @@ const WidgetConfigResponse: WidgetConfigReducerState = {
columns: 3, columns: 3,
header: "", header: "",
message: "", message: "",
widgetName: "Alert",
}, },
}, },
configVersion: 1, configVersion: 1,

View File

@ -47,7 +47,7 @@ export function* addChildSaga(addChildAction: ReduxAction<WidgetAddChild>) {
rows, rows,
parentRowSpace, parentRowSpace,
parentColumnSpace, parentColumnSpace,
getNextWidgetName(type, widgets), getNextWidgetName(defaultWidgetConfig.widgetName, widgets),
defaultWidgetConfig, defaultWidgetConfig,
); );
widgets[childWidget.widgetId] = childWidget; widgets[childWidget.widgetId] = childWidget;

View File

@ -12,7 +12,6 @@ import FontFaceObserver from "fontfaceobserver";
import PropertyControlRegistry from "./PropertyControlRegistry"; import PropertyControlRegistry from "./PropertyControlRegistry";
import WidgetBuilderRegistry from "./WidgetRegistry"; import WidgetBuilderRegistry from "./WidgetRegistry";
import { Property } from "../api/ActionAPI"; import { Property } from "../api/ActionAPI";
import { WidgetType } from "../constants/WidgetConstants";
import { FlattenedWidgetProps } from "../reducers/entityReducers/canvasWidgetsReducer"; import { FlattenedWidgetProps } from "../reducers/entityReducers/canvasWidgetsReducer";
import _ from "lodash"; import _ from "lodash";
@ -64,21 +63,18 @@ export const mapToPropList = (map: Record<string, string>): Property[] => {
}; };
export const getNextWidgetName = ( export const getNextWidgetName = (
type: WidgetType, prefix: string,
widgets: { widgets: {
[id: string]: FlattenedWidgetProps; [id: string]: FlattenedWidgetProps;
}, },
) => { ) => {
const prefix = type const regex = new RegExp(`^${prefix}(\\d+)$`);
.split("_")
.filter(token => token !== "WIDGET")
.join("")
.toLowerCase();
const usedIndices: number[] = Object.values(widgets).map(widget => { const usedIndices: number[] = Object.values(widgets).map(widget => {
if (widget.type === type) { if (widget && widget.widgetName && regex.test(widget.widgetName)) {
const ind = widget.widgetName const name = widget.widgetName || "";
? parseInt(widget.widgetName.split(prefix)[1], 10) const matches = name.match(regex);
: 0; const ind =
matches && Array.isArray(matches) ? parseInt(matches[1], 10) : 0;
return Number.isNaN(ind) ? 0 : ind; return Number.isNaN(ind) ? 0 : ind;
} }
return 0; return 0;