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: added types to TreeNode & TreeView #17038

Conversation

imp-dance
Copy link
Contributor

@imp-dance imp-dance commented Jul 24, 2024

Closes #13581
Relates to #12513

Changelog

New

  • Typescript types for TreeNode & TreeView components

Changed

  • Fallback on certain aria attributes should be undefined, not null

Testing / Reviewing

Did a tiny test, but it'd be useful if someone with intimate knowledge of how the component internals work could take a look.

Some of the props (depth, selected) seem to be meant for only internal use, so I've typed them as optional (and ensured that the JSDoc comment is there) - but made sure to cast them as non nullable inside the component - assuming the parent ensures that they are given.

I also had to use // @ts-ignore in a couple of spots, namely in the PropTypes - since the CarbonIconType does not seem to match PropTypes.oneOfType([PropTypes.func, PropTypes.object]), and (string | number)[] does not match PropTypes.arrayOf( PropTypes.oneOfType([PropTypes.string, PropTypes.number]) ),. If you know of any way we could drop the use of ts ignore here and fix the types or proptypes, that would be great.

Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 675d043
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/66a1583ff653de0008c75813
😎 Deploy Preview https://deploy-preview-17038--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 675d043
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/66a1583ffcb78600085b486f
😎 Deploy Preview https://deploy-preview-17038--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 47da411
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/66afa2c7f7edf40008ab54ec
😎 Deploy Preview https://deploy-preview-17038--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jul 24, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 47da411
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/66afa2c787a7fa0008df06de
😎 Deploy Preview https://deploy-preview-17038--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@imp-dance
Copy link
Contributor Author

Also looks like this is the last checkbox @ #12513 - which I guess means the issue should either be closed or be updated with new items 🚀

Copy link
Contributor

@Gururajj77 Gururajj77 left a comment

Choose a reason for hiding this comment

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

Hi @imp-dance! 👋

Great work on the PR, it's looking good overall. I have one small suggestion:

Could you please add comments to the remaining props in the interface? This helps improve the intellisense for the typings, making it easier for developers to understand and use the component.

Thanks for your attention to detail! 🙌

@imp-dance
Copy link
Contributor Author

Hi @imp-dance! 👋

Great work on the PR, it's looking good overall. I have one small suggestion:

Could you please add comments to the remaining props in the interface? This helps improve the intellisense for the typings, making it easier for developers to understand and use the component.

Thanks for your attention to detail! 🙌

Done! 🚀

@imp-dance
Copy link
Contributor Author

Actually, hold on a bit - I'll update the index file as well, and make sure that TreeView.TreeNode is typed properly.

@imp-dance
Copy link
Contributor Author

@Gururajj77 Done 👍 I'd appreciate another quick look over the changes when you can 😎

Copy link
Member

@tay1orjones tay1orjones left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! This looks good to me, just a couple notes

@@ -310,3 +388,6 @@ TreeView.propTypes = {
*/
size: PropTypes.oneOf(['xs', 'sm']),
};

TreeView.TreeNode = TreeNode;
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind adding a test to ensure <TreeView.TreeNode> works?

Copy link
Contributor Author

@imp-dance imp-dance Jul 26, 2024

Choose a reason for hiding this comment

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

Done, I just copy/pasted should render children as expected into should render children as expected when using dot syntax and refactored it to use <TreeView.TreeNode> instead of <TreeNode>.

packages/react/src/components/TreeView/TreeNode.tsx Outdated Show resolved Hide resolved
Comment on lines +119 to +121
// These are provided by the parent TreeView component
const depth = propDepth as number;
const selected = propSelected as (string | number)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have another proposal.

Since the component internals expect that these should always be defined, should we do something like this to provide a better error for consumers that try to mount TreeNode outside of TreeView?

if (depth === undefined || selected === undefined){
   throw new Error("It looks like you are trying to mount a TreeNode outside of a TreeView. To do this, ensure that the 'selected' and 'depth' props are provided.");
}

This would also mean that we wouldn't have to cast the types manually either.

Comment on lines +89 to 91
// @ts-ignore - will always be false according to prop types
[`${prefix}--tree--${size}`]: size !== 'default',
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also found this line of code a bit strange, considering size should only be xs or sm (according to types).

Copy link
Contributor

@Gururajj77 Gururajj77 left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the contribution

@Gururajj77 Gururajj77 added this pull request to the merge queue Aug 7, 2024
Merged via the queue into carbon-design-system:main with commit 6a32844 Aug 7, 2024
22 checks passed
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.

Add TypeScript types to TreeView, TreeNode
3 participants