Skip to content

chore: use TS generics for BottomTab types#3741

Merged
lukewalczak merged 13 commits intomainfrom
chore-ts-generics-for-bottom-tabs
Mar 20, 2023
Merged

chore: use TS generics for BottomTab types#3741
lukewalczak merged 13 commits intomainfrom
chore-ts-generics-for-bottom-tabs

Conversation

@tjaniczek
Copy link
Copy Markdown
Collaborator

@tjaniczek tjaniczek commented Mar 10, 2023

Summary

This PR switches BottomNavigation implementation to use generic type instead of hardcoded Route.

Due to the fact that BottomNavigation.Bar may be used with any router, we should not force users to adhere to Material's generic default types. As long as all methods passed to BottomNavigation.Bar are type safe the user should be able to define (or import from chosen router) the type himself.

This change is also required as a preparation for integration with React Navigation 7, that has two breaking changes related to MaterialBottomTabView:

  • removal of useLinkBuilder
  • removal of key in onTabPress events

There should be no changes to JS users. For TS users BottomNavigation should now correctly infer types in handlers (onTabPress, getLabelText and so on) - but if they used custom Route type previously - they had to disable type checking in their code anyway 🤷

Tested in both React Native 6 & 7, docs updated, added test to document the behaviour.

Test plan

It would be nice to install it on real-world app and see if everything works as expected.

@github-actions
Copy link
Copy Markdown

The mobile version of example app from this branch is ready! You can see it here.

@callstack-bot
Copy link
Copy Markdown

callstack-bot commented Mar 10, 2023

Hey @tjaniczek, thank you for your pull request 🤗. The documentation from this branch can be viewed here.

Comment thread example/src/Examples/BottomNavigationBarExample.tsx Outdated
Comment thread src/components/BottomNavigation/BottomNavigation.tsx Outdated
Comment thread src/components/BottomNavigation/BottomNavigationBar.tsx Outdated
Comment thread example/src/Examples/BottomNavigationBarExample.tsx Outdated
@tjaniczek tjaniczek requested a review from satya164 March 13, 2023 12:28
@tjaniczek
Copy link
Copy Markdown
Collaborator Author

State for now:

  • BottomNavigation allows custom state and properly infers Route state from the passed props
  • BottomNavigation.Bar allows custom state and properly infers Route state from the passed props
  • passing explicit Route shape is available when automatic inference is not possible
  • above behaviour documented in docs
  • works in RN6 & RN7.0.0-alpha.1 (with small change useLinkBuilder -> useLinkTools)
  • added test to check custom Route shape

* export default MyComponent;
* ```
*
* ## Usage with TypeScript
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ultimate goal for new docs is to introduce tabs JS and TS similar to npm and yarn so users can switch between those two and check which fit more for them.

cc: @DimitarNestorov (as far as I remember you were asking for that)

At the moment, since it's not available, maybe we can take advantage of my other PR introducing "More Examples" , wdyt?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, will change it as soon as Portal docs land in main 👍

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done: https://snack.expo.dev/@react-native-paper/bottomnavigation---usage-with-typescript

Please note that this snack will show warning at the moment, since this change is not merged yet. We'll need to update dependency in the snack when this change gets released 👍

Comment thread src/react-navigation/adapter.ts Outdated
@lukewalczak lukewalczak merged commit c665f5b into main Mar 20, 2023
@lukewalczak lukewalczak deleted the chore-ts-generics-for-bottom-tabs branch March 20, 2023 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants