PromucFlow_constructor/app/client/src/components/propertyControls/OptionControl.tsx
Pawan Kumar 764d8f3cb5
chore: fix prop name in options control (#37130)
/ok-to-test tags="@tag.Widget"

The options control was using `options` as the property name for the
options array even when thee prop name was not `options`. This was
causing issues when the property name was different.

<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/11590623056>
> Commit: 1e16edb367a0bbbfb54db681cb34283f01724e0a
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11590623056&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Widget`
> Spec:
> <hr>Wed, 30 Oct 2024 11:11:49 UTC
<!-- end of auto-generated comment: Cypress test results  -->


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

- **New Features**
- Enhanced the `OptionControl` component to support dynamic property
updates based on component props.

- **Bug Fixes**
- Improved the handling of property updates, allowing for more flexible
and accurate updates.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-10-30 16:43:00 +05:30

54 lines
1.4 KiB
TypeScript

import React from "react";
import type { ControlData, ControlProps } from "./BaseControl";
import BaseControl from "./BaseControl";
import type { DropdownOption } from "components/constants";
import { KeyValueComponent } from "./KeyValueComponent";
import { isDynamicValue } from "utils/DynamicBindingUtils";
import type { SegmentedControlOption } from "@appsmith/ads";
class OptionControl extends BaseControl<ControlProps> {
render() {
return (
<KeyValueComponent
pairs={this.props.propertyValue}
updatePairs={this.updateOptions}
/>
);
}
updateOptions = (
options: SegmentedControlOption[],
isUpdatedViaKeyboard = false,
) => {
this.updateProperty(this.props.propertyName, options, isUpdatedViaKeyboard);
};
static getControlType() {
return "OPTION_INPUT";
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static canDisplayValueInUI(config: ControlData, value: any): boolean {
if (isDynamicValue(value)) return false;
try {
const pairs: DropdownOption[] = JSON.parse(value);
for (const x of pairs) {
const keys = Object.keys(x);
if (!keys.includes("label") || !keys.includes("value")) {
return false;
}
}
} catch {
return false;
}
return true;
}
}
export default OptionControl;