-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
fix: Enhance SelectWidget label and value handling logic #38254
Conversation
- Updated the label extraction logic to support both static and dynamic keys from sourceData. - Improved value mapping to handle cases where optionValue is an array, ensuring consistency in label and value retrieval. - Added detailed documentation comments to clarify the expected structure of sourceData and the behavior of optionLabel and optionValue props. This change enhances the flexibility and usability of the SelectWidget component.
WalkthroughThe pull request modifies the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
/build-deploy-preview skip-tests=true |
Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/12409205202. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
app/client/src/widgets/SelectWidget/widget/derived.js (1)
8-30
: Consider adding TypeScript interfaces for better type safetyThe documentation clearly explains the data structure and various scenarios. To enhance type safety and developer experience, consider adding TypeScript interfaces for the sourceData structure.
interface SourceDataItem { [key: string]: string | number; // For dynamic properties } interface SelectWidgetProps { sourceData: SourceDataItem[]; optionLabel: string | string[]; optionValue: string | string[]; }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/client/src/widgets/SelectWidget/widget/derived.js
(1 hunks)
🔇 Additional comments (1)
app/client/src/widgets/SelectWidget/widget/derived.js (1)
Line range hint 1-67
: Verify integration with parent components
The changes to array handling logic might affect parent components that pass array-type optionLabel/optionValue props.
- Introduced a new helper function `processOptionArray` to streamline the processing of option arrays for both labels and values. - Simplified the logic for determining if option arrays are consistent and if keys exist in the source data, enhancing code readability and maintainability.
/build-deploy-preview skip-tests=true |
Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/12409342549. |
Deploy-Preview-URL: https://ce-38254.dp.appsmith.com |
- Added functionality to validate and parse array inputs in both labelKeyValidation and valueKeyValidation functions. - Implemented checks to determine if all values in the array are equal, allowing for dynamic key retrieval from sourceData. - Improved error handling and validation messages for better user feedback. This change increases the flexibility and robustness of the SelectWidget component's input handling.
/build-deploy-preview skip-tests=true |
Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/12409972029. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
app/client/src/widgets/SelectWidget/widget/propertyUtils.ts (1)
141-144
: Enhance documentation with examplesThe comments about array validation could be more explicit. Consider adding examples to clarify the expected input and output.
Add examples like:
/** * Validates array inputs where equal values represent a key in sourceData * Example: * Input: ["name", "name", "name"] * sourceData: [{ name: "John" }, { name: "Jane" }] * Output: ["John", "Jane"] */Also applies to: 242-245
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/client/src/widgets/SelectWidget/widget/propertyUtils.ts
(2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
app/client/src/widgets/SelectWidget/widget/propertyUtils.ts
[error] 149-149: Do not access Object.prototype method 'hasOwnProperty' from target object.
It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.
(lint/suspicious/noPrototypeBuiltins)
[error] 250-250: Do not access Object.prototype method 'hasOwnProperty' from target object.
It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.
(lint/suspicious/noPrototypeBuiltins)
/* | ||
* Here assumption is that if evaluated array is all equal, then it is a key, | ||
* and we can return the parsed value(from source data) as the options. | ||
*/ | ||
const areAllValuesEqual = value.every((item, _, arr) => item === arr[0]); | ||
|
||
if ( | ||
areAllValuesEqual && | ||
props.sourceData[0].hasOwnProperty(String(value[0])) | ||
) { | ||
const parsedValue = props.sourceData.map( | ||
(d: Record<string, unknown>) => d[String(value[0])], | ||
); | ||
|
||
return { | ||
parsed: parsedValue, | ||
isValid: true, | ||
messages: [], | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Extract duplicated array validation logic
The array validation logic is duplicated from labelKeyValidation
. Consider extracting this into a shared utility function.
- First, apply the same safety fixes:
if (
areAllValuesEqual &&
+ props.sourceData?.[0] &&
- props.sourceData[0].hasOwnProperty(String(value[0]))
+ Object.hasOwn(props.sourceData[0], String(value[0]))
) {
- Then, extract the common logic:
function validateEqualArrayValues(
value: unknown[],
props: SelectWidgetProps
): ValidationResponse {
const areAllValuesEqual = value.every((item, _, arr) => item === arr[0]);
if (
areAllValuesEqual &&
props.sourceData?.[0] &&
Object.hasOwn(props.sourceData[0], String(value[0]))
) {
const parsedValue = props.sourceData.map(
(d: Record<string, unknown>) => d[String(value[0])]
);
return {
parsed: parsedValue,
isValid: true,
messages: [],
};
}
return null;
}
🧰 Tools
🪛 Biome (1.9.4)
[error] 250-250: Do not access Object.prototype method 'hasOwnProperty' from target object.
It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.
(lint/suspicious/noPrototypeBuiltins)
/* | ||
* Here assumption is that if evaluated array is all equal, then it is a key, | ||
* and we can return the parsed value(from source data) as the options. | ||
*/ | ||
const areAllValuesEqual = value.every((item, _, arr) => item === arr[0]); | ||
|
||
if ( | ||
areAllValuesEqual && | ||
props.sourceData[0].hasOwnProperty(String(value[0])) | ||
) { | ||
const parsedValue = props.sourceData.map( | ||
(d: Record<string, unknown>) => d[String(value[0])], | ||
); | ||
|
||
return { | ||
parsed: parsedValue, | ||
isValid: true, | ||
messages: [], | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add safety checks and use Object.hasOwn
The array validation logic needs additional safety measures:
- Add null check for
props.sourceData[0]
- Replace unsafe
hasOwnProperty
withObject.hasOwn
Apply this diff:
if (
areAllValuesEqual &&
+ props.sourceData?.[0] &&
- props.sourceData[0].hasOwnProperty(String(value[0]))
+ Object.hasOwn(props.sourceData[0], String(value[0]))
) {
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
/* | |
* Here assumption is that if evaluated array is all equal, then it is a key, | |
* and we can return the parsed value(from source data) as the options. | |
*/ | |
const areAllValuesEqual = value.every((item, _, arr) => item === arr[0]); | |
if ( | |
areAllValuesEqual && | |
props.sourceData[0].hasOwnProperty(String(value[0])) | |
) { | |
const parsedValue = props.sourceData.map( | |
(d: Record<string, unknown>) => d[String(value[0])], | |
); | |
return { | |
parsed: parsedValue, | |
isValid: true, | |
messages: [], | |
}; | |
} | |
/* | |
* Here assumption is that if evaluated array is all equal, then it is a key, | |
* and we can return the parsed value(from source data) as the options. | |
*/ | |
const areAllValuesEqual = value.every((item, _, arr) => item === arr[0]); | |
if ( | |
areAllValuesEqual && | |
props.sourceData?.[0] && | |
Object.hasOwn(props.sourceData[0], String(value[0])) | |
) { | |
const parsedValue = props.sourceData.map( | |
(d: Record<string, unknown>) => d[String(value[0])], | |
); | |
return { | |
parsed: parsedValue, | |
isValid: true, | |
messages: [], | |
}; | |
} |
🧰 Tools
🪛 Biome (1.9.4)
[error] 149-149: Do not access Object.prototype method 'hasOwnProperty' from target object.
It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.
(lint/suspicious/noPrototypeBuiltins)
Deploy-Preview-URL: https://ce-38254.dp.appsmith.com |
Description
Fixes #
Issue Number
or
Fixes
Issue URL
Warning
If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.
Automation
/ok-to-test tags="@tag.Select"
🔍 Cypress test results
Tip
🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/12409964844
Commit: 1027f13
Cypress dashboard.
Tags:
@tag.Select
Spec:
Thu, 19 Dec 2024 09:59:56 UTC
Communication
Should the DevRel and Marketing teams inform users about this change?
Summary by CodeRabbit
optionLabel
andoptionValue
properties in theSelectWidget
component, allowing for more flexible input scenarios.