PromucFlow_constructor/app/client/src/utils/PropertyControlFactory.tsx
Pawan Kumar 9ffcb64da9
chore: Add documentation for property pane config (#36880)
/ok-to-test tags="@tag.Anvil"

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced a new `PropertyControlFactory` class for managing property
controls.
- Added new properties to the `PropertyPaneControlConfig` interface,
enhancing customization and validation options.
- Implemented methods for creating and registering control builders,
improving the flexibility of property controls.

- **Documentation**
- Enhanced JSDoc comments for existing properties and methods, providing
clearer guidance on their usage.
- Updated documentation for the `PropertyPaneSectionConfig`,
`PropertyPaneControlConfig`, and `ValidationConfigParams` interfaces to
clarify their functionality.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/11716829822>
> Commit: 62df82621a44126f2d876c77a892010265ec53d3
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11716829822&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Anvil`
> Spec:
> <hr>Thu, 07 Nov 2024 05:15:44 UTC
<!-- end of auto-generated comment: Cypress test results  -->
2024-11-07 15:41:20 +05:30

104 lines
3.1 KiB
TypeScript

import type { ControlType } from "constants/PropertyControlConstants";
import type {
ControlBuilder,
ControlProps,
ControlFunctions,
ControlData,
ControlMethods,
} from "components/propertyControls/BaseControl";
import type BaseControl from "components/propertyControls/BaseControl";
import { isArray } from "lodash";
import type { AdditionalDynamicDataTree } from "./autocomplete/customTreeTypeDefCreator";
/**
* PropertyPaneControlFactory
*
* This classes manages all the available controls for the property pane.
* It maintains a map of control types to their respective builders.
* The control builders are responsible for creating the actual controls.
*
* Key functionalities:
* 1. Register control builders, methods, and computed value functions
* 2. Create controls based on control data and preferences
* 3. Retrieve available control types
*/
class PropertyControlFactory {
static controlMap: Map<ControlType, ControlBuilder<ControlProps>> = new Map();
static controlMethods: Map<ControlType, ControlMethods> = new Map();
static inputComputedValueMap: Map<
ControlType,
typeof BaseControl.getInputComputedValue
> = new Map();
static registerControlBuilder(
controlType: ControlType,
controlBuilder: ControlBuilder<ControlProps>,
controlMethods: ControlMethods,
inputComputedValueFn: typeof BaseControl.getInputComputedValue,
) {
this.controlMap.set(controlType, controlBuilder);
this.controlMethods.set(controlType, controlMethods);
this.inputComputedValueMap.set(controlType, inputComputedValueFn);
}
static createControl(
controlData: ControlData,
controlFunctions: ControlFunctions,
preferEditor: boolean,
customEditor?: string,
additionalAutoComplete?: AdditionalDynamicDataTree,
hideEvaluatedValue?: boolean,
isSearchResult?: boolean,
): JSX.Element {
let controlBuilder;
let evaluatedValue = controlData.evaluatedValue;
if (preferEditor) {
controlBuilder = customEditor
? this.controlMap.get(customEditor)
: this.controlMap.get("CODE_EDITOR");
} else {
if (customEditor === "COMPUTE_VALUE" && isArray(evaluatedValue)) {
evaluatedValue = evaluatedValue[0];
}
controlBuilder = this.controlMap.get(controlData.controlType);
}
if (controlBuilder) {
const controlProps: ControlProps = {
...controlData,
...controlFunctions,
evaluatedValue,
key: controlData.id,
customJSControl: customEditor,
additionalAutoComplete,
hideEvaluatedValue,
isSearchResult,
};
const control = controlBuilder.buildPropertyControl(controlProps);
return control;
} else {
const ex: ControlCreationException = {
message:
"Control Builder not registered for control type " +
controlData.controlType,
};
throw ex;
}
}
static getControlTypes(): ControlType[] {
return Array.from(this.controlMap.keys());
}
}
export interface ControlCreationException {
message: string;
}
export default PropertyControlFactory;