From 36a7d530d81273cc1f894fa6afae43bfd5897a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Tue, 1 Oct 2024 09:14:18 +0200 Subject: [PATCH] feat: introduce new API --- README.md | 43 +++++---- example/src/Examples/FourTabs.tsx | 67 ++++++-------- example/src/Examples/ThreeTabs.tsx | 59 +++++------- example/src/Screens/Article.tsx | 11 ++- package.json | 3 + src/SceneMap.tsx | 34 +++++++ src/TabView.android.tsx | 54 ----------- src/TabView.tsx | 138 +++++++++++++++++++++++------ src/TabViewAdapter.android.tsx | 30 +++++++ src/TabViewAdapter.tsx | 7 ++ src/index.tsx | 2 + yarn.lock | 1 + 12 files changed, 268 insertions(+), 181 deletions(-) create mode 100644 src/SceneMap.tsx delete mode 100644 src/TabView.android.tsx create mode 100644 src/TabViewAdapter.android.tsx create mode 100644 src/TabViewAdapter.tsx diff --git a/README.md b/README.md index 620f0471..135a4d99 100644 --- a/README.md +++ b/README.md @@ -21,28 +21,33 @@ yarn add react-native-bottom-tabs ```tsx -import { TabView } from "react-native-bottom-tabs"; - -const items: TabViewItems = [ - { key: 'article', title: 'Article', icon: 'document.fill' }, - { key: 'albums', title: 'Albums', icon: 'square.grid.2x2.fill', badge: '3' }, - { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, -]; - -export default function App() { - const [selectedPage, setSelectedTab] = useState('contacts'); +import TabView, { SceneMap } from 'react-native-bottom-tabs'; + +export default function ThreeTabs() { + const [index, setIndex] = useState(0); + const [routes] = useState([ + { key: 'article', title: 'Article', icon: 'document.fill', badge: '!' }, + { + key: 'albums', + title: 'Albums', + icon: 'square.grid.2x2.fill', + badge: '5', + }, + { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, + ]); + + const renderScene = SceneMap({ + article: Article, + albums: Albums, + contacts: Contacts, + }); return ( - - - - + navigationState={{ index, routes }} + onIndexChange={setIndex} + renderScene={renderScene} + /> ); } ``` diff --git a/example/src/Examples/FourTabs.tsx b/example/src/Examples/FourTabs.tsx index 8a298968..5681cf34 100644 --- a/example/src/Examples/FourTabs.tsx +++ b/example/src/Examples/FourTabs.tsx @@ -1,53 +1,36 @@ -import { StyleSheet } from 'react-native'; -import TabView, { - type OnPageSelectedEventData, - type TabViewItems, -} from 'react-native-bottom-tabs'; +import TabView, { SceneMap } from 'react-native-bottom-tabs'; import { useState } from 'react'; import { Article } from '../Screens/Article'; import { Albums } from '../Screens/Albums'; -import { Chat } from '../Screens/Chat'; import { Contacts } from '../Screens/Contacts'; +import { Chat } from '../Screens/Chat'; -const items: TabViewItems = [ - { key: 'article', title: 'Article', icon: 'document.fill', badge: '!' }, - { key: 'albums', title: 'Albums', icon: 'square.grid.2x2.fill', badge: '5' }, - { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, - { key: 'chat', title: 'Chat', icon: 'keyboard' }, -]; - -export default function FourTabs() { - const [selectedPage, setSelectedTab] = useState('contacts'); - - const handlePageSelected = ({ - nativeEvent: { key }, - }: { - nativeEvent: OnPageSelectedEventData; - }) => setSelectedTab(key); +export default function ThreeTabs() { + const [index, setIndex] = useState(0); + const [routes] = useState([ + { key: 'article', title: 'Article', icon: 'document.fill', badge: '!' }, + { + key: 'albums', + title: 'Albums', + icon: 'square.grid.2x2.fill', + badge: '5', + }, + { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, + { key: 'chat', title: 'Chat', icon: 'keyboard' }, + ]); - const goToAlbums = () => { - setSelectedTab('albums'); - }; + const renderScene = SceneMap({ + article: Article, + albums: Albums, + contacts: Contacts, + chat: Chat, + }); return ( -
- - - - + navigationState={{ index, routes }} + onIndexChange={setIndex} + renderScene={renderScene} + /> ); } - -const styles = StyleSheet.create({ - fullWidth: { - width: '100%', - height: '100%', - }, -}); diff --git a/example/src/Examples/ThreeTabs.tsx b/example/src/Examples/ThreeTabs.tsx index fdc4d312..87ea997c 100644 --- a/example/src/Examples/ThreeTabs.tsx +++ b/example/src/Examples/ThreeTabs.tsx @@ -1,50 +1,33 @@ -import { StyleSheet } from 'react-native'; -import TabView, { - type OnPageSelectedEventData, - type TabViewItems, -} from 'react-native-bottom-tabs'; +import TabView, { SceneMap } from 'react-native-bottom-tabs'; import { useState } from 'react'; import { Article } from '../Screens/Article'; import { Albums } from '../Screens/Albums'; import { Contacts } from '../Screens/Contacts'; -const items: TabViewItems = [ - { key: 'article', title: 'Article', icon: 'document.fill', badge: '!' }, - { key: 'albums', title: 'Albums', icon: 'square.grid.2x2.fill', badge: '5' }, - { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, -]; - export default function ThreeTabs() { - const [selectedPage, setSelectedTab] = useState('contacts'); - - const handlePageSelected = ({ - nativeEvent: { key }, - }: { - nativeEvent: OnPageSelectedEventData; - }) => setSelectedTab(key); + const [index, setIndex] = useState(0); + const [routes] = useState([ + { key: 'article', title: 'Article', icon: 'document.fill', badge: '!' }, + { + key: 'albums', + title: 'Albums', + icon: 'square.grid.2x2.fill', + badge: '5', + }, + { key: 'contacts', title: 'Contacts', icon: 'person.fill' }, + ]); - const goToAlbums = () => { - setSelectedTab('albums'); - }; + const renderScene = SceneMap({ + article: Article, + albums: Albums, + contacts: Contacts, + }); return ( -
- - - + navigationState={{ index, routes }} + onIndexChange={setIndex} + renderScene={renderScene} + /> ); } - -const styles = StyleSheet.create({ - fullWidth: { - width: '100%', - height: '100%', - }, -}); diff --git a/example/src/Screens/Article.tsx b/example/src/Screens/Article.tsx index 2636684d..9e76905c 100644 --- a/example/src/Screens/Article.tsx +++ b/example/src/Screens/Article.tsx @@ -13,7 +13,7 @@ import { type Props = Partial & { date?: string; - onClick?: () => void; + jumpTo?: (key: string) => void; author?: { name: string; }; @@ -42,7 +42,7 @@ const DEFAULT_AUTHOR = { export function Article({ date = '1st Jan 2025', author = DEFAULT_AUTHOR, - onClick, + jumpTo, ...rest }: Props) { const ref = React.useRef(null); @@ -65,7 +65,12 @@ export function Article({ {date} -