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: BAL-3254 #2921

Merged
merged 6 commits into from
Jan 1, 2025
Merged

feat: BAL-3254 #2921

merged 6 commits into from
Jan 1, 2025

Conversation

chesterkmr
Copy link
Collaborator

@chesterkmr chesterkmr commented Dec 30, 2024

  • Added clearValueOnHide for conditionally rendered fields

Summary by CodeRabbit

  • New Features

    • Added a new hook useClearValueOnHide that automatically clears form field values when the associated UI element is hidden.
    • Enhanced form field management with dynamic value clearing capabilities.
  • Improvements

    • Introduced event emission logic in dynamic UI input components.
    • Enhanced logic for retrieving country codes based on input indices.
  • Types

    • Extended UI schema types to support optional value clearing on element hide.

Copy link

changeset-bot bot commented Dec 30, 2024

⚠️ No Changeset found

Latest commit: a1782f6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Dec 30, 2024

Walkthrough

This pull request introduces a new useClearValueOnHide hook in the JSON Form system, which automatically clears form field values when a UI element becomes hidden. The implementation spans multiple files, adding a new hook, modifying the FieldTemplate and withDynamicUIInput components, and extending the UIElement interface to support this new functionality. The changes enhance the dynamic behavior of form elements by providing a mechanism to manage field visibility and associated values.

Changes

File Change Summary
...JSONForm/components/FieldTemplate/FieldTemplate.tsx Added import and usage of useClearValueOnHide hook
...JSONForm/hocs/withDynamicUIInput/withDynamicUIInput.tsx Added event emitter logic, exported injectIndexToDestinationIfNeeded function
...JSONForm/hooks/useClearValueOnHide/index.ts Added export for all entities from useClearValueOnHide module
...JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts New hook implementation for clearing values when UI elements are hidden
...JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts Added comprehensive unit tests for the new hook
...collection-flow/types/ui-schema.types.ts Added optional clearValueOnHide property to UIElement interface
...JSONForm/components/StatePicker/StatePicker.tsx Updated logic for deriving countryCode using injectIndexToDestinationIfNeeded

Suggested Reviewers

  • alonp99
  • tomer-shvadron

Poem

🐰 A rabbit's tale of forms so neat,
Where hidden fields make logic complete,
Values vanish with a gentle touch,
No clutter, no mess—we've learned so much!
Hop, hop, hooray for clean UI delight! 🎉

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
apps/kyb-app/src/domains/collection-flow/types/ui-schema.types.ts (1)

66-68: Consider documenting the new optional property for clarity.
The clearValueOnHide property is a welcome addition and appears logically consistent. However, adding a brief description in comments or in the relevant documentation would help other developers understand when and how to use valueDestination.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (1)

23-38: Replace console.log with a more robust logging method or remove it in production.
Leaving console statements in production code can create clutter and potential performance hits. Consider removing or replacing with a dedicated logger.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hocs/withDynamicUIInput/withDynamicUIInput.tsx (1)

33-36: Export function name clarity.
Exporting injectIndexToDestinationIfNeeded is helpful for external usage. Consider renaming to something shorter or more descriptive (e.g., applyArrayIndexToPath) to improve readability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5219746 and 3990e42.

📒 Files selected for processing (6)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FieldTemplate/FieldTemplate.tsx (3 hunks)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hocs/withDynamicUIInput/withDynamicUIInput.tsx (3 hunks)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/index.ts (1 hunks)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (1 hunks)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts (1 hunks)
  • apps/kyb-app/src/domains/collection-flow/types/ui-schema.types.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/index.ts
🔇 Additional comments (14)
apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (2)

1-2: Imports look good.
No issues with these import statements; they correctly reference relevant modules and maintain consistency across the project.


9-22: Check logic around refs for potential concurrency.
Storing both payload and setContext in a ref is practical, but keep an eye on concurrency or stale data issues if the logic grows in complexity. This approach looks solid for the current use case.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FieldTemplate/FieldTemplate.tsx (2)

14-14: Import of the new hook is straightforward.
This import properly references the custom hook. Integration steps look consistent with the rest of the codebase.


53-54: Hook usage is concise and well-placed.
Invoking the useClearValueOnHide hook here is a clean way to ensure values are cleared once the field is hidden. Implementation appears correct.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hocs/withDynamicUIInput/withDynamicUIInput.tsx (2)

3-3: New event emitter import integrated smoothly.
The import for useEventEmitterLogic fits seamlessly, preparing the wrapper for future event-based logic expansions.


12-12: Ref-based utility import looks solid.
The useRefValue hook appears to help maintain consistent references. Implementation is straightforward.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts (8)

1-11: Good practice with well-organized imports.

The imports are concise and dedicated to relevant testing utilities. No concerns here.


12-30: Test suite structure is logical and well-defined.

The describe block sets up everything cleanly, and mocking is handled in beforeEach. This promotes clean separation of concerns and fosters test reliability.


