Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds custom sort function feature flag for table widget #39649

Draft
wants to merge 5 commits into
base: release
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/client/src/ce/entities/FeatureFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const FEATURE_FLAG = {
"release_external_saas_plugins_enabled",
release_tablev2_infinitescroll_enabled:
"release_tablev2_infinitescroll_enabled",
release_table_custom_sort_function_enabled:
"release_table_custom_sort_function_enabled",
} as const;

export type FeatureFlag = keyof typeof FEATURE_FLAG;
Expand Down Expand Up @@ -99,6 +101,7 @@ export const DEFAULT_FEATURE_FLAG_VALUE: FeatureFlags = {
release_ads_entity_item_enabled: false,
release_external_saas_plugins_enabled: false,
release_tablev2_infinitescroll_enabled: false,
release_table_custom_sort_function_enabled: false,
};

export const AB_TESTING_EVENT_KEYS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { ControlProps } from "./BaseControl";
import BaseControl from "./BaseControl";
import { StyledDynamicInput } from "./StyledControls";
import type { InputType } from "components/constants";
import type { CodeEditorExpected } from "components/editorComponents/CodeEditor";
import type {
CodeEditorExpected,
EditorProps,
} from "components/editorComponents/CodeEditor";
import type { FieldEntityInformation } from "components/editorComponents/CodeEditor/EditorConfig";
import {
CodeEditorBorder,
Expand Down Expand Up @@ -36,16 +39,20 @@ export function InputText(props: {
enableAI?: boolean;
isEditorHidden?: boolean;
blockCompletions?: FieldEntityInformation["blockCompletions"];
maxHeight?: string | number;
height?: string | number;
}) {
const {
blockCompletions,
dataTreePath,
enableAI = true,
evaluatedValue,
expected,
height,
hideEvaluatedValue,
isEditorHidden,
label,
maxHeight,
onBlur,
onChange,
onFocus,
Expand All @@ -64,6 +71,7 @@ export function InputText(props: {
evaluatedPopUpLabel={label}
evaluatedValue={evaluatedValue}
expected={expected}
height={height as EditorProps["height"]}
hideEvaluatedValue={hideEvaluatedValue}
hinting={[bindingHintHelper, slashCommandHintHelper]}
hoverInteraction
Expand All @@ -72,6 +80,7 @@ export function InputText(props: {
onChange: onChange,
}}
isEditorHidden={isEditorHidden}
maxHeight={maxHeight as EditorProps["maxHeight"]}
mode={EditorModes.TEXT_WITH_BINDING}
onEditorBlur={onBlur}
onEditorFocus={onFocus}
Expand All @@ -92,6 +101,7 @@ class InputTextControl extends BaseControl<InputControlProps> {
render() {
const {
additionalAutoComplete,
controlConfig,
dataTreePath,
defaultValue,
expected,
Expand All @@ -111,9 +121,11 @@ class InputTextControl extends BaseControl<InputControlProps> {
additionalAutocomplete={additionalAutoComplete}
dataTreePath={dataTreePath}
expected={expected}
height={controlConfig?.height as EditorProps["height"]}
hideEvaluatedValue={hideEvaluatedValue}
isEditorHidden={!isOpen}
label={label}
maxHeight={controlConfig?.maxHeight as EditorProps["maxHeight"]}
onBlur={onBlur}
onChange={this.onTextChange}
onFocus={onFocus}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { EditorTheme } from "components/editorComponents/CodeEditor/EditorConfig";
import TableCustomSortControl from "./TableCustomSortControl";

const requiredParams = {
evaluatedValue: "",
widgetProperties: {
widgetName: "Table1",
primaryColumns: {
id: {
alias: "id",
originalId: "id",
},
name: {
alias: "name",
originalId: "name",
},
},
},
parentPropertyName: "",
parentPropertyValue: "",
additionalDynamicData: {},
label: "Custom Sort",
propertyName: "customSort",
controlType: "TABLE_CUSTOM_SORT",
isBindProperty: true,
isTriggerProperty: false,
onPropertyChange: jest.fn(),
openNextPanel: jest.fn(),
deleteProperties: jest.fn(),
hideEvaluatedValue: false,
placeholderText: "Enter custom sort logic",
};

describe("TableCustomSortControl.getInputComputedValue", () => {
it("should extract computation expression correctly from binding string", () => {
const bindingString = `{{(() => {
try {
const tableData = Table1.tableData || [];
const filteredTableData = Table1.filteredTableData || [];
if(filteredTableData.length > 0 && true) {
const sortedTableData = ((originalTableData, computedTableData, column, order) => (computedTableData.length > 5))(tableData, filteredTableData, Table1.sortOrder.column, Table1.sortOrder.order);
return Array.isArray(sortedTableData) && sortedTableData.length > 0 ? sortedTableData : filteredTableData;
}
return filteredTableData;
} catch (e) {
return filteredTableData;
}
})()}}`;

const result = TableCustomSortControl.getInputComputedValue(bindingString);

expect(result).toBe("{{computedTableData.length > 5}}");
});

it("should handle complex expressions", () => {
const bindingString = `{{(() => {
try {
const tableData = Table1.tableData || [];
const filteredTableData = Table1.filteredTableData || [];
if(filteredTableData.length > 0 && true) {
const sortedTableData = ((originalTableData, computedTableData, column, order) => (computedTableData.filter(item => item.id > 10)))(tableData, filteredTableData, Table1.sortOrder.column, Table1.sortOrder.order);
return Array.isArray(sortedTableData) && sortedTableData.length > 0 ? sortedTableData : filteredTableData;
}
return filteredTableData;
} catch (e) {
return filteredTableData;
}
})()}}`;

const result = TableCustomSortControl.getInputComputedValue(bindingString);

expect(result).toBe("{{computedTableData.filter(item => item.id > 10)}}");
});

it("should return original value when binding string doesn't match expected format", () => {
const nonMatchingString = "{{Table1.tableData[0].id}}";
const result =
TableCustomSortControl.getInputComputedValue(nonMatchingString);

expect(result).toBe(nonMatchingString);
});
});

describe("TableCustomSortControl instance methods", () => {
it("generates correct binding with getComputedValue", () => {
const instance = new TableCustomSortControl({
...requiredParams,
theme: EditorTheme.LIGHT,
});

const inputValue = "{{computedTableData.sort((a, b) => a.id - b.id)}}";
const expectedOutput = `{{(() => {
try {
const tableData = Table1.tableData || [];
const filteredTableData = Table1.filteredTableData || [];
if(filteredTableData.length > 0 && computedTableData.sort((a, b) => a.id - b.id)) {
const sortedTableData = ((originalTableData, computedTableData, column, order) => (computedTableData.sort((a, b) => a.id - b.id)))(tableData, filteredTableData, Table1.sortOrder.column, Table1.sortOrder.order);
return Array.isArray(sortedTableData) && sortedTableData.length > 0 ? sortedTableData : filteredTableData;
}
return filteredTableData;
} catch (e) {
return filteredTableData;
}
})()}}`;

// Use a regex to ignore whitespace differences
const normalizeString = (str: string) => str.replace(/\s+/g, " ").trim();

const result = instance.getComputedValue(inputValue, "Table1");

expect(normalizeString(result)).toBe(normalizeString(expectedOutput));
});

it("handles non-binding values correctly in getComputedValue", () => {
const instance = new TableCustomSortControl({
...requiredParams,
theme: EditorTheme.LIGHT,
});

const plainValue = "plain text";
const result = instance.getComputedValue(plainValue, "Table1");

expect(result).toBe(plainValue);
});
});

describe("TableCustomSortControl.getControlType", () => {
it("returns the correct control type", () => {
expect(TableCustomSortControl.getControlType()).toBe("TABLE_CUSTOM_SORT");
});
});
Loading