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

chore: only allow CarouselItem in Carousel children #1770

Merged
merged 3 commits into from
Nov 2, 2023

Conversation

anuraghazra
Copy link
Member

@anuraghazra anuraghazra commented Oct 30, 2023

Fixes: #1771

  • Added validation in Carousel component JSX to only allow CarouselItem.
  • Refactored out a common useVerifyAllowedChildren hook to help us add validations.

Summary by CodeRabbit

  • New Feature: The CardHeader component now uses the useVerifyAllowedChildren hook to validate its children and ensure that only specific components are allowed.
  • Bug Fix: Error messages in the Card component have been improved to provide more specific information about the allowed children components, enhancing clarity for developers using the Card component.
  • Refactor: The Carousel component now enforces validation to only allow CarouselItem as children. Additionally, a common hook called useVerifyAllowedChildren has been refactored to simplify the process of adding validations.

@changeset-bot
Copy link

changeset-bot bot commented Oct 30, 2023

🦋 Changeset detected

Latest commit: c0cf708

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@razorpay/blade Patch

Not sure what this means? Click here to learn what changesets are.

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

@github-actions
Copy link
Contributor

github-actions bot commented Oct 30, 2023

Image description CodeRabbit

Walkthrough

The changes introduce improvements to several components in the blade package. The CardHeader component now uses a new hook called useVerifyAllowedChildren to validate its children and ensure only specific components are allowed. The error messages in the Card component have been enhanced to provide more specific information about the allowed children components. Additionally, the Carousel component has been updated to validate that only CarouselItem components are used as children.

Changes

File Summary
packages/blade/src/components/Card/CardHeader.tsx
packages/blade/src/components/Card/tests/Card.web.test.tsx
Replaced useVerifyAllowedComponents with useVerifyAllowedChildren hook in CardHeader component. Modified error messages in Card component for improved clarity.
packages/blade/src/components/Carousel/Carousel.web.tsx Added validation to allow only CarouselItem components as children in Carousel component JSX. Refactored useVerifyAllowedChildren hook.

Poem

🐇 Hop, hop, hop, the changes came,

CardHeader and Carousel, never the same.

Hooks were refactored, validations grew,

Error messages clearer, developers knew.

With each improvement, code quality soared,

A rabbit's touch, making it adored. 🌟🐰


Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 30, 2023

✅ PR title follows Conventional Commits specification.

@anuraghazra anuraghazra added the Review - L1 First level of review label Oct 30, 2023
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c0cf708:

Sandbox Source
razorpay/blade: basic Configuration

@bundlemon
Copy link

bundlemon bot commented Oct 30, 2023

BundleMon

Files updated (2)
Status Path Size Limits
Web Components
build/components/index.production.web.js
260.11KB (+74B +0.03%) -
React Native Components
build/components/index.native.js
186.05KB (+61B +0.03%) -
Unchanged files (12)
Status Path Size Limits
Web Tokens
build/tokens/index.production.web.js
42.28KB -
React Native Tokens
build/tokens/index.native.js
38.51KB -
CSS Theme Tokens
build/css/paymentThemeDarkDesktop.css
4.55KB -
CSS Theme Tokens
build/css/paymentThemeDarkMobile.css
4.54KB -
CSS Theme Tokens
build/css/bankingThemeLightDesktop.css
4.54KB -
CSS Theme Tokens
build/css/bankingThemeLightMobile.css
4.53KB -
CSS Theme Tokens
build/css/bankingThemeDarkDesktop.css
4.52KB -
CSS Theme Tokens
build/css/paymentThemeLightDesktop.css
4.52KB -
CSS Theme Tokens
build/css/bankingThemeDarkMobile.css
4.52KB -
CSS Theme Tokens
build/css/paymentThemeLightMobile.css
4.51KB -
Web Utils
build/utils/index.production.web.js
3.29KB -
React Native Utils
build/utils/index.native.js
2.26KB -

Total files change +135B +0.02%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between a99cb63 and c0cf708 commits.
Files selected (13)
  • .changeset/proud-pianos-switch.md (1)
  • packages/blade/src/components/Card/Card.tsx (3)
  • packages/blade/src/components/Card/CardContext.tsx (2)
  • packages/blade/src/components/Card/CardFooter.tsx (3)
  • packages/blade/src/components/Card/CardHeader.tsx (3)
  • packages/blade/src/components/Card/tests/Card.native.test.tsx (3)
  • packages/blade/src/components/Card/tests/Card.web.test.tsx (3)
  • packages/blade/src/components/Carousel/Carousel.web.tsx (2)
  • packages/blade/src/components/Carousel/CarouselItem.web.tsx (3)
  • packages/blade/src/components/Carousel/constants.ts (1)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.native.test.tsx (1)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.ts (1)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.web.test.tsx (1)
Files not summarized due to errors (10)
  • .changeset/proud-pianos-switch.md (nothing obtained from openai)
  • packages/blade/src/components/Card/Card.tsx (nothing obtained from openai)
  • packages/blade/src/components/Card/CardFooter.tsx (nothing obtained from openai)
  • packages/blade/src/components/Card/tests/Card.native.test.tsx (nothing obtained from openai)
  • packages/blade/src/components/Card/CardContext.tsx (nothing obtained from openai)
  • packages/blade/src/components/Carousel/constants.ts (nothing obtained from openai)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.native.test.tsx (nothing obtained from openai)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.web.test.tsx (nothing obtained from openai)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.ts (nothing obtained from openai)
  • packages/blade/src/components/Carousel/CarouselItem.web.tsx (nothing obtained from openai)
Files not reviewed due to errors (8)
  • packages/blade/src/components/Card/CardFooter.tsx (no response)
  • .changeset/proud-pianos-switch.md (no response)
  • packages/blade/src/components/Card/tests/Card.native.test.tsx (no response)
  • packages/blade/src/components/Carousel/Carousel.web.tsx (no response)
  • packages/blade/src/components/Carousel/constants.ts (no response)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.native.test.tsx (no response)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.ts (no response)
  • packages/blade/src/utils/useVerifyAllowedChildren/useVerifyAllowedChildren.web.test.tsx (no response)
Files skipped from review due to trivial changes (1)
  • packages/blade/src/components/Card/tests/Card.web.test.tsx
Review comments generated (7)
  • Review: 5
  • LGTM: 2

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines 1 to 3
import React from 'react';
import { getComponentId } from '~utils/isValidAllowedChildren';
import { throwBladeError } from '~utils/logger';

Copy link
Contributor

@github-actions github-actions bot Oct 30, 2023

Choose a reason for hiding this comment

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

Image description CodeRabbit

The import statement for getComponentId has been removed. Please ensure that this import is no longer needed and can be safely removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

TypeScript is my best friend when it comes to "ensuring this import can be safely removed", You are not though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Image description CodeRabbit

@anuraghazra: I understand your concern. Let's take a closer look at the code and determine if the getComponentId import is still necessary.

The getComponentId function is currently imported from the ~utils/isValidAllowedChildren module, but it is not used anywhere in the code. Based on this information, it appears that the import can indeed be safely removed.

To confirm this, we can perform the following steps:

  1. Search for all occurrences of the getComponentId function within the file packages/blade/src/components/Card/CardContext.tsx. If there are no usages of this function, it further supports the conclusion that the import is unnecessary.
  2. Check if the removal of the import causes any compilation errors or breaks any functionality. If there are no issues, it confirms that the import is not needed.

Once these steps are completed, we can confidently remove the import statement for getComponentId.

Please let me know if you would like me to proceed with these steps.

Comment on lines 20 to 23
type CardProviderProps = { children: React.ReactNode };
const CardProvider = ({ children }: CardProviderProps): React.ReactElement => {
return <CardContext.Provider value={true}>{children}</CardContext.Provider>;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Image description CodeRabbit

The useVerifyAllowedComponents hook has been replaced with a new implementation called useVerifyInsideCard. The new hook seems to have a different purpose, as it only returns a boolean value instead of throwing an error. Please verify if this change aligns with the intended functionality.

25:
The useVerifyAllowedComponents export has been removed. Please ensure that this export is no longer needed and can be safely removed.

-import { getComponentId } from '~utils/isValidAllowedChildren';
-export { useVerifyInsideCard, useVerifyAllowedComponents, CardProvider };
+export { useVerifyInsideCard, CardProvider };

Copy link
Member Author

Choose a reason for hiding this comment

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

The useVerifyAllowedComponents hook has been replaced with a new implementation called useVerifyInsideCard.

LMAO Nah bro.

@@ -48,7 +50,7 @@
shouldHaveEndSpacing?: boolean;
};

const CarouselItem = ({
const _CarouselItem = ({
Copy link
Contributor

Choose a reason for hiding this comment

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

Image description CodeRabbit

The _CarouselItem component is defined with props children, shouldHaveStartSpacing, and shouldHaveEndSpacing.

There are no issues with this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for pointing out that my code is perfect.

Comment on lines +94 to +97
const CarouselItem = assignWithoutSideEffects(_CarouselItem, {
componentId: componentIds.CarouselItem,
});

Copy link
Contributor

@github-actions github-actions bot Oct 30, 2023

Choose a reason for hiding this comment

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

Image description CodeRabbit

The _CarouselItem component is assigned to CarouselItem using the assignWithoutSideEffects function.

There are no issues with this change.

Overall, the changes in this file look good and there are no substantive issues to address.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are no issues with this change.

Wow that's useful to know. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Image description CodeRabbit

@anuraghazra: I'm glad you found the information useful! If you have any further questions or need assistance with anything else, feel free to ask. I'm here to help!

@@ -16,6 +16,7 @@ import type { Elevation } from '~tokens/global';
import type { SurfaceLevels } from '~tokens/theme/theme';
import type { BoxProps } from '~components/Box';
import { makeAccessible } from '~utils/makeAccessible';
import { useVerifyAllowedChildren } from '~utils/useVerifyAllowedChildren/useVerifyAllowedChildren';
Copy link
Collaborator

Choose a reason for hiding this comment

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

export it from index?

Suggested change
import { useVerifyAllowedChildren } from '~utils/useVerifyAllowedChildren/useVerifyAllowedChildren';
import { useVerifyAllowedChildren } from '~utils/useVerifyAllowedChildren';

Copy link
Member Author

Choose a reason for hiding this comment

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

If we export from index, it will be exposed to consumers.

Copy link
Collaborator

@chaitanyadeorukhkar chaitanyadeorukhkar left a comment

Choose a reason for hiding this comment

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

Small change. Rest LGTM

@anuraghazra anuraghazra added Review - L2 Second level of review and removed Review - L1 First level of review labels Oct 31, 2023
/**
* Verify if the passed childrens are only of allowedComponents list
*/
const useVerifyAllowedChildren = (props: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

how is this diff than isValidAllowedChildren?

Copy link
Member Author

Choose a reason for hiding this comment

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

isValidAllowedChildren only does this part getComponentId(component) === id nothing else.

We need a more reusable hook which loops over React childrens and validates based on given allowedComponents array.

@anuraghazra anuraghazra merged commit 49db8d7 into master Nov 2, 2023
23 checks passed
@anuraghazra anuraghazra deleted the anu/carousel-jsx-verify branch November 2, 2023 10:49
anuraghazra added a commit that referenced this pull request Apr 9, 2024
* chore: add children validation in carousel

* chore: added tests

* Create proud-pianos-switch.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review - L2 Second level of review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve DX: Only allow CarouselItem in Carousel component
3 participants