31-43: Comprehensive test scenario for the absence of clearValueOnHide.

Verifies that values remain intact when the feature is not enabled. This coverage ensures no accidental mutation occurs if the config is missing.


45-60: Correct approach for testing visibility logic.

Ensures hidden state is respected before clearing any values, preventing unintended data loss.


62-78: Validates the primary objective of clearing values.

This test confirms the core functionality when clearValueOnHide is set. Well-structured expectations are present.


79-111: Proper handling of custom destinations.

Demonstrates that alternate fields can be cleared without impacting unrelated ones. This flexibility is crucial for dynamic UIs.


113-140: Robust test for array index usage.

It's great to see indexing logic tested, ensuring the hook can handle arrays gracefully. This reduces edge-case regressions.


142-214: Extensive rerender coverage.

Verifies that values remain unmodified upon toggling from hidden to visible, and correctly cleared from visible to hidden. This is comprehensive coverage of potential state transitions.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/StatePicker/StatePicker.tsx (1)

22-25: Consider adding a fallback for an invalid country code path.
If injectIndexToDestinationIfNeeded forms an unexpected path that returns undefined, subsequent steps might fail silently. Adding a fallback or validation can prevent errors.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3990e42 and 7886082.

📒 Files selected for processing (1)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/StatePicker/StatePicker.tsx (2 hunks)
🔇 Additional comments (3)
apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/StatePicker/StatePicker.tsx (3)

6-6: Importing useMemo is appropriate and consistent with React best practices.
The added import for useMemo is correct, ensuring the memoization of computed values.


7-7: Confirm full test coverage for injectIndexToDestinationIfNeeded.
While importing and using injectIndexToDestinationIfNeeded is valid, it would be beneficial to confirm this function is robustly tested to handle various edge cases (e.g., invalid indices, missing paths).


30-30: Dependency array update looks good.
Adding props.inputIndex to the memo’s dependency array ensures recalculation when the index changes, aligning with React’s recommended usage.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (3)

14-18: Consider avoiding direct mutations of the payload object
The code directly mutates ref.current.payload. If your state management depends on immutability for proper updates or to avoid tricky bugs, consider creating a shallow or deep copy before modifying.

- set(ref.current.payload, formattedDestination, filteredItems);
- ref.current.setContext(ref.current.payload);
+ const newPayload = structuredClone(ref.current.payload);
+ set(newPayload, formattedDestination, filteredItems);
+ ref.current.setContext(newPayload);

24-49: Refactor repetitive removal logic
The removal logic is repeated under the condition for clearValueOnHide.byId (lines 24-49) and again for clearValueOnHide.valueDestination (lines 51-65). Consider extracting a helper function to avoid duplication, making the code more maintainable and less prone to errors.


42-45: Remove or replace console logging
Console logs can leak sensitive details (depending on the data in payload) and clutter logs in production. Consider removing them or using a debug logging utility that can be enabled or disabled in different environments.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts (3)

7-9: Use partial mocking or explicit mocks
Currently, the entire modules for StateProvider and useUIElementProps are mocked. If only specific methods or hooks need mocking, partial mocks can preserve real behavior for other parts, reducing the risk of diverging from the real implementation.


46-66: Expand tests to cover toggling visibility
The test checks clearing the value by ID when hidden. Consider adding a test that toggles hidden from false to true and back, ensuring no erroneous re-clearing occurs when the element is re-hidden.


105-125: Increase coverage for edge cases
The current test for array index clearing is good. Additional edge cases—like an index out of range or an undefined array—could further ensure robust handling of the payload structure.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7886082 and 12935e5.

📒 Files selected for processing (3)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (1 hunks)
  • apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts (1 hunks)
  • apps/kyb-app/src/domains/collection-flow/types/ui-schema.types.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/kyb-app/src/domains/collection-flow/types/ui-schema.types.ts
🔇 Additional comments (2)
apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.ts (1)

66-67: Check the dependencies in the useEffect
The dependency array references definition.valueDestination, even though the code checks definition.clearValueOnHide.valueDestination internally. Make sure this is intentional. If you want to specifically watch for changes to the nested property, consider referencing it directly.

apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/hooks/useClearValueOnHide/useClearValueOnHide.unit.test.ts (1)

32-44: Add assertions for unchanged payload values
When verifying that no clear operation occurs if clearValueOnHide is not defined, it might be beneficial to also assert that mockPayload remains unchanged to confirm the code doesn't mutate anything unexpectedly.

@chesterkmr chesterkmr changed the title feat: BAL-3254 (WIP) feat: BAL-3254 Dec 31, 2024
@tomer-shvadron tomer-shvadron enabled auto-merge (squash) January 1, 2025 10:13
@tomer-shvadron tomer-shvadron merged commit bcd15d5 into dev Jan 1, 2025
10 checks passed
@tomer-shvadron tomer-shvadron deleted the bal-3254 branch January 1, 2025 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants